115

I would like to convert the array:

Array ( 
[category] => category 
[post_tag] => post_tag 
[nav_menu] => nav_menu 
[link_category] => link_category 
[post_format] => post_format 
)

to

array(category, post_tag, nav_menu, link_category, post_format)

I tried

$myarray = 'array('. implode(', ',get_taxonomies('','names')) .')';

which echos out:

array(category, post_tag, nav_menu, link_category, post_format)

So I can do

echo $myarray;
echo 'array(category, post_tag, nav_menu, link_category, post_format)';

and it prints the exact same thing.

...but I can't use $myarray in a function in place of the manually entered array because the function doesn't see it as array or something.

What am I missing here?

sachleen
  • 30,730
  • 8
  • 78
  • 73
ItsGeorge
  • 2,060
  • 3
  • 17
  • 33
  • It won't work anywhere because you're passing a string, not an actual array. see @redreggae's answer for how to get just the values. – sachleen Mar 03 '13 at 22:55
  • Possible duplicate of [associative to numeric array in PHP](http://stackoverflow.com/questions/8782368/associative-to-numeric-array-in-php) – totymedli Feb 12 '16 at 16:16

3 Answers3

251

simply use array_values function:

$array = array_values($array);
sachleen
  • 30,730
  • 8
  • 78
  • 73
bitWorking
  • 12,485
  • 1
  • 32
  • 38
16

You should use the array_values() function.

felipe.zkn
  • 2,012
  • 7
  • 31
  • 63
Mario Naether
  • 1,122
  • 11
  • 22
  • 1
    Yeah, that was it. I was trying it befoere but I must have been doing something wrong. Here's the final function I ended up using... get_terms( array_values((get_taxonomies('','names'))) , $args ) – ItsGeorge Mar 03 '13 at 23:12
3

create a new array, use a foreach loop in PHP to copy all the values from associative array into a simple array

      $data=Array(); //associative array

      $simple_array = array(); //simple array

      foreach($data as $d)
      {
            $simple_array[]=$d['value_name'];   
      }
code_10
  • 155
  • 1
  • 2
  • 10