I have the below multi dimensional array i want to make a new array from taking each array's first element and make one array than after second element of each array and make a new array, i did tried for loop and foreach but i can't able to make it help me to solve this please, Thank you in advance.
the below is my array
Array
(
[0] => Array
(
[0] => Array
(
[y] => 40
[label] => SCIENCE
)
[1] => Array
(
[y] => 100
[label] => SCIENCE
)
[2] => Array
(
[y] => 89
[label] => SCIENCE
)
)
[1] => Array
(
[0] => Array
(
[y] => 72
[label] => MATHEMATICS
)
[1] => Array
(
[y] => 20
[label] => MATHEMATICS
)
[2] => Array
(
[y] => 70
[label] => MATHEMATICS
)
[3] => Array
(
[y] => 85
[label] => MATHEMATICS
)
)
[2] => Array
(
[0] => Array
(
[y] => 66
[label] => ENGLISH
)
)
)
I want my first new array be like
[0] => Array
(
[0] => Array
(
[y] => 40
[label] => SCIENCE
)
[1] => Array
(
[y] => 72
[label] => MATHEMATICS
)
[2] => Array
(
[y] => 66
[label] => ENGLISH
)
)
My second new array be like
[1] => Array
(
[0] => Array
(
[y] => 100
[label] => SCIENCE
)
[1] => Array
(
[y] => 20
[label] => MATHEMATICS
)
)
Foreach loop the array with $key and you will be able to get the other items easily with array_column.
foreach($arr[0] as $key => $sub){
$new[$key] = array_column($arr, $key);
}
Now $new will have three items (the same as first subarray).
To fix what you asked for I have to loop and find the max count of the arrays.
$max =0;
foreach($arr as $sub){
$max = max($max, count($sub));
}
for($i=0;$i<$max;$i++){
$new[$i] = array_column($arr, $i);
}
You can transpose an array, remove empty entries and take needed row. In the case your result will be in $res[0] and $res[1] arrays
$res = array_map('array_filter', array_map(null, ...$a));
demo
Related
I have an array like this what i want is to sub array converts into unique key sub array with multiple unique keys. I have tried array_merge_recursive function which is not working because i have multiple arrays in loop and the function works with 2 or 3 arrays.
Array
(
[0] => Array
(
[fromWho] => abc#email.se
)
[1] => Array
(
[tid] => 2013-05-05 23:35
)
[2] => Array
(
[answer] => Company
)
[3] => Array
(
[name] => message
)
[4] => Array
(
[label] => Message
)
[5] => Array
(
[fromWho] => bcd#email.se
)
[6] => Array
(
[tid] => 2013-05-05 23:35
)
[7] => Array
(
[answer] => Star Company
)
[8] => Array
(
[name] => name
)
[9] => Array
(
[label] => Name
)
)
I want the array like this array keys order doesn't matter:
Array
(
[0] => Array
(
[label] => Message
[name] => message
[answer] => Company
[tid] => 2013-05-05 23:35
[fromWho] => abc#email.se
)
[0] => Array
(
[label] => Name
[name] => name
[answer] => Star Company
[tid] => 2013-05-05 23:35
[fromWho] => bcd#email.se
)
)
Use array_chunk() to group the array into chunks of 5 elements. Then you can convert each chunk into an associative array.
$chunks = array_chunk($data, 5);
$result = array_map(function($chunk) {
$arr = [];
foreach ($chunk as $entry) {
foreach ($entry as $key => $val) {
$arr[$key] = $val;
}
}
return $arr;
}, $chunks);
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 an array $result as such:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
and an array $items as such:
[0] => Array (
[itemid] => 2
[name] => B
)
[1] => Array (
[itemid] => 4
[name] => D
)
How do I remove all items from the $result array, that occur in the $items array? In this case, the $result would become:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
)
Since the question is mostly code, here's some extra characters to make StackOverflow accept the question.
I think this is what you want. (Not tested yet)
<?php
foreach ($result as $key => $array) {
$result[$key] = array_diff($array, $items);
}
print_r($result);
I have an array, seen below. My desired output is to group by store, and then concatenate (I think?) all of the related quantities and denominations into those arrays. I've shown what the desired output would be.
CURRENT ARRAY
Array
(
[denomination] => Array
(
[0] => 25
[1] => 50
[2] => 100
[3] => 200
)
[quantity] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 4
)
[store] => Array
(
[0] => candy store
[1] => candy store
[2] => book store
[3] => candy store
)
)
DESIRED OUTPUT
Array
(
[candy store] => Array
(
[0] => Array
(
[denomination] => Array
(
[0] => 25
[1] => 50
[2] => 200
)
)
[1] => Array
(
[quantity] => Array
(
[0] => 1
[1] => 1
[2] => 4
)
)
)
[book store] => Array
(
[0] => Array
(
[denomination] => Array
(
[0] => 100
)
)
[1] => Array
(
[quantity] => Array
(
[0] => 2
)
)
)
)
$result = array();
foreach ($array['store'] as $index => $type) {
$result[$type]['denomination'][] = $array['demoniation'][$index];
$result[$type]['quantity'][] = $array['quantity'][$index];
}
This is not exactly, what you specified as "desired output", but I don't see a reason, why one should put the denomination- and quantity-arrays into additional arrays.
However, if this has any reason, you can get it similar
$result = array();
foreach ($array['store'] as $index => $type) {
$result[$type][0]['denomination'][] = $array['demoniation'][$index];
$result[$type][1]['quantity'][] = $array['quantity'][$index];
}
I have this Array
Array (
[0] => Array (
[0] => Array ( [value] => Cooking, [occurence] => 1 )
[1] => Array ( [value] => Music, [occurence] => 1 )
[2] => Array ( [value] => Football,Cooking, [occurence] => 1 )
[3] => Array ( [value] => Travel, [occurence] => 1 )
[4] => Array ( [value] => Cooking,Reading, [occurence] => 2 )
[5] => Array ( [value] => Football,Travel, [occurence] => 1 )
[6] => Array ( [value] => Football, [occurence] => 1 )
[7] => Array ( [value] => Music,Cooking, [occurence] => 1 )
[8] => Array ( [value] => Reading,Travel, [occurence] => 1 )
)
)
The [2], [4], [5], [7] and [8] have 2 values for the key [value].
What I want to do is to split the 2 values of these keys in different keys.
The new values should not go to new Arrays, but they will be added to the similar existing Arrays.
For example, if I break the [2] (Football,Cooking) the result will be that the occurence of [6] (Football) will be incremented by 1 and the [occurence] of [0] (Cooking) will be incremented by 1 also.
Thank you !
Yann
$newdata = array()
foreach($array as $data) { // $array being the inner array with the 9 elements
$keys = explode(',', $data['value']);
foreach ($keys as $subkey) {
$newdata[$subkey]++;
}
}
which would give you something like
$newdata = array(
'Cooking' => 4,
'Football' => 3
etc...
);
Not sure how you want your structure to look afterwards, but at least this'll do the inventory for you.