Function to flatten multilevel array of objects - php

I have a array of stdObjects, which are multilevel. I need to flatten them out.
Here is the array of objects:
[
(int) 0 => object(stdClass) {
link => ''
icon => ''
title => 'Main'
id => (int) 2
children => [
(int) 0 => object(stdClass) {
link => 'YTo0OntzOjEwOiJjb250cm9sbGVyIjtzOjU6InBhZ2VzIjtzOjY6ImFjdGlvbiI7czo3OiJkaXNwbGF5IjtzOjU6InBhc3MwIjtzOjQ6ImhvbWUiO3M6NToicGFzczEiO3M6MDoiIjt9'
icon => 'fa-tachometer-alt'
title => 'Dashboard'
id => (int) 3
}
]
}
]
Now I created following function to reduce the multilevel (can go to 4 levels deep) to single-level and converting the objects to a classic array:
function reduceArrayOfObjects(array $array, array $flatArray, $children = 'children') {
foreach($array as $lvl1) {
$lvl1 = (array) $lvl1;
if(isset($lvl1[$children])) {
reduceArrayOfObjects($lvl1[$children], $flatArray, $children);
unset($lvl1[$children]);
}
$flatArray[] = $lvl1;
}
return $flatArray;
}
$multiLevel = []; // this is the array of objects
$newArray = [];
reduceArrayOfObjects($multiLevel, $newArray);
It won't work properly, I only get the first level back into my new array. I tried lots of variations on how to get to my result, but all of those are failing. When I hardcode all foreach() loops, it works...
This is my current output:
[
(int) 0 => [
'link' => '',
'icon' => '',
'title' => 'Main',
'id' => (int) 2
]
]
Anyone has an idea how to solve my problem?

Thanks to #NigelRen I found the solution.
I just had to add & to the function:
reduceArrayOfObjects(array $array, array &$flatArray, $children = 'children) {}

Related

Laravel Format AllDirectories() output into tree

To start off with, I have checked all resources I could for examples but was not able to find one that brought me close enough so i can resolve this query (simple as it may seem).
I've also seen there is a question that is the same but never resolved here: Get allDirectories() in Laravel and create a tree
I'll also just use the same sample data cause it's the exact same scenario.
I basically get an output from laravel's AllDirectories() function which output's something like this:
array:20 [▼
0 => "test"
1 => "files"
2 => "files/2"
3 => "files/2/Blocks"
4 => "files/2/Blocks/thumbs"
5 => "files/shares"
]
And I want to convert that into a multidimensional array that looks something like this:
[
["label" => "test", "path" => "test", "children" => []],
["label" => "files", "path" => "files", "children" =>
[
["label" => "2", "path" => "files/2", "children" =>
[
["label" => "Blocks", "path" => "files/2/Blocks", "children" =>
[
[
"label" => "thumbs", "path" => "files/2/Blocks/thumbs", "children" => []
]
]
]
]
],
["label" => "shares", "path" => "files/shares", "children" => []]
]
],
];
How can one go about converting the output from AllDirectories() to a multidimensional array?
Thanks in advance for any tips or tricks :)
You could exploit laravel collections and a bit of recursion to achieve what you need.
I wrote a function which works on a preprocessed output (array instead of plain string) and does the following steps:
Take all given paths and create groups based on the first segment of each path.
For each created group, take its children paths and remove the first segment from each children (filter out empty paths).
Execute convertPathsToTree function on the children paths, and assign its output result to the children key of the resulting tree structure.
Here is the code:
function convertPathsToTree($paths, $separator = '/', $parent = null)
{
return $paths
->groupBy(function ($parts) {
return $parts[0];
})->map(function ($parts, $key) use ($separator, $parent) {
$childrenPaths = $parts->map(function ($parts) {
return array_slice($parts, 1);
})->filter();
return [
'label' => (string) $key,
'path' => $parent . $key,
'children' => $this->convertPathsToTree(
$childrenPaths,
$separator,
$parent . $key . $separator
),
];
})->values();
}
Usage
First of all, let's assume the paths are assigned as a collection to a $data variable:
$data = collect([
'test',
'files',
'files/2',
'files/2/Blocks',
'files/2/Blocks/thumbs',
'files/shares',
]);
You first need to pre-process the array by splitting each path with the directory separator (/ in this example). This can be done with a simple map call:
$processedData = $data->map(function ($item) {
return explode('/', $item);
});
Then, you can use the above function and provide the transformed input to obtain the requested structure:
convertPathsToTree($processedData);
If you would rather obtain an output array instead of an collection, add ->toArray(); after the ->values() call at the end of the function.
STEPS
Convert the paths into arrays.
Find the maximum path depth.
Group paths based on their level of depth.
Merge groupings in a hierarchical format.
Reset the result's array indices/keys.
Print output.
$rawPaths = [
0 => "test",
1 => "files",
2 => "files/2",
3 => "files/2/Blocks",
4 => "files/2/Blocks/thumbs",
5 => "files/karma",
6 => "files/karma/foo",
7 => "files/karma/foo/bar",
8 => "files/shares",
];
// 1. Convert the paths into arrays.
$paths = array_map(function ($path) {
return explode("/", $path);
}, $rawPaths);
// 2. Find the maximum path depth.
$maxDepth = 0;
for ($i = 0; $i < count($rawPaths); $i++) {
if (($count = substr_count($rawPaths[$i], "/")) > $maxDepth) {
$maxDepth = $count;
}
}
// 3. Group paths based on their level of depth.
$groupings = [];
for ($j = 0; $j <= $maxDepth; $j++) {
$groupings[] = array_filter($paths, function ($p) use ($j) {
return count($p) === ($j + 1);
});
}
// 4. Merge groupings in a hierarchical format.
$result = [];
for ($depth = 0; $depth <= $maxDepth; $depth++) {
array_map(function ($grouping) use (&$result, $depth) {
setNode($result, $grouping, $depth);
}, $groupings[$depth]);
}
function setTree(&$grouping, &$depth): array
{
$pathBuilder = $grouping[$depth];
for ($i = 0; $i < $depth; $i++) {
$pathBuilder = $grouping[$depth - ($i + 1)] . "/" . $pathBuilder;
}
return [
"label" => $grouping[$depth],
"path" => $pathBuilder,
"children" => []
];
}
function setNode(&$result, $grouping, $depth)
{
$node = &$result[$grouping[0]];
if ($depth) {
for ($i = ($depth - 1); $i >= 0; $i--) {
$node = &$node["children"][$grouping[$depth - $i]];
}
}
$node = setTree($grouping, $depth);
}
// 5. Reset the result's array indices/keys.
$arrayIterator = new \RecursiveArrayIterator(array_values($result));
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $key => $value) {
if (is_array($value) && ($key === "children")) {
$value = array_values($value);
// Get the current depth and traverse back up the tree, saving the modifications.
$currentDepth = $recursiveIterator->getDepth();
for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) {
// Get the current level iterator.
$subIterator = $recursiveIterator->getSubIterator($subDepth);
// If we are on the level we want to change, use the replacements ($value), otherwise set the key to the parent iterators value.
$subIterator->offsetSet($subIterator->key(), ($subDepth === $currentDepth ? $value : $recursiveIterator->getSubIterator(($subDepth + 1))->getArrayCopy()));
}
}
}
// 6. Print output.
var_export($recursiveIterator->getArrayCopy());
// Output
array (
0 =>
array (
'label' => 'test',
'path' => 'test',
'children' =>
array (
),
),
1 =>
array (
'label' => 'files',
'path' => 'files',
'children' =>
array (
0 =>
array (
'label' => '2',
'path' => 'files/2',
'children' =>
array (
0 =>
array (
'label' => 'Blocks',
'path' => 'files/2/Blocks',
'children' =>
array (
0 =>
array (
'label' => 'thumbs',
'path' => 'files/2/Blocks/thumbs',
'children' =>
array (
),
),
),
),
),
),
1 =>
array (
'label' => 'karma',
'path' => 'files/karma',
'children' =>
array (
0 =>
array (
'label' => 'foo',
'path' => 'files/karma/foo',
'children' =>
array (
0 =>
array (
'label' => 'bar',
'path' => 'files/karma/foo/bar',
'children' =>
array (
),
),
),
),
),
),
2 =>
array (
'label' => 'shares',
'path' => 'files/shares',
'children' =>
array (
),
),
),
),
)

PHP Nested Arrays: Imploding all the tree keys of each leaf results in a multidimensional array instead of a 1D associative one

From a nested array, I want to generate the 1D associative array which contains, for each leaf, its ascending keys concatenation.
Summary
Expected results example
1.1. Input
1.2. Output
Actual results example
1.1. Input
1.2. Output
Question
Minimal, Testable Executable Sources
4.1. Explanations
4.2. Sources & Execution
Expected results example
Input
The following nested array:
[
'key1' => 'foo',
'key2' => [
'key3' => [
0 => ['key4' => 'bar' ],
1 => ['key4' => 'azerty']
]
]
]
Output
The following 1D associative array (glue character for the concatenation of the keys: _):
[
'key1' => 'foo',
'key2_key3_0_key4' => 'bar',
'key2_key3_1_key4' => 'azerty'
]
Actual results example
Input
[
'etat' => 'bar',
'proposition_en_cours' => [
'fichiers' => [
0 => ['url_fichier' => 'foo' ],
1 => ['url_fichier' => 'bar']
]
]
]
Output
Array
(
[] => bar
[proposition_en_cours] => Array
(
[fichiers] => Array
(
[0] => Array
(
[url_fichier] => foo
)
[1] => Array
(
[url_fichier] => bar
)
)
)
[proposition_en_cours_fichiers] => Array
(
[0] => Array
(
[url_fichier] => foo
)
[1] => Array
(
[url_fichier] => bar
)
)
[proposition_en_cours_fichiers_0] => foo
[proposition_en_cours_fichiers_0_1] => bar
)
Question
As you can see, the array I get differs in all points from the expected one. I can't figure out why.
Minimal, Testable Executable Sources
Explanations
I initialize an array that must contain all the ascending keys for each leaf: $key_in_db_format = [];.
I iterate on the input array. For each element (leaf or subarray), I pop $key_in_db_format if, and only if, the current depth equals the last depth. If it's an array (i.e.: not a leaf): I add the key to $key_in_db_format. I set a value (the leaf) at the key that is the concatenation of the ascending keys.
Sources & Execution
First, define this array in an empty PHP script of your choice:
$values = [
'etat' => 'bar',
'proposition_en_cours' => [
'fichiers' => [
0 => [ 'url_fichier' => 'foo' ],
1 => [ 'url_fichier' => 'bar' ]
]
]
];
Then, copy/paste the following code and you will be able to execute it:
$values_to_insert_in_meta_table = [];
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($values), \RecursiveIteratorIterator::SELF_FIRST);
$last_depth = 0;
$key_in_db_format = [];
foreach ($iterator as $value_key_field => $value_value_field) {
if($iterator->getDepth() == $last_depth) {
array_pop($key_in_db_format);
}
if(is_array($value_value_field)) {
array_push($key_in_db_format, $value_key_field);
} else {
$values_to_insert_in_meta_table[implode('_', $key_in_db_format)] = $value_value_field;
}
$last_depth = $iterator->getDepth();
}
echo '<pre>';
print_r($values_to_insert_in_meta_table);
Maybe I missed something, but as far as I understand, I would do something like that:
<?php
function flatten(array $array, ?string $prefix = null): array {
$prefix = $prefix === null ? '' : "{$prefix}_";
$output = [];
foreach ($array as $key => $value) {
$key = $prefix . $key;
if (is_array($value)) {
$output = array_merge($output, flatten($value, $key));
} else {
$output[$key] = $value;
}
}
return $output;
}
var_export(flatten([
'key1' => 'foo',
'key2' => [
'key3' => [
0 => ['key4' => 'bar' ],
1 => ['key4' => 'azerty']
]
]
]));
Output:
array (
'key1' => 'foo',
'key2_key3_0_key4' => 'bar',
'key2_key3_1_key4' => 'azerty',
)
I think I've found a solution!!! :-)
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($values), \RecursiveIteratorIterator::SELF_FIRST);
$key_in_db_format = [];
$current_counter = 0;
foreach($iterator as $value_key_field => $value_value_field) {
if(is_array($value_value_field)) {
$current_counter = 0;
array_push($key_in_db_format, $value_key_field);
}
if(!is_array($value_value_field)) {
$key_to_insert_in_db = !empty($key_in_db_format) ? implode('_', $key_in_db_format) . '_' . $value_key_field : $value_key_field ;
$values_to_insert_in_meta_table[$key_to_insert_in_db] = $value_value_field;
if($current_counter == count($iterator->getSubIterator())) {
array_pop($key_in_db_format);
}
$current_counter++;
}
}
echo '<br /> <pre>';
print_r($values_to_insert_in_meta_table);
exit;
The idea is:
We add to the array of ascendent keys the key if, and only if, the current element is not a leaf.
If the current element is a leaf, then we define the key equalled to the imploded ascendent keys PLUS (concatenation) the current element's key. Moreover we pop the array of ascendent keys if there are not following siblings elements.

Preserve elements in each row of a two-dimensional array based on another array

I have this array:
0 => array:3 [
"product_id" => "1138"
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png"
"product_sku" => "6500722"
]
1 => array:3 [
"product_id" => "1144"
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png"
"product_sku" => "6501046"
]
2 => array:3 [
"product_id" => "113"
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png"
"product_sku" => "6294915"
]
What I am looking for is a way to get a multiple array with only required columns (array_column is not a option, since it's give me only 1 column).
What I have done
function colsFromArray($array, $keys)
{
return array_map(function ($el) use ($keys) {
return array_map(function ($c) use ($el) {
return $el[$c];
}, $keys);
}, $array);
}
$array = array(
[
"product_id" => "1138",
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
"product_sku" => "6500722"
],
[
"product_id" => "1144",
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
"product_sku" => "6501046"
],
[
"product_id" => "113",
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
"product_sku" => "6294915"
]
);
colsFromArray($array, array("product_id", "product_sku"));
//0 => array:3 [
// "product_id" => "1138"
// "product_sku" => "6500722"
// ]
//1 => array:3 [
// "product_id" => "1144"
// "product_sku" => "6501046"
// ]
//2 => array:3 [
// "product_id" => "113"
// "product_sku" => "6294915"
//]
The problem is that it seems too laggy, since it iterates twice over this.
Is there any way to get multiple columns without this workaround?
I'm using PHP5.6
If you need two columns from an array where one is SKU (which generally is unique) then you can use array_column with the third parameter.
$new = array_column($arr, "product_id", "product_sku");
This will return a flat array with the SKU as the key and ID as value making the array easy to work with also.
Output:
array(3) {
[6500722]=>
string(4) "1138"
[6501046]=>
string(4) "1144"
[6294915]=>
string(3) "113"
}
https://3v4l.org/UDGiO
I think the bigger issue is you lose the keys
Original Code
array (
0 =>
array (
0 => '1138',
1 => '6500722',
),
1 =>
array (
0 => '1144',
1 => '6501046',
),
2 =>
array (
0 => '113',
1 => '6294915',
);
You can use a simple foreach instead of the second array_map:
function colsFromArray(array $array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
return array_map(function ($el) use ($keys) {
$o = [];
foreach($keys as $key){
// if(isset($el[$key]))$o[$key] = $el[$key]; //you can do it this way if you don't want to set a default for missing keys.
$o[$key] = isset($el[$key])?$el[$key]:false;
}
return $o;
}, $array);
}
Output
array (
0 =>
array (
'product_id' => '1138',
'product_sku' => '6500722',
),
1 =>
array (
'product_id' => '1144',
'product_sku' => '6501046',
),
2 =>
array (
'product_id' => '113',
'product_sku' => '6294915',
),
)
Sandbox
the problem is that it seems too laggy, since it iterates twice over this.
There is no real way to not iterate over it 2 times, but you probably don't want to throw away the keys either.
That said you can recursively unset the items you don't want.
function colsFromArray(array &$array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
foreach ($array as $key => &$value) {
if (is_array($value)) {
colsFromArray($value, $keys); //recursive
}else if(!in_array($key, $keys)){
unset($array[$key]);
}
}
}
colsFromArray($array, array("product_id", "product_sku"));
var_export($array);
Same output as before
This is easier to do by reference. Rather or not that is faster you'll have to test the 2 and see.
Sandbox
As a final note you shouldn't assume the key will exist or that keys will be an array unless you type cast it as an array.
You could also do it with array filter
function colsFromArray(array $array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
$filter = function($k) use ($keys){
return in_array($k,$keys);
};
return array_map(function ($el) use ($keys,$filter) {
return array_filter($el, $filter, ARRAY_FILTER_USE_KEY );
}, $array);
}
There is some small performance benefit to declaring the function for filtering outside of the loop (array_map).
Sandbox
If you do not want to change your original array and want your desired output
Use array_insersect_key function to get your desired output as following
$array = array(
[
"product_id" => "1138",
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
"product_sku" => "6500722"
],
[
"product_id" => "1144",
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
"product_sku" => "6501046"
],
[
"product_id" => "113",
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
"product_sku" => "6294915"
]
);
$keys = array("product_id"=>1, "product_sku"=>2);
$filteredArray = array_map(function($a) use($keys){
return array_intersect_key($a,$keys);
}, $array);
print_r($filteredArray);
I refactored the elegant approach from #Chayan into a function so it can be used like array_column(). Keys to be filtered can now be presented as a simple array.
Btw this is most likely also the fastest approach, since it uses build-in functions for most of the heavy lifting.
function array_columns(array $arr, array $keysSelect)
{
$keys = array_flip($keysSelect);
return array_map(
function($a) use($keys) {
return array_intersect_key($a,$keys);
},
$arr
);
}
$arr = [
[
"product_id" => "1138",
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
"product_sku" => "6500722"
],
[
"product_id" => "1144",
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
"product_sku" => "6501046"
],
[
"product_id" => "113",
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
"product_sku" => "6294915"
]
];
$keysSelect = ["product_id" , "product_sku"];
$filteredArray = array_columns($arr, $keysSelect);
var_dump($filteredArray);
If I understand your question correctly, you could try a traditional foreach - it might be a little faster.
function colsFromArray($array, $filterKeys) {
$newArr = [];
foreach($array as $val) {
$element = [];
foreach($filterKeys as $filterKey) {
$element[$filterKey] = $val[$filterKey];
}
$newArr[] = $element;
}
}
(Not tested)
The problem is that it seems too laggy, since it iterates twice over this
Your original code isn't iterating twice over the same array. You won't be able to get around iterating over the main array and then the filterKeys array if you want to have an array where each element is another array of elements with keys from the filterKeys array.
This is a refactored function based on Chayan's with added renaming of selected columns:
/** Function - array_columns Selects columns from multidimantional array and renames columns as required
*
* #param array $arr, array $selectColRenameKeys
* example: (NewName1->colNameneeded1,NewName2->colNameneeded2,ect...)
* #return array
* #access public
*
*/
private function array_columns( $arr,$selectColRenameKeys) {
$keys = array_flip($selectColRenameKeys);
$filteredArray = array_map(function($a) use($keys){
$data = array_intersect_key($a,$keys);
$rename_arr= array();
foreach ($data as $colname => $value){
$r_arr[$keys[$colname]]= $value ;
}
return $r_arr;
}, $arr);
return $filteredArray;
}
An added feature to the array_columns function that eventually traces back to Chayan's answer, this time extended from Joseph Mangion's function.
I occasionally have long lists of the selected columns where I want to preserve the keys and don't necessarily want to follow the cumbersome ['orignal_field_name'] => ['original_field_name'] format for a great number of fields.
This version preserves the original key for each field by default unless a new key is specified.
// See answer from Joseph Mangion: https://stackoverflow.com/questions/52706383/php-get-multiple-columns-from-array
/** Function - array_columns Selects columns from multidimensional array and renames columns as required
*
* #param array $in_array, array $select_columns_rename_keys
* example of $select_columns_rename_keys:
* ['new_column_name1' => 'original_column_name1', 'original_column_name2', 'original_column_name3', 'new_column_name4' => 'original_column_name4', ...]
* This will use the original keys for columns 2 and 3 and rename columns 1 and 4
* #return array
* #access public
*
*/
public function array_columns($in_array, $select_columns_rename_keys) {
foreach ($select_columns_rename_keys as $k => $v)
if (is_int($k)) {
$select_columns_rename_keys[$v] = $v;
unset($select_columns_rename_keys[$k]);
}
$keys = array_flip($select_columns_rename_keys);
$filtered_array =
array_map(function($a) use($keys) {
$data = array_intersect_key($a, $keys);
$return_array = [];
foreach ($data as $column_name => $value) $return_array[$keys[$column_name]] = $value;
return $return_array;
}, $in_array);
return $filtered_array;
}

Counting distinct values in multidimensional array

I'm having real problems trying to figure this one out.
I have a PHP array which looks like this:
$info = array();
$info[0] = array(
'car' => 'Audi',
'previous_car' => 'BMW'
);
$info[1] = array(
'car' => 'Audi',
'previous_car' => 'Seat'
);
$info[2] = array(
'car' => 'Audi',
'previous_carg' => 'BMW'
);
$info[3] = array(
'car' => 'BMW',
'previous_car' => 'BMW'
);
$info[4] = array(
'car' => 'Ford',
'previous_car' => 'Seat'
);
I need to do some sorting on this, so the result looks like this:
Array (
car [
'Audi' => 3,
'BMW' => 1,
'Ford' => 1
],
previous_car [
'BMW' => 3,
'Seat' => 2
]
);
I need to count distinct occurrences of a value in the same key, but the search is made upon couple of arrays. I was trying to use array_value_count(), but I doesn't work well on multidimensional arrays.
I am trying to avoid the looping, since it can be overkill if the array is large.
I will be very grateful for all the help.
If you're running PHP 5.5, you can use:
$newArray = array(
'car' => array_count_values(array_column($info, 'car')),
'previous_car' => array_count_values(array_column($info, 'previous_car'))
);
var_dump($newArray);
For versions of PHP prior to 5.5
$newArray = array(
'car' => array_count_values(
array_map(
function($value) {
return $value['car'];
},
$info
)
),
'previous_car' => array_count_values(
array_map(
function($value) {
return $value['previous_car'];
},
$info
)
)
);
var_dump($newArray);
In a more object orientated way you can solve it as follows
$values = new ArrayObject();
$iterator = new RecursiveArrayIterator($info);
iterator_apply($iterator, 'countDistinct', array($iterator, $values));
function countDistinct($iterator, $values) {
while ( $iterator -> valid() ) {
if ( $iterator -> hasChildren() ) {
countDistinct($iterator -> getChildren(), $values);
} else {
if (!$values->offsetExists($iterator->key())) {
$values->offsetSet($iterator->key(), new ArrayObject());
}
if (!$values->offsetGet($iterator->key())->offsetExists($iterator->current())) {
$values->offsetGet($iterator->key())
->offsetSet($iterator->current(), 1);
} else {
$values->offsetGet($iterator->key())
->offsetSet($iterator->current(),
$values->offsetGet($iterator->key())->offsetGet($iterator->current()) + 1);
}
}
$iterator -> next();
}
}
Sure, with this example you do not avoid the loop. But with the ArrayObject and the RecursiveArrayIterator you will have some memory and performance advantages.
The result of this will exactly match your expected result, which you can easyliy iterate with the getIterator() function of the ArrayObject.
You can write a function that will sort your data but for now check this out:
http://www.php.net/manual/en/function.array-multisort.php
Here is what might help you:
$returnArray = array('car' => NULL, 'previous_car' => NULL);
foreach($info as $newInfo) {
$returnArray['car'][] = $newInfo['car'];
$returnArray['previous_car'][] = $newInfo['previous_car'];
}
$ret['car'] = array_count_values($returnArray['car']);
$ret['previous_car'] = array_count_values($returnArray['previous_car']);
var_dump($ret);
This returns:
array (size=2)
'car' =>
array (size=3)
'Audi' => int 3
'BMW' => int 1
'Ford' => int 1
'previous_car' =>
array (size=2)
'BMW' => int 3
'Seat' => int 2

Get path and value of all elements in nested associative array

Consider an associative array of arbitrary form and nesting depth, for example:
$someVar = array(
'name' => 'Dotan',
'age' => 35,
'children' => array(
0 => array(
'name' => 'Meirav',
'age' => 6,
),
1 => array(
'name' => 'Maayan',
'age' => 4,
)
),
'dogs' => array('Gili', 'Gipsy')
);
I would like to convert this to an associative array of paths and values:
$someVar = array(
'name' => 'Dotan',
'age' => 35,
'children/0/name' => 'Meirav',
'children/0/age' => 6,
'children/1/name' => 'Maayan',
'children/1/age' => 4,
'dogs/0' => 'Gili',
'dogs/1' => 'Gipsy'
);
I began writing a recursive function which for array elements would recurse and for non-array elements (int, floats, bools, and strings) return an array $return['path'] and $return['value']. This got sloppy quick! Is there a better way to do this in PHP? I would assume that callables and objects would not be passed in the array, though any solution which deals with that possibility would be best. Also, I am assuming that the input array would not have the / character in an element name, but accounting for that might be prudent! Note that the input array could be nested as deep as 8 or more levels deep!
Recursion is really the only way you'll be able to handle this, but here's a simple version to start with:
function nested_values($array, $path=""){
$output = array();
foreach($array as $key => $value) {
if(is_array($value)) {
$output = array_merge($output, nested_values($value, (!empty($path)) ? $path.$key."/" : $key."/"));
}
else $output[$path.$key] = $value;
}
return $output;
}
function getRecursive($path, $node) {
if (is_array($node)) {
$ret = '';
foreach($node as $key => $val)
$ret .= getRecursive($path.'.'.$key, $val);
return $ret;
}
return $path.' => '.$node."\n";
}
$r = getRecursive('', $someVar);
print_r($r);
All yours to place it in an array.

Categories