Stripping out numerical array keys from var

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?


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.


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 function, shuffle , which will shuffle the array for you, then using a foreach loop or the next / each methods you can pull it out in the random order.

链接地址: http://www.djcxy.com/p/6908.html

上一篇: QTAgent32保持文件打开

下一篇: 从var中删除数字数组键