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);
Related
I have two multidimensional arrays and want to join them with all the items.
it can be re-index again wont be any issue.
I have create these array in a foreach loop and use this for batch insert query.
here is the array, I want to add both of them
Array
(
[1] => Array
(
[0] => Array
(
[question_def_id] => 1
[description] => 3
[description_hindi] => 3
[correct] => 0
)
[1] => Array
(
[question_def_id] => 1
[description] => 4
[description_hindi] => 4
[correct] => 1
)
[2] => Array
(
[question_def_id] => 1
[description] => 5
[description_hindi] => 5
[correct] => 0
)
[3] => Array
(
[question_def_id] => 1
[description] => 6
[description_hindi] => 6
[correct] => 0
)
)
)
Join two multidimensional array
$test_array1 = array(array('question_def_id'=>1,'description'=>2), array('question_def_id'=>3,'description'=>4));
$test_array2 = array(array('question_def_id'=>5,'description'=>6), array('question_def_id'=>7,'description'=>8));
$C = array_merge($test_array1, $test_array2);
print_r($C);
You can use PHP array_merge_recursive function to merge both arrays into one.
See here
E.g
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
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 is my old array.
$oldarray = Array
(
[0] => http://test.to/getac/l4p0y6ziqt9h
[mock] => stdClass Object
(
[0] => http://test.to/getae/vidzichawal1
[1] => http://test.to/getae/vidzi6
[4] => http://test.to/getae/1x5fbr9t64xn
[2] => http://test.to/getae/vidzi7
)
)
which i want to merge with this new array:
$newarray = Array
(
[mock] => Array
(
[0] => http://test.to/getae/vidzichawal2
)
)
I am merging array by array_merge_recursive($oldarray, $newarray);
And the Result is this:
Array
(
[0] => http://test.to/getac/l4p0y6ziqt9h
[mock] => Array
(
[0] => http://test.to/getae/vidzi5
[1] => http://test.to/getae/vidzi6
[4] => http://test.to/getae/1x5fbr9t64xn
[2] => http://test.to/getae/vidzi7
[0] => http://test.to/getae/vidzichawal1
)
);
All things is working good but there is one problem you can see in result there double 0 key when i am using this link in loop only 1 link retrive of 0 i want to set this keys automatically like 0 1 2 3 4 5 6 and go on after merging.
I hope you understand what i want thanks
Use array_merge()
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
REF.
Note You can't have duplicate keys!
UPDATE
Example using array_merge_recursive()
<?php
$oldarray = array('http://test.to/getac/l4p0y6ziqt9h', 'mock' => array('http://test.to/getae/vidzichawal1', 'http://test.to/getae/vidzi6', 'http://test.to/getae/1x5fbr9t64xn', 'http://test.to/getae/vidzi7'));
$newarray = array('mock' => array('http://test.to/getae/vidzichawal2'));
$result = array_merge_recursive($oldarray, $newarray);
var_dump($result);
?>
OUTPUT
array (size=2)
0 => string 'http://test.to/getac/l4p0y6ziqt9h' (length=33)
'mock' =>
array (size=5)
0 => string 'http://test.to/getae/vidzichawal1' (length=33)
1 => string 'http://test.to/getae/vidzi6' (length=27)
2 => string 'http://test.to/getae/1x5fbr9t64xn' (length=33)
3 => string 'http://test.to/getae/vidzi7' (length=27)
4 => string 'http://test.to/getae/vidzichawal2' (length=32)
I have a multidimensional array called $test:
Array (
[First item] => Array (
[screen] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 2
)
[Second Item] => Array (
[screen] => 3
[1] => 3
[2] => 3
[3] => 3
[4] => 3
)
)
I am trying to get the keys: screen, 1, 2, 3, and 4.
They are the same for First item and Second item. Do you know how I can loop through this array to get those values? So, basically getting the keys for the first array in my multidimensional array. Thank you!
How about this:
$keys = array_keys($test['First item']);
Or if you want to do it manually:
$keys = array();
foreach($test['First item'] as $key => $value) {
$keys[] = $key;
}
That would be something like:
$keys = array_keys(reset($your_array));
reset() gets the first value of your array and array_keys() the keys of the resulting array.
You might want to split it in two lines using a temporary variable to avoid strict warnings.
It depends on what you are trying to archive:
$test = Array (
"First item" => Array (
"screen" => 2,
1 => 2,
2 => 2,
3 => 2,
4 => 2,
),
"Second Item" => Array (
"screen" => 3,
1 => 3,
2 => 3,
3 => 3,
4 => 3,
)
);
To get all the keys
$vals = [];
foreach($test as $k=>$v){
$vals = array_merge($vals, array_keys($v));
}
this will yield you:
Array
(
[0] => screen
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => screen
[6] => 1
[7] => 2
[8] => 3
[9] => 4
)
Separated in another multidimensional array:
foreach($arr as $k=>$v){
$vals[] = array_keys($v);
}
Only the unique keys:
foreach($test as $k=>$v){
$vals = array_unique(array_merge($vals, array_keys($v)));
}
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',
),
)