Removing rows from a var_export array

I want to do var_export () and cross out all the numeric keys of the array in the array. My array is output like this:

array ( 2 => array ( 1 => array ( 'infor' => 'Radiation therapy & chemo subhead', 'PPOWithNotif' => '', 'PPOWithOutNotif' => 'Radiation therapy & chemo PPO amount', 'NonPPO' => 'Radiation therapy & chemo Non PPO amount', ), ), 3 => array ( 1 => array ( 'infor' => 'Allergy testing & treatment subhead', 'PPOWithNotif' => '', 'PPOWithOutNotif' => 'Allergy testing & treatment PPO amount', 'NonPPO' => 'Allergy testing & treatment Non PPO amount', ), ) ) 

By doing this, I can shuffle the array values ​​that you need without worrying about the numerical values ​​of the array.

I tried using echo preg_replace("/[0-9]+ \=\>/i", '', var_export($data)); but he does nothing. Any suggestions? Is there something I am not doing with my regex? Is there a better solution for all this?

+7
source share
3 answers

You must set the second var_export parameter to true , otherwise there is no return value specified by your preg_replace call.


Link: https://php.net/manual/function.var-export.php

return
If TRUE is used and set, var_export () will return a variable instead of outputting it.


Update: Looking back at this question, I have a hunch, a simple array_values($input) would be enough.

+3
source

Why not just use array_rand :

  $keys = array_rand($array, 1); var_dump($array[$keys[0]]); // should print the random item 

PHP also has a shuffle function that will shuffle the array for you, and then use a foreach or next / each foreach , you can pull it out at random.

0
source

It may not be the answer you are looking for, but if you have a sibling array , you can use the function below. It may not be pretty, but it worked for me.

 function arrayToText($array, $name = 'new_array') { $out = ''; foreach($array as $item) { $export = var_export($item, true); $export = str_replace("array (\n", '', $export); $export = substr($export, 0, -1); $out .= "[\n"; $out .= $export; $out .= "],\n"; } return '$' . $name . ' = ' . "[\n" . substr($out, 0, -2) . "\n];"; } echo arrayToText($array); 
0
source

All Articles