Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 763
ItsGeorge Avatar asked Mar 03 '13 22:03

ItsGeorge


People also ask

What is associative arrays in PHP explain it with simple PHP code?

Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values. Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.

How do you convert an array into associative array?

Your code is the exact equivalent of: $assoc = array_fill_keys(array(1, 2, 3, 4, 5), 1); // or $assoc = array_fill_keys(range(1, 5), 1);

How do you remove a key and its value from an associative array in PHP?

Method 1: Using unset() function: The unset() function is used to unset a key and its value in an associative array. print_r( $arr ); ?> Method 2: Using array_diff_key() function: This function is used to get the difference between one or more arrays.


1 Answers

simply use array_values function:

$array = array_values($array); 
like image 88
bitWorking Avatar answered Sep 19 '22 21:09

bitWorking