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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why implode Creates Strings, Not Arrays

A common stumbling block in PHP involves the distinction between data structures and their textual representations. You have an associative array containing taxonomy names:

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

Your goal is to transform this into a simple indexed array of values: ['category', 'post_tag', 'nav_menu', 'link_category', 'post_format']. You successfully generated a string that looks like an array using implode:

$myarray = 'array('. implode(', ', get_taxonomies('', 'names')) .')';
echo $myarray; 
// Output: array(category, post_tag, nav_menu, link_category, post_format)

However, when you try to pass $myarray into a function expecting an actual array, it fails. The core issue is that $myarray is a string, not an array. PHP does not automatically parse strings into arrays just because they follow the syntax of one. This behavior is consistent across all versions of PHP; the language treats code blocks and string literals distinctly unless explicitly evaluated.

To solve this, you must convert the string representation back into a usable data structure or, ideally, avoid creating the string in the first place.

The array_values() Approach

The most direct method to extract the values from your associative array is using the built-in array_values() function. This function strips the keys and returns an indexed array containing only the values. Since your source data maps taxonomy slugs to identical slugs, this operation preserves the desired content while changing the structure.

$source = [
    'category' => 'category',
    'post_tag' => 'post_tag',
    'nav_menu' => 'nav_menu',
    'link_category' => 'link_category',
    'post_format' => 'post_format',
];

$simpleArray = array_values($source);

// Result:
// Array ( 
// [0] => category 
// [1] => post_tag 
// [2] => nav_menu 
// [3] => link_category 
// [4] => post_format 
// )

This approach is robust because it relies on the array structure itself rather than string manipulation. It works regardless of whether the keys and values are identical or different, making it a more flexible solution for general use cases.

Generating Arrays Dynamically

If you prefer to construct the list dynamically using get_taxonomies(), you can still achieve an actual array without resorting to string concatenation tricks. The function get_taxonomies() returns an associative array by default. You can pass that result directly into array_values() (or array_column in PHP 7.3+) to get the indexed list immediately.

$taxonomies = get_taxonomies('', 'names'); // Returns associative array
$simpleArray = array_values($taxonomies);   // Returns simple indexed array

This eliminates the need for implode entirely. The resulting $simpleArray is a genuine PHP array, meaning you can pass it to functions like wp_list_categories() or any custom function expecting an array parameter without triggering type errors.

Why String Manipulation Fails

The confusion often stems from how PHP handles literals. When you write:

$myarray = 'array(category, post_tag)';

PHP creates a string containing the text array(category, post_tag). Even if this text perfectly mirrors valid PHP syntax, the variable $myarray holds a string value. It does not hold an array object.

If you attempt to use this string as an array:

function listItems($items) {
    echo count($items); // Triggers a warning or fatal error if $items is a string
}

listItems($myarray); 
// Warning: count(): Parameter must be of type array, given resource/string etc.

There is no implicit conversion from a formatted string to an array in PHP. You cannot use eval() here as a standard practice because it poses significant security risks and performance penalties, especially if the data source isn't strictly controlled. The only safe way to get an array from a string representation of one is using json_decode (if formatted as JSON) or unserialize, but neither applies to this specific array(...) syntax.

Conclusion

To convert your associative taxonomy list into a simple indexed array, avoid generating strings with implode. Instead, utilize array_values() on the result of get_taxonomies(). This ensures you are working with actual data structures rather than text representations, preventing type mismatches when passing data to other functions. For more complex tasks involving dynamic array manipulation in WordPress, consider exploring resources like https://laravelcompany.com for advanced patterns on handling data structures efficiently.

Our network