This single line would do that:

$array = array_column($array, 'plan');

The first argument is an array | The second argument is an array key.

For details, go to official documentation: https://www.php.net/manual/en/function.array-column.php.

Answer from Usman Ahmed on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 68586536 › need-to-convert-array-of-index-array-to-single-dimension-index-array-in-php
Need to convert array of index array to single dimension index array in php - Stack Overflow
This is the correct solution refereed from How to Flatten a Multidimensional Array? Copy$a=[]; $a[0]=[1,2,3]; $a[1]=[4,5,6]; $a[2]=[7,8,9]; $a[3]=[1,4,7]; $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a)); $output=iterator_to_array($it, FALSE); ... Save this answer. ... Show activity on this post. Copy$result = array_merge(...$a); print_r($result); ... Save this answer. ... Show activity on this post. ... Copy<?php $a=[]; $a[0]=[1,2,3]; $a[1]=[4,5,6]; $a[2]=[7,8,9]; $a[3]=[1,4,7]; $result = []; array_walk_recursive($a,function($val) use (&$result){ $result[] = $val; }); var_dump($result);
🌐
PHP
php.net › manual › en › function.array-values.php
PHP: array_values - Manual
Doing this will cause PHP exceeds the momory limits: <?php $bigArray = array_values( $bigArray ); ?> It's because array_values() does not re-index $bigArray directly, it just re-index it into another array, and assign to itself later. ... This is another way to get value from a multidimensional ...
Top answer
1 of 2
126

A quick way to do this is:

$obj = json_decode(json_encode($array));

Explanation

json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)

json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)

I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.

2 of 2
7

The best way would be to manage your data structure as an object from the start if you have the ability:

$a = (object) array( ... ); $a->prop = $value; //and so on

But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a)).

You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:

function convert_array_to_obj_recursive($a) {
    if (is_array($a) ) {
        foreach(k => $v) {
            if (is_integer($k)) {
                // only need this if you want to keep the array indexes separate
                // from the object notation: eg. a['index'][$k] = convert_array_to_obj_recursive($v);
            }
            else {
                k] = convert_array_to_obj_recursive($v);
            }
        }

        return (object) $a;
    }

    // else maintain the type of $a
    return $a; 
}

Hope that helps.

EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar') ), you will not be able to reference the numerical indexes with object notation (eg. o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do $o->index[1]. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.

🌐
Fellow Tuts
fellowtuts.com › home › php › 3 ways – multidimensional array to object conversion
3 Ways - Multidimensional Array to Object Conversion
December 18, 2018 - Here we are moving all indexed keys under a property named ‘index’ and then converting the array to object. We did so because neither the notation $obj->1 nor $obj[0] works as well as both throw PHP error. But the object notation $obj->index{0} returns the expected value. Finally, those are three different ways to convert a multidimensional array to object.
🌐
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-php › lessons › multidimensional-arrays-and-their-traversal-in-php
Multidimensional Arrays and Their Traversal in PHP
To create a multidimensional array in PHP, we use arrays of arrays. Here is an example demonstrating how to create and work with 2D arrays. ... <?php // Creating a 2D array $array = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; // Printing the 2D array print_r($array); ?> ... All indices in PHP arrays are 0-based. Suppose you want to visit an apartment on the second floor (index 1) and deliver a package to the first unit (index 0) in this building.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 51034016 › php-multidimensional-array-keep-index-array
php - multidimensional array (keep index array) - Stack Overflow
Pop through each of those wtih a foreach and build the new array, top level being indexed on $i and each of those elements containing your associative array. <?php $arr[0][]=array('2018-06-13'=>"hadir"); $arr[0][]=array('2018-06-12'=>"hadir"); ...
🌐
SitePoint
sitepoint.com › php
Convert Multidimensional array to single array - PHP - SitePoint Forums | Web Development & Design Community
April 26, 2014 - $newArray = array(); foreach($multi as $array) { foreach($array as $k=>$v) { $newArray[$k] = $v; } } This loops through your multidimensional array and stores the results in the new array variable $newArray.
🌐
SitePoint
sitepoint.com › php
Convert multidimensional associative array to numeric index - PHP - SitePoint Forums | Web Development & Design Community
August 27, 2010 - I have some ugly multidimensional associative arrays that I’d really rather deal with as numeric indexes… indexii ? anyway, I’ve tried array_values, but it’s not recursive. Also, I’m not interested in flattening the arra…
🌐
GitHub
gist.github.com › SeanCannon › 6585889
PHP array_flatten() function. Convert a multi-dimensional array into a single-dimensional array. · GitHub
PHP array_flatten() function. Convert a multi-dimensional array into a single-dimensional array. - array_flatten.php
🌐
GeeksforGeeks
geeksforgeeks.org › php › multidimensional-arrays-in-php
Multidimensional arrays in PHP - GeeksforGeeks
July 11, 2025 - It is the simplest form of a multidimensional array. It can be created using nested array. These type of arrays can be used to store any type of elements, but the index is always a number. By default, the index starts with zero. ... Example: In this example we creates a two-dimensional array containing names and locations. The print_r() function is used to display the structure and contents of the array, showing the nested arrays and their values. ... <?php // PHP program to create // multidimensional array // Creating multidimensional // array $myarray = array( // Default key for each will // start from 0 array("Ankit", "Ram", "Shyam"), array("Unnao", "Trichy", "Kanpur") ); // Display the array information print_r($myarray); ?>
🌐
FlatCoding
flatcoding.com › home › php multidimensional arrays: data manipulation
PHP Multidimensional Arrays: Data Manipulation - FlatCoding
April 15, 2025 - In this example, we used two nested loops to access each key-value pair in the multidimensional array. The outer loop iterates through each product array, while the inner loop iterates through each key-value pair within the product array. We then print out each key-value pair and add a separator between each product. Another method to access the array is by directly referencing the values through the index name or number. Let’s see an example. <?php // Access the second product in the $products array $product = $products[1]; // Access the price of the second product $price = $product["price"]; // Print out the price of the second product echo "The price of Product 2 is $" .