I have an array.in which i want to combine value according to same index value of sub array.it's a multi dimensional dynamic array which contain some same and some different indexes like sports footer etc.Please check the below array
Array
(
[0] => Array
(
[0] => Array
(
[Sport] => 15
)
[1] => Array
(
[Sport] => 14
)
[2] => Array
(
[Sport] => 29
)
)
[1] => Array
(
[0] => Array
(
[Surgical] => 11
)
[1] => Array
(
[Surgical] => 12
)
[2] => Array
(
[Surgical] => 13
)
[3] => Array
(
[Footwear] => 10
)
)
)
Below it the array format which i want as an output
Array
(
[0] => Array
(
[0] => 15
[1] => 14
[2] => 29
)
[1] => Array
(
[0] => 11
[1] => 12
[2] => 13
),
[2] => Array(
[0] => 10
)
)
Assuming your input array is called $data, you could do this:
foreach ($data as $row) {
foreach ($row as $pair) {
foreach ($pair as $key => $value) {
$result[$key][] = $value;
}
}
}
This will provide the $result as follows:
[
"Sport" => [15, 14, 29],
"Surgical" => [11, 12, 13],
"Footwear" => [10]
]
If you really want to throw away the "labels" and just keep the values, then add the following conversion at the end:
$result = array_values($result);
Which will give you the desired result:
[
[15, 14, 29],
[11, 12, 13],
[10]
]
... but that would seem less useful to me.
Related
I have an array,
Array (
[0] => Array ( [did] => 12 [uid] => 21 )
[1] => Array ( [did] => 10 [uid] => 12 )
[2] => Array ( [did] => 9 [uid] => 11 )
[3] => Array ( [did] => 10 [uid] => 11 )
[4] => Array ( [did] => 12 [uid] => 11 )
)
where I want all the uids who have the same did value to be connected with each other. For example- for did=12, uids- 21 & 11 should be connected with each other. I am struggling to figure out how to do this in PHP and MySQL? Please note that array gets generated dynamically using query and can have many rows.
You could loop over the array and use the did as the key. If the key already exists, add to the array, else create a new entry using the key:
$items = [
["did" => 12, "uuid" => 21],
["did" => 10, "uuid" => 12],
["did" => 9, "uuid" => 11],
["did" => 10, "uuid" => 11],
["did" => 12, "uuid" => 11],
];
$result = [];
foreach ($items as $i) {
array_key_exists($i["did"], $result) ? $result[$i["did"]][] = $i["uuid"] : $result[$i["did"]] = [$i["uuid"]];
}
print_r($result);
Result:
Array
(
[12] => Array
(
[0] => 21
[1] => 11
)
[10] => Array
(
[0] => 12
[1] => 11
)
[9] => Array
(
[0] => 11
)
)
Php demo
I have an array of arrays, and I want to filter that array by multiple key values, and group the arrays with matching keys if there are any. Example array:
Array
(
[0] => Array
(
[id] => 1
[value] => 11
[quantity] => 14
)
[1] => Array
(
[id] => 2
[value] => 11
[quantity] => 14
)
[2] => Array
(
[id] => 3
[value] => 22
[quantity] => 14
)
[3] => Array
(
[id] => 4
[value] => 22
[quantity] => 14
)
[4] => Array
(
[id] => 5
[value] => 23
[quantity] => 15
)
)
and let's say I want the arrays with matching value and quantity to be grouped in a new array
The desired output would be something like this:
Array
(
[11] => Array
(
[0] => Array
(
[id] => 1
[value] => 11
[quantity] => 14
)
[1] => Array
(
[id] => 2
[value] => 11
[quantity] => 14
)
)
[22] => Array
(
[0] => Array
(
[id] => 3
[value] => 22
[quantity] => 14
)
[1] => Array
(
[id] => 4
[value] => 22
[quantity] => 14
)
)
[23] => Array
(
[0] => Array
(
[id] => 5
[value] => 23
[quantity] => 15
)
)
)
I'm clueless on how to achieve this.
A simple foreach loop over your array to ceate a new array will suffice for this purpose
$new_arr = [];
foreach ($inArray as $arr ) {
$new_arr[$arr['value']][] = $arr;
}
// unset the original array if you are finished with it
// in case it is large and you could make better use
// of the memory for something else
unset($inArray);
If you want to group by the values of multiple keys of the inner arrays, you can join those values together to form the key in your result array.
foreach ($array as $item) {
// combine value and quantity, for example
$key = $item['value'] . '|' . $item['quantity'];
$result[$key][] = $item;
}
just pass an array References and a sort key
function sortBy(&$arr,$by){
$result=array();
foreach($arr as $value){
if(isset($value[$by]))
$result[$value[$by]][]=$value;
}
return $result;
}
Examples
$sorted=sortBy($yourArray,'value'); //by value
$sorted=sortBy($yourArray,'id'); //by Idx
$sorted=sortBy($yourArray,'quantity'); //quantity
You want to group the array keys passing, if I understood correctly.
I usually use the laravel collection library, because it's provided out of the box.
ALthoug, here's my contribution.
Let's try:
function groupArray( $array, $key, $remove = null )
{
$result = array();
foreach (array_unique(array_column($array, $key)) as $value) {
$result[$value] = array_map(function ( $item ) use ( $remove ) {
unset($item[$remove]);
return $item;
}, array_filter($array, function ( $item ) use ( $value, $key ) {
return $item[$key] === $value;
}));
}
return $result;
}
The above function does the job, first we get all the selected key values using the array_column function. THen we do a foreach in the array to filter the array data using the provided key and finally we remove the selected key, if necessary (just because the selected key will be the grouped array keys).
Usage:
$sample = array(
[
'id' => 1,
'value' => 11,
'quantity' => 14
],
[
'id' => 2,
'value' => 11,
'quantity' => 14
],
[
'id' => 3,
'value' => 22,
'quantity' => 14
],
[
'id' => 4,
'value' => 22,
'quantity' => 14
],
[
'id' => 5,
'value' => 23,
'quantity' => 14
],
);
$a = groupArray($sample, 'value', 'value');
$b = groupArray($sample, 'value');
$c = groupArray($sample, 'quantity');
I have got 2 arrays(One single and one multidimensional).
Single array "A" looks like
[questionid] => Array
(
[0] => 12
[1] => 13
[2] => 55
[3] => 15
[4] => 16
)
Multidimensional array "B" looks like
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 15
[answer] =>
)
[3] => Array
(
[quid] => 16
[answer] =>
)
[4] => Array
(
[quid] => 55
[answer] =>
)
)
Now I want the array B (quid) values to be rearranged depending upon the values from array A. So in array B the value of quid last element(55) is at the very end whereas in array A it is in 3rd position.
I want the array B look like this
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 55
[answer] =>
)
[3] => Array
(
[quid] => 15
[answer] =>
)
[4] => Array
(
[quid] => 16
[answer] =>
)
)
The code for multidimensional array is
$ansid = array
(
array
(
"quid" => 12,
"answer" => "AAA"
),
array
(
"quid" => 13,
"answer" => "neighbour"
),
array
(
"quid" => 15,
"answer" =>""
),
array
(
"quid" => 16,
"answer" =>""
),
array
(
"quid" => 55,
"answer" =>""
)
);
Not using array_walk() as to be mor demonstrative, you could just
$newB=array()
foreach ($arrayB as $b) $newB[$b['quid']]=$b;
$newA=array()
foreach ($arrayA as $k=>$v) $newA[$k]=$newB[$v]
//$newA has the required structure
With the user sort function:
$single_array = ...; // order by the index of this array
$mult_dim_array = ...; // to be ordered by the 'quid' value of the elements
function my_comp($a, $b) {
return array_search($a['quid'], $single_array ) - array_search($b['quid'], $single_array );
}
usort($mult_dim_array, "my_comp");
This will get the index on your first array to determine which element goes first or later. The function reads $single_array as a global variable (defined outside the function).
Documentation at http://php.net/manual/en/function.usort.php
I am trying to subtract parts of one nested array from another, but I'm having difficulty specifying the parts that I want to subtract as both values are numbers.
My arrays are, for example:
Array ( [0] => Array ( [id] => 43 [quantity] => 4 ) )
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
And after the subtraction I want the Result to be:
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
I'm using the following code to perform the subtraction, but I can't stop it from subtracting the id from itself:
foreach(array_keys($arrayA) as $id)
{
foreach(array_keys($arrayA[$id]) as $type)
{
$newArray[$id][$type] = $arrayA[$id][$type] - $arrayB[$id][$type];
}
}
print_r($newArray);
Could someone please tell me how I can just effect the [quantity] part of the array, without changing the [id]? With the code as it is I get:
Array ( [0] => Array ( [id] => 0 [quantity] => 2 ) )
Thanks in advance.
$ar1 = array(0 => array('id' => 43, 'quantity' => 4));
$ar2 = array(0 => array('id' => 43, 'quantity' => 2));
$new_array = array();
foreach($ar1 as $key => $value)
{
$new_array[$key] = array('id' => $value['id'], 'quantity' => ($value['quantity'] - $ar2[$key]['quantity']));
}
Array
(
[0] => Array
(
[id] => 43
[quantity] => 2
)
)
I've got the following array as $main_array .Wanted to sum up all the elements in the sub array such as [0]=>6, [1]=>11, [2]=>15.
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 2
[1] => 4
[2] => 5
)
[2] => Array
(
[0] => 8
[1] => 4
[2] => 3
)
)
Tried the following code.
foreach ($main_array as $key => $value)
$main_array[$key] = Array('1'=>array_sum($value));
print_r($main_array);
But the array structure I got was,
Array
(
[0] => Array
(
[1] => 6
)
[1] => Array
(
[1] => 11
)
[2] => Array
(
[1] => 15
)
)
I'm expecting the array structure as follows.
Array
(
[0] => 6
[1] => 11
[2] => 15
)
Thanks in advance!
When you're calling Array function you're explicitly making an array so you have to remove this from Array('1'=>array_sum($value));
This is how your code should look like
foreach ($main_array as $key => $value)
$main_array[$key] = array_sum($value);
Try this:
foreach ($main_array as $key => $value)
$main_array[$key] = array_sum($value);
That is, place the sum directly in the top level array.
Call array_sum() on every row in your input array. array_map() makes this operation expressive, concise, and doesn't require any new variables to be declared.
Code: (Demo)
$array = [
[1, 2, 3],
[2, 4, 5],
[8, 4, 3],
];
var_export(array_map('array_sum', $array));
Output:
array (
0 => 6,
1 => 11,
2 => 15,
)