How can I merge these two array together?
Array
(
[0] => Array
(
[id] => 5
[cnt] => 14
)
[1] => Array
(
[id] => 8
[cnt] => 2
)
)
Array
(
[0] => Array
(
[id] => 8
[binding] => hardcover
)
[1] => Array
(
[id] => 5
[binding] => softcover
)
)
The expected result is:
Array
(
[0] => Array
(
[id] => 5
[binding] => softcover
[cnt] => 14
)
[1] => Array
(
[id] => 8
[binding] => hardcover
[cnt] => 2
)
)
The merge of these two array should happen on the [id] value and not on any sort of the array. How can I do this with php in a fast way?
$output = array();
$arrayAB = array_merge($arrayA, $arrayB);
foreach ( $arrayAB as $value ) {
$id = $value['id'];
if ( !isset($output[$id]) ) {
$output[$id] = array();
}
$output[$id] = array_merge($output[$id], $value);
}
var_dump($output);
Optionally if you want to reset output's keys, just do:
$output = array_values($output);
Merge the input arrays, then
Loop the rows and merge associative row data with the array union operator (+). The array union operator should only be used with associative, non-numeric keyed arrays.
The first time that a given id is encountered, there will be no "group" in the result array yet. To avoid a Warning generated by trying to merge row data with an undeclared variable, use the null coalescing operator (??) to fallback to an empty array.
The snippet below will be highly efficient because it makes no iterated function calls.
If you do not want the first level keys in the result array, then call array_values() to re-index the array.
Code: (Demo)
$a1 = [
['id' => 5, 'cnt' => 14],
['id' => 8, 'cnt' => 2],
];
$a2 = [
['id' => 8, 'binding' => 'hardcover'],
['id' => 5, 'binding' => 'softcover'],
];
$result = [];
foreach (array_merge($a1, $a2) as $row) {
$result[$row['id']] = ($result[$row['id']] ?? []) + $row;
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'id' => 5,
'cnt' => 14,
'binding' => 'softcover',
),
1 =>
array (
'id' => 8,
'cnt' => 2,
'binding' => 'hardcover',
),
)
Related
I got this array, comes from database. The order of values is changeable by the users
Array
(
[0] => 14
[1] => 15
[2] => 9
[3] => 13
[4] => 12
...
[n] => n
)
There is another array, which will be extracted to browser:
Array
(
[0] => Array
(
[id] => 1
...
[nr] => 9
...
)
[1] => Array
(
[id] => 2
...
[nr] => 12
...
)
[2] => Array
(
[id] => 3
...
[nr] => 15
...
)
[n] => Array
)
I need to find a way to sort the this second array by nr (not id) and the nr order comes from the first array.
Any ideas?
You can flip the first array in order to know what's the order of the id's:
$order = array_flip($order);
Then you simply use usort to sort the array based on the earlier flipped array:
usort($array, static function(array $a, array $b) use ($order) {
return $order[$a['nr']] <=> $order[$b['nr']];
});
You can just map your values to keys, and use that map to re-order.
<?php
$order =
[
2,
5,
1
];
$items =
[
['o' => 1],
['o' => 5],
['o' => 2],
];
foreach($items as $k => $v) {
$map[$v['o']] = $k;
}
var_export($map);
foreach($order as $o) {
$k = $map[$o];
$result[$k] = $items[$k];
}
var_export($result);
Output:
array (
1 => 0,
5 => 1,
2 => 2,
)array (
2 =>
array (
'o' => 2,
),
1 =>
array (
'o' => 5,
),
0 =>
array (
'o' => 1,
),
)
Assuming there's a one-one mapping.
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 try to get the differences between two arrays
I have one big array with subarray and a small simple array.
I want to get the difference from the big array.
I use this to get the differences between 2 arrays, but working with subarray is someting else.
$array3 = array_diff($bigArray, $smallArray);
$smallArray = Array
(
[0] => 2 (how i compare this values)
[1] => 3 (how i compare this values)
)
$bigArray = Array
(
[0] => Array
(
[g_id] => 2 (with this value)
[g_nume] => Arad I Betel
)
[1] => Array
(
[g_id] => 3 (with this value)
[g_nume] => Arad IV Agape
)
[2] => Array
(
[g_id] => 4 (with this value)
[g_nume] => Frumuseni
)
[3] => Array
(
[g_id] => 7 (with this value)
[g_nume] => Cuvin
)
)
And the result to be like this:
Array
(
[0] => Array
(
[g_id] => 4 (with this value)
[g_nume] => Frumuseni
)
[1] => Array
(
[g_id] => 7 (with this value)
[g_nume] => Cuvin
)
)
<?php
$disard_ids = [
'2', '3'
];
$items =
[
[
'id'=>'1'
],
[
'id'=>'2'
],
[
'id'=>'3'
],
[
'id'=>'4'
]
];
foreach ($items as $item) {
if(array_search($item['id'], $discard_ids) === false) {
$result[] = $item;
}
}
var_export($result);
Output:
array (
0 =>
array (
'id' => '1',
),
1 =>
array (
'id' => '4',
),
)
I have 2 Multidimensional arrays as follow:
Array1:
Array (
[0] => Array (
[0] => 2D Design
[1] => 3D Design & Modeling)
[1] => Array ( [0] => Android Developer
[1] => Artificial Intelligence
[2] => Web Developer)
)
Array2:
Array (
[0] => Array (
[0] => 5
[1] => 10)
[1] => Array ( [0] => 2
[1] => 4
[2] => 6)
)
I want to combine the above 2 arrays as key and value as below.
Array (
[0] => Array (
[2D Design] => 5
[3D Design & Modeling] => 10 )
[1] => Array (
[Android Developer] => 2
[Artificial Intelligence] => 4
[Web Developer] => 6 )
)
Please help me to do this. Answers will be appreciated.
use array_combine() function creates an array by using the elements from one "keys" array and one "values" array.
Note: Both arrays must have equal number of elements!
First parameter array taken as key of new array and second parameter taken as value new array .
$new_array=array();
for($i=0;$i<count($arr1);$i++)
{
$new_array[$i]=array_combine($arr1[$i],$arr2[$i]);
}
print_r($new_array);
Output :
Array
(
[0] => Array
(
[2D Design] => 5
[3D Design & Modeling] => 10
)
[1] => Array
(
[Android Developer] => 2
[Artificial Intelligence] => 4
[Web Developer] => 6
)
)
This will work,
$arr1 = array(
0 => array(
0 => "2D Design",
1 => "3D Design & Modeling"),
1 => array(0 => "Android Developer",
1 => "Artificial Intelligence",
2 => "Web Developer",
),
);
$arr2 = array(
0 => array(
0 => 5,
1 => 10,
),
1 => array(0 => 2,
1 => 4,
2 => 6,
),
);
$temp = [];
foreach ($arr1 as $k => &$v) {
foreach ($v as $k1 => &$v1) {
$temp[$k][$v1] = $arr2[$k][$k1];
}
}
print_r($temp);
I have fetched values of first array arr1 as key to temp variable and map it with values of arr2as value to temp array.
This code will work even if index i.e. 0,1,2,3 can be anything.
Here is working code.
Simply make mapped calls of array_combine(). So long as the same positioned rows have the same number of elements in them, everything will work perfectly.
Code: (Demo)
$keys =[
['2D Design', '3D Design & Modeling'],
['Android Developer', 'Artificial Intelligence', 'Web Developer']
];
$values = [
[5, 10],
[2, 4, 6]
];
var_export(
array_map('array_combine', $keys, $values)
);
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have three arrays as follows:
Array
(
[1000] => Item 0
[1001] => Item 1
[1002] => Item 2
)
Array
(
[1000] => £35.00
[1001] => £60.00
[1002] => £24.00
)
Array
(
[1000] => 1
[1001] => 2
[1002] => 3
)
I need to merge these three arrays preserving the keys as follows:
Array
(
[1000] => Array
(
[0] => Item 0
[1] => £35.00
[2] => 1
)
[1001] => Array
(
[0] => Item 1
[1] => £60.00
[2] => 2
)
[1002] => Array
(
[0] => Item 2
[1] => £24.00
[2] => 3
)
)
array_map(null, array1, array2, array3) solves it to some level but doesn't preserves the keys. How can it be done?
You could wrap your array_map together with an array_keys() against your original array within an array_combine()
$array1 = array(
1000 => 'Item 0',
1001 => 'Item 1',
1002 => 'Item 2',
);
$array2 = array(
1000 => '£35.00',
1001 => '£60.00',
1002 => '£24.00',
);
$array3 = array(
1000 => 1,
1001 => 2,
1002 => 3,
);
$result = array_combine(
array_keys($array1),
array_map(null, $array1, $array2, $array3)
);
var_dump($result);
Try this code, save one array keys in another array and fill keys after merged array
$array1_keys = array_keys($array1);
$mapped_array = array_map(null, $array1, $array2, $array3);
//now assign original keys
$merged_final_array = array_fill_keys($array1_keys, $mapped_array);
DEMO
I hope this code will help you:
$one =Array( "1000" => "£35.00","1001" => "£60.00","1002" => "£24.00","1003" => "£36.00","1004" => "£80.00","1005" => "£24.00");
$two = array("1000"=>"1","1001"=>"2","1002"=>"3","1003"=>"4","1004"=>"5","1005"=>"6");
$response = array();
foreach(array_keys($one) as $key =>$val)
{
$response[$val] = array(0=>"item ".$key,1=>$one[$val],2=>$two[$val]);
}
echo "<pre>";print_R($response);