Convert an associative array to a simple array of its values in php

PhpArraysAssociative Array

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

simply use array_values function:

$array = array_values($array);

Solution 2 - Php

You should use the array_values() function.

Solution 3 - Php

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'];   
      }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionItsGeorgeView Question on Stackoverflow
Solution 1 - PhpbitWorkingView Answer on Stackoverflow
Solution 2 - PhpMario NaetherView Answer on Stackoverflow
Solution 3 - Phpcode_10View Answer on Stackoverflow