I have an array and I want to add a dimension. I want to group it using the position column in the array
$view_rows = [];
Array
(
[row] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
[2] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
After adding the dimension (group by location), it should look like this:
Array
(
[rows] => Array
(
[1] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
)
[2] => Array
(
[0] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
)
Is there a PHP function to do this? If not, I will use the good old looping method.
I did some research here ( Add Dimension in PHP array ), and I believe it can be achieve using array_map.
I've tried it, with no success (I'm close)
$new_array = array_map(function($x) {
return [$x[0]['position'] => $x];
}, $view_rows);
Since you need to change the structure of the array, you can't use array_map, but you can use array_reduce to get the result you want:
$data['rows'] = array_reduce($data['rows'], function ($c, $a) {
$c[$a['position']][] = $a;
return $c;
}, array());
Output:
Array
(
[rows] => Array
(
[1] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
)
[2] => Array
(
[0] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
)
Demo on 3v4l.org
#Nick answer works but the same thing can be achieved using a simple foreach loop easily so everyone can understand it.
$result = [];
foreach($data['rows'] as $row) {
$result[$row['position']][] = $row;
}
Related
I need help modifying arrays to reach a desired structure. I am sorry the example structure maybe big, but i wanted to show the structure better.
I have a target Array like below that I need to generate
[bf_1040242] => Array
(
[326] => Just some Information. Additional Text
[17565] => Array
(
[0] => 2
[1] => 1
[2] => 3
)
[other] => Array
(
[17565] => Testing
[28623] =>
[42284] => Something Else
)
[597] => 1
[327] => This is some text
[328] => asdasd
[11880] => wwwww
[329] => xxxxx
[28622] => 2
[42283] => 1
[42284] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
The data for me to generate this comes in a different format where the structure of these values are in string.
Array
(
[0] => Array
(
[name] => bf_1040242[326]
[value] => Just some Information. Additional Text
)
[1] => Array
(
[name] => bf_1040242[17565][]
[value] => 2
)
[2] => Array
(
[name] => bf_1040242[17565][]
[value] => 1
)
[3] => Array
(
[name] => bf_1040242[17565][]
[value] => 3
)
[4] => Array
(
[name] => bf_1040242[other][17565]
[value] => Testing
)
[5] => Array
(
[name] => bf_1040242[597]
[value] => 1
)
[6] => Array
(
[name] => bf_1040242[327]
[value] => This is some text
)
[7] => Array
(
[name] => bf_1040242[328]
[value] => asdasd
)
[8] => Array
(
[name] => bf_1040242[11880]
[value] => wwwww
)
[9] => Array
(
[name] => bf_1040242[329]
[value] => xxxxx
)
[10] => Array
(
[name] => bf_1040242[28622]
[value] => 2
)
[11] => Array
(
[name] => bf_1040242[other][28623]
[value] =>
)
[12] => Array
(
[name] => bf_1040242[42283]
[value] => 1
)
[13] => Array
(
[name] => bf_1040242[42284][]
[value] => 2
)
[14] => Array
(
[name] => bf_1040242[42284][]
[value] => 3
)
[15] => Array
(
[name] => bf_1040242[42284][]
[value] => 4
)
[16] => Array
(
[name] => bf_1040242[other][42284]
[value] => Something Else
)
)
In the above array, the key 'name' represents the structure of individual array in string format and the 'value' holds the value that the array structure needs to be updated to.
This is have managed to get done by using
$extra = []
foreach ($mainArray as $key => $value)
{
parse_str($value['name'], $tempArr);
$m = $this->multiArr($k, $value);
array_push($extra, $m);
}
protected function multiArr($k, $val)
{
foreach($k as $key => $value)
{
if(is_array($value) ) $k[$key] = $this->multiArr($k[$key], $val);
if(!is_array($value))
{
$k[$key] = (string) $val['value'];
}
}
return $k;
}
$m holds individual array values like
Array
(
[bf_1040242] => Array
(
[other] => Array
(
[42284] => Something Else
)
)
)
Using these individual array values i want to create the the Main required array so i used
array_push($extra, $m);
but this adds each $m array below each other instead of updating the way expected above.
Below is the way it is coming in my end.
Array
(
[0] => Array
(
[bf_1040242] => Array
(
[326] => Just some Information. Additional Text
)
)
[1] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 2
)
)
)
[2] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 1
)
)
)
[3] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 3
)
)
)
[4] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[17565] => Testing
)
)
)
[5] => Array
(
[bf_1040242] => Array
(
[597] => 1
)
)
[6] => Array
(
[bf_1040242] => Array
(
[327] => This is some text
)
)
[7] => Array
(
[bf_1040242] => Array
(
[328] => asdasd
)
)
[8] => Array
(
[bf_1040242] => Array
(
[11880] => wwwww
)
)
[9] => Array
(
[bf_1040242] => Array
(
[329] => xxxxx
)
)
[10] => Array
(
[bf_1040242] => Array
(
[28622] => 2
)
)
[11] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[28623] =>
)
)
)
[12] => Array
(
[bf_1040242] => Array
(
[42283] => 1
)
)
[13] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 2
)
)
)
[14] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 3
)
)
)
[15] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 4
)
)
)
[16] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[42284] => Something Else
)
)
)
)
I am not sure if array_push is what i need to use, or something else.
Also, is there a cleaner or a more efficient way to this.
Any help is much appreciated, Thanks
Normally you'd use array_merge_recursive for this, but it wont preserve named keys, so I wrote a little array_join function to make life easier :)
function array_join($value, &$result) {
if (!is_array($value)) {
$result = $value;
return;
}
foreach ($value as $k => $v) {
array_join($v, $result[$k]);
}
}
$result = [];
foreach ($mainArray as $entry)
{
parse_str($entry['name'] . '=' . $entry['value'], $nameArray);
array_join($nameArray, $result);
}
We simply call array_join recursively until the value is no longer an array. Notice the & in &$result of array_join. We pass the reference of our current position in the array.
To make full use of parse_str I've added the value aswell by appending =<value>
Working example.
Pro tip for your next post. Use var_export to give us an array, which can be copy pasted as code :)
EDIT
More simple solution with escaped values for "evil" input.
foreach ($mainArray as $entry)
{
$stringVarArray[] = $entry['name'] . '=' . urlencode($entry['value']);
}
parse_str(implode('&', $stringVarArray), $result);
var_dump($result);
Working example.
Array
(
[3M] => Array
(
[0] => Array
(
[name] => 3M
[price] => 158.15
)
[440] => Array
(
[name] => 3M
[price] => 156.69
)
)
[AO Smith] => Array
(
[1] => Array
(
[name] => AO Smith
[price] => 47.29
)
[441] => Array
(
[name] => AO Smith
[price] => 47.19
)
)
So I have an Array that is above^^^. I would like to get it into a condensed array format. I need a function that loops through the above and outputs it in the format below.
Array
(
[3M] => Array
(
[price1] => 158.15
[price2] => 156.69
)
[AO Smith] => Array
(
[price1] => 47.29
[price2] => 47.19
)
)
Above is how I would like the data oriented.
Thanks for the help.
What you'll find is the format you want is not good and not as usable or flexible. This however will give you a better format. name and price are descriptive, price1 and price2 are no different than 0 and 1:
foreach($array as $key => $values) {
$result[$key] = array_column($values, 'price');
}
Yields:
Array
(
[3M] => Array
(
[0] => 158.15
[1] => 156.69
)
[AO Smith] => Array
(
[0] => 47.29
[1] => 47.19
)
)
Hi I need to Sort the multi dimensional array by its value My array is like this I need to group the array by its position to create a new array sorting is just confusing How do i create a sorted array from this
Array
(
[prodata] => Array
(
[4] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
[3] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
[richtext] => Array
(
[1] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
)
I this array i need to sort by it position and create a new array,
Goal
Array
(
[0]=>Array(
[richtext] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
[1]=>Array(
[prodata] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
)
[2]=>Array(
[prodata] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
)
How do I achieve this help
You can flat the multi dimension array first, then sort it. Demo
$flat_array = [];
foreach($multi_dimension_array as $k => $array){
foreach($array as $value){
$flat_array[] = array(
$k => $value
);
}
}
usort($flat_array,function($a,$b){return current($a)["position"] - current($b)["position"];});
print_r($flat_array);
Though this code works, But I suggest restruct the data source for better data format. Which can make it a easy work
I have two arrays and I want to combine them together
1) first look like this:
[11] => Array
(
[id] => 11
[name] => test
)
[12] => Array
(
[id] => 12
[name] => test1
)
2) second array look like this:
[0] => Array
(
[offer_id] => 11
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[1] => Array
(
[offer_id] => 12
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
I want this result. How is it possible can any one provide solution for same?
[11] => Array
(
[id] => 11
[name] => test
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[12] => Array
(
[id] => 12
[name] => test
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
Thank you for the help!
Try this:
foreach ($array1 as &$arr1) {
$offer_id = $arr1['id']; // Search for this offer_id in array 2
$match = array_filter($array2, function($v) use ($offer_id){
return $v['offer_id'] == $offer_id; // Return matching offer id
});
$arr1['countries'] = current($match)['countries']; // Assign matched country to array
}
I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;