How can I convert a multidimensional array into one level array? [duplicate] - php

This question already has answers here:
Turning multidimensional array into one-dimensional array [duplicate]
(11 answers)
Closed 2 years ago.
I have a multidimensional array, but I want to put all values into one single array:
array:28 [▼
0 => array:8 [▼
0 => "id"
1 => "monkey"
2 => "horse"
3 => "bird"
4 => "elephant"
5 => "cat"
6 => "whale"
7 => "frog"
]
1 => array:4 [▼
0 => "id"
1 => "whale"
2 => "lion"
3 => "dog"
]
2 => array:4 [▶]
3 => array:4 [▶]
4 => array:3 [▶]
5 => array:4 [▶]
6 => array:10 [▶]
7 => array:3 [▶]
8 => array:5 [▶]
9 => array:18 [▶]
10 => array:3 [▶]
11 => array:4 [▶]
12 => array:5 [▶]
13 => array:3 [▶]
14 => array:6 [▶]
15 => array:3 [▶]
16 => array:3 [▶]
17 => array:3 [▶]
18 => array:3 [▶]
19 => array:4 [▶]
20 => array:6 [▶]
21 => array:3 [▶]
22 => array:5 [▶]
23 => array:8 [▶]
24 => array:3 [▶]
25 => array:5 [▶]
26 => array:3 [▶]
27 => array:5 [▶]
]
My approach:
$singleArray = array();
foreach ($multiArray as $key => $value){
$singleArray[] =$value;
array_merge($singleArray, $value);
}
dump($singleArray);
But this is again creating a multidimensional array

It looks like you want all sub array values to be in the single array.
$singleArray = [];
foreach($multiarray as $array) {
$singleArray = array_merge($singleArray, array_values($array));
}
This may contain some values as a duplicate. To clean them up you can do
$uniqueValues = array_unique($singleArray);

You can use this function to convert nested array to one array.
<?php
$a = ["a","b","c",["d","e",["f","g"]],["p","q","r"],["s","t","u"]];
function convert(array $array){
$arr = [];
foreach ($array as $item) {
if(is_array($item)){
$arr = array_merge($arr, convert($item));
}else {
$arr[] = $item;
}
}
return $arr;
}
echo "<pre>";
print_r(convert($a));
echo "</pre>";
?>

Did you try with array_merge?
$array = array(yourArray1(), yourArray2());
$oneLevelArray = array_reduce($array, 'array_merge', array());

Related

Show genre labels from movies

I have a structure like below, there are movies and for each movie the genres and some other info that is not relevant for the question.
The dump:
dd($moviesInformation['movies']['data']);
shows:
^ array:8 [▼
0 => array:22 [▶]
1 => array:22 [▼
"id" => 75
"name" => array:1 [▼
"label" => "movie title"
]
"genres" => array:2 [▼
"data" => array:32 [▼
0 => array:3 [▼
"id" => 64
"name" => array:1 [▼
"label" => "Comedy"
]
"on_request" => 1
]
1 => array:3 [▼
"id" => 65
"name" => array:1 [▼
"label" => "Action"
]
"on_request" => 1
]
2 => array:3 [▶]
3 => array:3 [▶]
4 => array:3 [▶]
5 => array:3 [▶]
]
"meta" => array:1 [▶]
]
...
]
2 => array:22 [▶]
3 => array:22 [▶]
4 => array:22 [▶]
5 => array:22 [▶]
6 => array:22 [▶]
7 => array:22 [▶]
The output should be just the unique values of all movies for genres separated by "," like for example:
Comedy, Action, ...
But I'm not understanding how to properly achieve that. Do you know how it can be?
Your main problem is you are not treating genre like an array in your example and also missing the nested data array.
Collections will make this logic far more readable. This should give you a comma separated strings of genres.
collect(data_get($moviesInformation, 'genres.data', []))
->map(function ($genre) {
return data_get($genre, 'name.label');
})->implode(', ')
Screenshot in Tinkerwell of the output.

Show all genres separated by "," (Array to string conversion)

This dump:
dd($moviesInformation['movies']['data'][1]['genres']);
show all genres for each movie returned like this:
^ array:2 [▼
"data" => array:32 [▼
0 => array:3 [▶]
1 => array:3 [▶]
2 => array:3 [▼
"id" => 446
"genre" => array:1 [▼
"label" => "Comedy"
]
"on_request" => 1
]
3 => array:3 [▶]
4 => array:3 [▶]
5 => array:3 [▶]
And I want to, based on this, have an output with all genres separated by ",", like this:
'result' => [Comedy, Action, etc],
However doing like this:
collect($moviesInformation['movies']['data'])->map(function ($movie) {
return [
'result' => implode(',', $movie['genres'])
];
});
Im getting "Array to string conversion". Do you know how to properly achieve that?

Php look through json array modify a special value and return the array

So I have a JSON array with some content in the array. each item has product id and price, my aim is to loop through this array, and modify the product price and return array with modified product price.
here is the json array snippet in result/
"variation_combination_price" => array:4 [▼
0 => array:10 [▼
"id" => 1
"product_id" => 1364
"cost_price" => 600
"promo_price" => 900
"price" => 900
"combination_array" => array:2 [▼
0 => array:8 [▶]
1 => array:8 [▶]
]
"combination_array_string" => "{"1":"3","2":"4"}"
"quantity" => 10000
"status" => 1
"DOCUMENTATION" => array:2 [▶]
]
1 => array:10 [▶]
2 => array:10 [▶]
3 => array:10 [▶]
So the result above is the original response. As you can see we have 4 items there, the aim is to loop though them, modify the price and return same 4 items.
foreach ($product_data['variation_combination_price'] as
$variation_combination_price){
$variation_combination_price['price'] = 666;
$product_data['variation_combination_price'] =
$variation_combination_price;
}
dd($product_data['variation_combination_price']);
My result should come with the same array where price is modified.
"variation_combination_price" => array:4 [▼
0 => array:10 [▼
"id" => 1
"product_id" => 1364
"cost_price" => 600
"promo_price" => 900
"price" => 666
"combination_array" => array:2 [▼
0 => array:8 [▶]
1 => array:8 [▶]
]
"combination_array_string" => "{"1":"3","2":"4"}"
"quantity" => 10000
"status" => 1
"DOCUMENTATION" => array:2 [▶]
]
1 => array:10 [▶]
2 => array:10 [▶]
3 => array:10 [▶]
You are not looping by reference.
Whatever changes you do in the loop will not change the value of the array.
In order to do so you need to add a &
foreach ($product_data['variation_combination_price'] as &$variation_combination_price){
$variation_combination_price['price'] = 666;
}
Just remember to unset the variable after the loop also.
unset($variation_combination_price);
To make sure you don't change the array data after the loop is done.

Print tree-array in Twig

I'm relatively new in PHP/Symfony, can you help me with this?
I want to create a CSS tree and print an encapsulated array which contains 6 main categories and n-child categories, structured in this way:
Every array-node contains null-key-array with main-category with name(string) --> contains the category name (1. , 1.1, 1.1.1. ) and n-arrays with sub-nodes. The tree-array is structured by name, contains 6 main-category nodes and n sub- and sub-sub-categories for each node. looks like this:
array:6 [▼
1 => array:8 [▼
"" => array:1 [▼
"mydata" => array:3 [▼
"id" => 182
"name" => "1."
"titel" => "Maincategorie"
]
]
1 => array:9 [▼
"" => array:1 [▼
"mydata" => array:3 [▼
"id" => 1
"name" => "1.1."
"titel" => "Maincategorie - subcat1"
]
]
1 => array:1 [▼
"" => array:1 [▼
"mydata" => array:3 [▼
"id" => 2
"name" => "1.1.1."
"titel" => "Mainkategorie - subcat1_2"
]
]
]
2 => array:6 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
]
2 => array:7 [▶]
3 => array:10 [▶]
4 => array:4 [▶]
5 => array:17 [▶]
6 => array:6 [▶]
7 => array:5 [▶]
]
2 => array:5 [▶]
3 => array:7 [▶]
4 => array:5 [▶]
5 => array:7 [▶]
6 => array:6 [▶]
]
My questions are:
Is it possible to call a recursive function in Twig or something like a self-called macro?
Or, do I need a function to set the depth of array first in the controller? In the empty key "", what is my main-category?
What is the best solution to create a tree from an array?
Thanks a lot - Ifgenia.

Display an array in a readable/hierarchical format without mess up the path

I have an array
array:10 [▼
0 => array:3 [▼
"name" => "Xperia Z Ultra"
"img" => "xperia-z-ultra.jpg"
"img_path" => "/Applications/MAMP/htdocs/code/site/public/images/photos/devices/"
]
1 => array:3 [▶]
2 => array:3 [▶]
3 => array:3 [▶]
4 => array:3 [▶]
5 => array:3 [▶]
6 => array:3 [▶]
7 => array:3 [▶]
8 => array:3 [▶]
9 => array:3 [▶]
]
I want to make it pretty so I did this
$pretty_devices = json_encode($devices, JSON_PRETTY_PRINT);
VIEW
<div><pre>{{$pretty_devices}}</pre></div>
I got it display in pretty format as I wanted, but it kind mess up my
img_path.
How do I stop that ?
If you only want to see it nicely try print_r() instead.
echo "<div><pre>".print_r($pretty_devices, true)."</pre></div>"

Categories