Extending 2 arrays depending on a value - php

I have two example arrays:
$array1 = array(
0 => array("a" => '123', "b" => 234),
1 => array("a" => 'abs', "b" => 234),
2 => array("a" => '123', "b" => 234),
3 => array("a" => 'abs', "b" => 234),
4 => array("a" => '123', "b" => 234),
5 => array("a" => 'abs', "b" => 898),
6 => array("a" => '123', "b" => 234),
7 => array("a" => 'abs', "b" => 234),
8 => array("a" => '123', "b" => 234),
9 => array("a" => 'abs', "b" => 898)
);
$array2 = array(
0 => array("b" => '234', "c" => "Herr"),
1 => array("b" => '898', "c" => "Frau"),
);
Array 1 should be extended by c, depending on the value in b, which is present in both arrays. Finally, array 1 should look like this:
$array3 = array(
0 => array("a" => '123', "b" => 234, "c" => "Herr"),
1 => array("a" => 'abs', "b" => 234, "c" => "Herr"),
2 => array("a" => '123', "b" => 234, "c" => "Herr"),
3 => array("a" => 'abs', "b" => 234, "c" => "Herr"),
4 => array("a" => '123', "b" => 234, "c" => "Herr"),
5 => array("a" => 'abs', "b" => 898, "c" => "Frau"),
6 => array("a" => '123', "b" => 234, "c" => "Herr"),
7 => array("a" => 'abs', "b" => 234, "c" => "Herr"),
8 => array("a" => '123', "b" => 234, "c" => "Herr"),
9 => array("a" => 'abs', "b" => 898, "c" => "Frau")
);
Are there already simple ways to realize this in PHP7.x?

Make an iteration over $array1 using array_walk(). In use() scope pass the filter array prepared (['234' => 'Herr', '898' => 'Frau']) from $array2. In every cycle of the iteration get value from $filter array by the index value of b and set it to $array1's c index. Example:
$filter = array_column($array2, 'c', 'b');
array_walk($array1, function(&$val) use ($filter) {
$val['c'] = $filter[$val['b']];
});
print_r($array1);
Working demo.

One way of doing it would be a loop through foreach, create an array that contains all the b indexes in $array2 by using array_column(). Then use array_search() on that column, and look for the index b of the current iteration of $array1 - that key (from $array2) will tell you which index to choose. Fetch the value and append it to the temporary array, then append the temporary array into $array3.
$array3 = array();
$b_values = array_column($array2, "b");
foreach ($array1 as $v) {
$temp = $v;
$key_2 = array_search($v['b'], $b_values);
$temp['c'] = $array2[$key_2]['c'];
$array3[] = $temp;
}
Live demo at https://3v4l.org/lZHKS

Related

PHP - Merge 2 arrays with same keys [duplicate]

In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array.
The problem:
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
array_merge($A, $B);
// result
[a] => 1 [b] => 2 [c] => 4 [d] => 5
As you see, 'c' => 3 is missed.
So how can I merge all of them with the same keys?
You need to use array_merge_recursive instead of array_merge. Of course there can only be one key equal to 'c' in the array, but the associated value will be an array containing both 3 and 4.
Try with array_merge_recursive
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);
echo "<pre>";
print_r($c);
echo "</pre>";
will return
Array
(
[a] => 1
[b] => 2
[c] => Array
(
[0] => 3
[1] => 4
)
[d] => 5
)
$arr1 = array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
"4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);
if you want to convert this array as following:
$arr2 = array(
"0" => array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
),
"1" => array(
"0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
)
);
so, my answer will be like this:
$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
$inner_array = array();
$fid_value = $value['fid'];
if(!in_array($value['fid'], $unique_array))
{
array_push($unique_array, $fid_value);
unset($value['fid']);
array_push($inner_array, $value);
$outer_array[$fid_value] = $inner_array;
}else{
unset($value['fid']);
array_push($outer_array[$fid_value], $value);
}
}
var_dump(array_values($outer_array));
hope this answer will help somebody sometime.
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$C = array_merge_recursive($A, $B);
$aWhere = array();
foreach ($C as $k=>$v) {
if (is_array($v)) {
$aWhere[] = $k . ' in ('.implode(', ',$v).')';
}
else {
$aWhere[] = $k . ' = ' . $v;
}
}
$where = implode(' AND ', $aWhere);
echo $where;
I just wrote this function, it should do the trick for you, but it does left join
public function mergePerKey($array1,$array2)
{
$mergedArray = [];
foreach ($array1 as $key => $value)
{
if(isset($array2[$key]))
{
$mergedArray[$value] = null;
continue;
}
$mergedArray[$value] = $array2[$key];
}
return $mergedArray;
}
Two entries in an array can't share a key, you'll need to change the key for the duplicate

how merge multiple query result into single result in php [duplicate]

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
I'm currently stuck on how to merge multi query result into single result like;
multiple result:
$a1 = array(
["name" => "coca-cola"],
["name" => "sprite"],
["name" => "pepsi"]
);
$a2 = array(
["color" => "red"],
["color" => "green"],
["color" => "blue"]
);
$a3 = array(
["price" => 2],
["price" => 1],
["price" => 4]
);
expected output:
$res = array(
["name" => "coca-cola","color" => "red", "price" => 2],
["name" => "sprite","color" => "green", "price" => 1],
["name" => "pepsi","color" => "blue", "price" => 4]
);
Try this solution.
$a1 = array(
["name" => "coca-cola"],
["name" => "sprite"],
["name" => "pepsi"]
);
$a2 = array(
["color" => "red"],
["color" => "green"],
["color" => "blue"]
);
$a3 = array(
["price" => 2],
["price" => 1],
["price" => 4]
);
$res = array();
for($i=0; $i<count($a1); $i++){
$res[] = array_merge($a1[$i],$a2[$i],$a3[$i]);
}
Here we are assuming that all $a1, $a3, $a3 arrays have the same dimension.
One more solution:
$result = array_map(
function($name, $color, $price) {
return array_merge($name, $color, $price);
},
$a1, $a2, $a3
);
print_r($result);
share PHP code
Array
(
[0] => Array
(
[name] => coca-cola
[color] => red
[price] => 2
)
[1] => Array
(
[name] => sprite
[color] => green
[price] => 1
)
[2] => Array
(
[name] => pepsi
[color] => blue
[price] => 4
)
)

PHP: Merge arrays with same key inside one multidimensional array [duplicate]

In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array.
The problem:
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
array_merge($A, $B);
// result
[a] => 1 [b] => 2 [c] => 4 [d] => 5
As you see, 'c' => 3 is missed.
So how can I merge all of them with the same keys?
You need to use array_merge_recursive instead of array_merge. Of course there can only be one key equal to 'c' in the array, but the associated value will be an array containing both 3 and 4.
Try with array_merge_recursive
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);
echo "<pre>";
print_r($c);
echo "</pre>";
will return
Array
(
[a] => 1
[b] => 2
[c] => Array
(
[0] => 3
[1] => 4
)
[d] => 5
)
$arr1 = array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
"4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);
if you want to convert this array as following:
$arr2 = array(
"0" => array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
),
"1" => array(
"0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
)
);
so, my answer will be like this:
$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
$inner_array = array();
$fid_value = $value['fid'];
if(!in_array($value['fid'], $unique_array))
{
array_push($unique_array, $fid_value);
unset($value['fid']);
array_push($inner_array, $value);
$outer_array[$fid_value] = $inner_array;
}else{
unset($value['fid']);
array_push($outer_array[$fid_value], $value);
}
}
var_dump(array_values($outer_array));
hope this answer will help somebody sometime.
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$C = array_merge_recursive($A, $B);
$aWhere = array();
foreach ($C as $k=>$v) {
if (is_array($v)) {
$aWhere[] = $k . ' in ('.implode(', ',$v).')';
}
else {
$aWhere[] = $k . ' = ' . $v;
}
}
$where = implode(' AND ', $aWhere);
echo $where;
I just wrote this function, it should do the trick for you, but it does left join
public function mergePerKey($array1,$array2)
{
$mergedArray = [];
foreach ($array1 as $key => $value)
{
if(isset($array2[$key]))
{
$mergedArray[$value] = null;
continue;
}
$mergedArray[$value] = $array2[$key];
}
return $mergedArray;
}
Two entries in an array can't share a key, you'll need to change the key for the duplicate

PHP combine array all array elements

Assuming I don't want to loop through and build a new array, is there a built in way in PHP to add two arrays together and push all keys from the second array after the keys from the first? I Googled around and couldn't find anything that does exactly this, but wondering if anyone might know
For example to combine these..
array( 0 => "a", 1 => "b" );
array ( 0 => "c", 1 => "d" );
and get this..
array( 0 => "a", 1 => "b", 2 => "c", 3 => "d" );
This:
array_merge(array( 0 => "a", 1 => "b" ),array ( 0 => "c", 1 => "d" ));
Or
array( 0 => "a", 1 => "b" ) + array ( 0 => "c", 1 => "d" )
This first one will overwrite duplicate keys, the second will not. And you may have to sort the array afterwords.
Or, you could do:
array_merge(array_values(array( 0 => "a", 1 => "b" )), array_values(array ( 0 => "c", 1 => "d" )))
That will definitely work
Take a look at array_merge.
<?php
$ab = array('a', 'b');
$cd = array('c', 'd');
var_dump(
array_merge($ab, $cd)
);
/*
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
}
*/
You could also try:
<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = array_merge($array1, $array2);
print_r($result);
/*
Array
(
[0] => zero_a
[1] => two_a
[2] => three_a
[3] => one_b
[4] => three_b
[5] => four_b
)
*/
?>
Reference

Merging arrays with the same keys

In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array.
The problem:
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
array_merge($A, $B);
// result
[a] => 1 [b] => 2 [c] => 4 [d] => 5
As you see, 'c' => 3 is missed.
So how can I merge all of them with the same keys?
You need to use array_merge_recursive instead of array_merge. Of course there can only be one key equal to 'c' in the array, but the associated value will be an array containing both 3 and 4.
Try with array_merge_recursive
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);
echo "<pre>";
print_r($c);
echo "</pre>";
will return
Array
(
[a] => 1
[b] => 2
[c] => Array
(
[0] => 3
[1] => 4
)
[d] => 5
)
$arr1 = array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
"4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);
if you want to convert this array as following:
$arr2 = array(
"0" => array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
),
"1" => array(
"0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
)
);
so, my answer will be like this:
$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
$inner_array = array();
$fid_value = $value['fid'];
if(!in_array($value['fid'], $unique_array))
{
array_push($unique_array, $fid_value);
unset($value['fid']);
array_push($inner_array, $value);
$outer_array[$fid_value] = $inner_array;
}else{
unset($value['fid']);
array_push($outer_array[$fid_value], $value);
}
}
var_dump(array_values($outer_array));
hope this answer will help somebody sometime.
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$C = array_merge_recursive($A, $B);
$aWhere = array();
foreach ($C as $k=>$v) {
if (is_array($v)) {
$aWhere[] = $k . ' in ('.implode(', ',$v).')';
}
else {
$aWhere[] = $k . ' = ' . $v;
}
}
$where = implode(' AND ', $aWhere);
echo $where;
I just wrote this function, it should do the trick for you, but it does left join
public function mergePerKey($array1,$array2)
{
$mergedArray = [];
foreach ($array1 as $key => $value)
{
if(isset($array2[$key]))
{
$mergedArray[$value] = null;
continue;
}
$mergedArray[$value] = $array2[$key];
}
return $mergedArray;
}
Two entries in an array can't share a key, you'll need to change the key for the duplicate

Categories