Merge or combine an array like this? on PHP - php

I have this multi-dimensional PHP array (below).
Array
(
[0] => Array
(
[2] => one#example.com
)
[1] => Array
(
[4] => two#example.com
)
[2] => Array
(
[3908] => twenty#example.com
)
[3] => Array
(
[2548] => eleven#example.com
)
[4] => Array
(
[3099] => ten#example.com
)
[5] => Array
(
[5283] => six#example.com
)
)
I was wondering how could I merge? or combine? or simply do it like this using PHP (below).
Array
(
[2] => one#example.com
[4] => two#example.com
[3908] => twenty#example.com
[2548] => eleven#example.com
[3099] => ten#example.com
[5283] => six#example.com
)
Help

You can "promote" the second level elements to first level elements via call_user_func_array(). And while array_merge() re-indexes the array, array_replace() does not but retains the original keys which makes this a one-liner:
$a = call_user_func_array('array_replace', $a);
test:
<?php
$a = array (
array (2 => 'one#example.com'),
array (4 => 'two#example.com'),
array (3908 => 'twenty#example.com'),
array (2548 => 'eleven#example.com'),
array (3099 => 'ten#example.com'),
array (5283 => 'six#example.com'),
);
$a = call_user_func_array('array_replace', $a);
var_dump($a);

Simply go through the array and store to another.
$a = array( 0 => array(1) , 1=> array(3) );
$another=array();
foreach ($a as $k=>$v){
foreach ($v as $y=>$z){
$another[$k]=$z;
}
}
print_r($another);

How about this?
foreach( $old_arr as $old )
{
$key = key( $old[0] );
$new[ $key ] = $old[0][$key];
}
EDIT: Never mind didn't realize previous answer was corrected.

Related

PHP - Merge two arrays on same key

I'd like to merge two arrays on same key.
Here's the 1st array :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
)
)
)
And this is the 2nd array
Array
(
[2052] => Array
(
[495] => Array
(
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
And i'd like merge that two arrays to be like this :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
This is my first project and I don't know what to do.
Can anyone tell me how to do that?
Pls
If your arrays have same Key then:
$array1 = array(); //put your value in this array
$array2 = array(); //put your value in this array
$array3 = array();
$array3[] = $array1;
$array3[] = $array2;
Assuming your two arrays are $array1 and $array 2 respectively, try this:
foreach($array1 as $k1 => $v1) {
foreach($v1 as $k2 => $v2) {
foreach($v2 as $k3 => $v3) {
$new[$k1][$k2][$k3] = $array1[$k1][$k2][$k3];
$new[$k1][$k2] = array_merge($new[$k1][$k2], $array2[$k1][$k2]);
}
}
}
=> store the all value in this variable
$Arr1 //put your value in this array
$Arr2 //put your value in this array
=> And merge it
$ResponseDetails = array_merge( (array)$Arr1, (array)$Arr2);
array_replace_recursive should do the job:
// $arr1 is the 1st array, $arr2 - is your 2nd array
$result = array_replace_recursive($arr1, $arr2); // now the $result variable contains the expected merged result
http://php.net/manual/en/function.array-replace-recursive.php

How to concatenate arrays element recursively [duplicate]

This question already has answers here:
Joining similar data from two indexed arrays
(2 answers)
Closed 5 months ago.
I'm working on a project and I'm stacked since 2 days, this is my problem: I have two arrays and want to retrieve the second item in each object in Array_2 and concatenate it to the content of each object in first Array_1 in PHP.
Array_1
[[1453274700000,24011],[1453275000000,24222],[1453275300000,24284],[1453275600000,24331],...]
Array_2
[[1453274700000,51951],[1453275000000,52093],[1453275300000,52251],[1453275600000,52288],...]
Wanted_array
[[1453274700000,24011,51951],[1453275000000,24222,52093],[1453275300000,24284,52251],[1453275600000,24331,52288]...]
A functional solution:
$result = array_map(function (array $a1, array $a2) {
return array_merge($a1, [$a2[1]]);
}, $array_1, $array_2);
This assumes that all items are in order and only need to be merged by their order, not by their first value.
If you want $item[0] to define what "group" each value belongs to, you can iterate through the first array and save $item[0] as the key and $item[1] as the value. Do the same for the second array. Now iterate through the saved array for array1, and check if the saved array for array2 contains the same keys. Do the same for array2 (in case it has key that array1 doesn't have), and save it all to a new array:
<?php
$arr1 = array(
array('1453274700000',24011),
array('1453275000000',24222),
array('1453276000000',24222), // inexistent in $arr2
);
$arr2 = array(
array('1453275000000',52093),
array('1453274700000',51951),
array('1453273000000',24222), // inexistent in $arr1
);
$arr1dictionary = [];
$arr2dictionary = [];
$result = [];
foreach ($arr1 as $collection) {
$arr1dictionary[$collection[0]] = $collection[1];
}
foreach ($arr2 as $collection) {
$arr2dictionary[$collection[0]] = $collection[1];
}
foreach ($arr1dictionary as $key => $value) {
if (isset($arr2dictionary[$key])) {
$result[$key] = [$key, $value, $arr2dictionary[$key]];
} else {
$result[$key] = [$key, $value, null];
}
}
foreach ($arr2dictionary as $key => $value) {
if (isset($result[$key])) {
continue;
}
$result[$key] = [$key, null, $value];
}
$result = array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453276000000
[1] => 24222
[2] => (null, the value only exists in $arr1)
)
[3] => Array
(
[0] => 1453273000000
[1] => (null, the value only exists in $arr2)
[2] => 24222
)
)
DEMO
Use array_walk and add second item from $array2 if it exists.
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331)
);
$array2 = array(
array(1453274700000,51951),
array(1453275000000,52093),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
if(isset($array2[$key][1])){
$item[] = $array2[$key][1];
}
});
print_r($array1);
Output
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
)
EDIT
As #h2ooooooo pointed out that there could be possibility that array items are in random order. If array items can be in random order and they are matched with first index value, use this (works with PHP >= 5.5.0):
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331),
array(1453276000000,24222) // no match in $array2
);
$array2 = array(
array(1453275000000,52093),
array(1453274700000,51951),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
// Find match in $array2
$array2_key = array_search($item[0], array_column($array2, 0));
// If match found
if($array2_key !== false && isset($array2[$array2_key][1])){
$item[] = $array2[$array2_key][1];
}
// No match
else{
$item[] = null;
}
});
print_r($array1);
OUTPUT
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
[4] => Array
(
[0] => 1453276000000
[1] => 24222
[2] =>
)
)

How to merge Arrays having same keys only and neglect different keys?

I am new in PHP and love to learn it.
I want to merge two or more arrays having same keys only. And neglect the arrays whose keys are not in both the Arrays. Like
Here is the First Array :
Array
(
[1] => Array
(
[111] => 36265
)
[2] => Array
(
[222] => 36265
)
[3] => Array
(
[333] => 36265
)
)
and Second Array as :
Array
(
[1] => Array
(
[444] => 36265
)
[2] => Array
(
[555] => 36265
)
[4] => Array
(
[666] => 36265
)
)
And i want my result to be as :
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
Neglecting the rest Array with key [3] & [4]....
So Please anyone tell me how to get this. I try out "array_merge_recursive()" but this one displays all keys.
Thanks in advance.
You'd have to loop over one of the arrays, check for the existence of the current key in the other array, if it exists, merge them, eg:
$output = array();
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $array2)) {
$output[$key] = $value + $array2[$key];
}
}
Here's a demo
You're probably looking for array_intersect_key.
You could also use a foreacah loop and create a new one. While in this loop you can use one of the arrays keys to match them both. Consider this example:
$array1 = array( 1 => array(111 => 36265), 2 => array(222 => 36265), 3 => array(333 => 36265), ); $array2 = array( 1 => array(444 => 36265), 2 => array(555 => 36265), 4 => array(666 => 36265), );
$new_array = array();
foreach($array1 as $key => $value) {
if(isset($array2[$key])) {
$new_array[$key][key($array1[$key])] = reset($array1[$key]);
$new_array[$key][key($array2[$key])] = reset($array2[$key]);
}
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
Should yield something like this:
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)

php merge 2 arrays into one associative array

Using PHP I need to merge 2 arrays (of equal length into one associative array) here is an excerpt from my current data set:
[1] => Array
(
[0] => C28
[1] => C29
)
[2] => Array
(
[0] => 1AB010050093
[1] => 1AB008140029
)
both elements [1] and [2] are actually a lot longer than just 2 sub-elements (like I said, this is an excerpt).
The deal is that "C28" in the first array corresponds to "1AB010050093" in the second array, and so on... The result I need is to create a new associative array that looks like this:
[1] => Array
(
['ref'] => C28
['part'] => 1AB010050093
)
[2] => Array
(
['ref'] => C29
['part'] => 1AB008140029
)
and so on...
If you are willing to compromise with an array structure like this:
array(
'C28' => '1AB010050093',
'C29' => '1AB008140029'
);
Then you can use the array_combine() (Codepad Demo):
array_combine($refNumbers, $partIds);
Otherwise, you'll need to use a foreach (Codepad Demo):
$combined = array();
foreach($refNumbers as $index => $refNumber) {
if(!array_key_exists($index, $partIds)) {
throw OutOfBoundsException();
}
$combined[] = array(
'ref' => $refNumber,
'part' => $partIds[$index]
);
}
If you're using PHP 5.5+, there is a new method called array_column(), which will get all of the values in a particular column. That could potentially be used, although I think just a simple foreach loop will probably still be your best bet.
How about:
$arr1 = array(
0 => 'C28',
1 => 'C29',
);
$arr2 = array(
0 => '1AB010050093',
1 => '1AB008140029',
);
$result = array();
for ($i=0; $i<count($arr1); $i++) {
$result[] = array('ref' => $arr1[$i], 'part' => $arr2[$i]);
}
print_r($result);
ouptut:
[1] => Array
(
[0] => C28
[1] => C29
)
[2] => Array
(
[0] => 1AB010050093
[1] => 1AB008140029
)

Splice an array when key_value same?

My sorted array needs to be split up so that each created array has all of those associated with that value.
Array (
[NAME] => Array
(
[0] => Fooaz
[1] => bzdsfdasfz
[2] => Fooooooooo
)
[DESCRIPTION] => Array
(
[0] => Foo
[1] => Foo
[2] => Barrrr
)
)
For example, from that array I want to get two arrays. One containing:
[NAME]=>Array([0] => Fooooooooo), [DESCRIPTION]=>Array([0] => Barrrr):
The other containing the remaining elements.
What's an efficient way of doing this?
$arr = Array (
'NAME' => Array
(
0 => 'Fooaz',
1 => 'bzdsfdasfz',
2 => 'Fooooooooo'
),
'DESCRIPTION' => Array
(
0 => 'Foo',
1 => 'Foo',
2 => 'Barrrr'
)
);
$arr1 = array();
foreach ($arr as $key => $value) {
$arr1[$key][] = array_pop($arr[$key]);
}
print_r($arr);
print_r($arr1);
Use array_pop():
http://codepad.org/GeP6OEtf
<?php
$arr=array(
"NAME"=>array('a','b','c','d'),
"DESC"=>array('A','B','C','D')
);
$newarr=array();
foreach ($arr as $key=>$value) {
$newarr[$key]=array(array_pop($arr[$key]));
}
print_r($arr);
echo "\n-------------\n";
print_r($newarr);
You can use extract function.
extract($array);
this will give you two arrays automatically.
reference

Categories