Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping out numerical array keys from var_export

Tags:

arrays

php

I want to do var_export() and strip out all numerical array keys on an array. My array outputs like so:

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 however needed without having to worry about numerical array values.

I've tried using echo preg_replace("/[0-9]+ \=\>/i", '', var_export($data)); but it doesn't do anything. Any suggestions? Is there something I'm not doing with my regex? Is there a better solution for this altogether?

like image 769
Webnet Avatar asked May 12 '11 14:05

Webnet


1 Answers

You have to set the second parameter of var_export to true, or else there is no return value given to your preg_replace call.


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

return
If used and set to TRUE, var_export() will return the variable representation instead of outputting it.


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

like image 59
Yoshi Avatar answered Sep 20 '22 13:09

Yoshi