Merge Two Arrays as Key Value Pairs in PHP

Merge two arrays as key value pairs in PHP

See array_combine() on PHP.net.

PHP merge two arrays on the same key AND value

Try out this code, It might help you, it's short and without traversing loops:

    usort($array2,function($a,$b){
return strnatcmp($a['ur_user_id'],$b['ur_user_id']);
});
$array3 = array_replace_recursive($array1, $array2);

$result = array_uintersect($array3,$array1,function($a,$b){
return strnatcmp($a['ur_user_id'],$b['ur_user_id']);
});
print_r($result);

Output

Array
(
[0] => Array
(
[ur_user_id] => 1
[ur_fname] => PerA
[ur_lname] => SonA
[ur_code] => AA
[ur_user_role] => testA
)

[1] => Array
(
[ur_user_id] => 2
[ur_fname] => PerB
[ur_lname] => SonB
[ur_code] => BB
[ur_user_role] => testB
)

[2] => Array
(
[ur_user_id] => 3
[ur_fname] => PerC
[ur_lname] => SonC
[ur_code] => CC
[ur_user_role] => testC
)

)

And Here is Demo

Merge two arrays with unequal values into key value pair

This is how I solved it, using array_chunks only:

$values = array_chunk($data, count($headers));

This almost produced what I wanted, except the key was the index from the headers array, not the header itself. Since I still had I reference to the headers array, when I wanted to loop through, I did it like this:

When I wanted to use these values in the array, I extracted like this:

foreach ($values[$key] as $key => $value)
{
$value; // the data
$headers[$key]; // the header (String, not index)
}

PHP: Set two arrays into key and value

Do they need to be separate?

You could use array_combine() to assign a key to the value and then input is just $array[$input] - e.g.

$arr1 = ['A', 'B', 'C'];
$arr2 = [1, 2, 3];

$arr = array_combine($arr2, $arr1);

echo $arr[$_POST['input']]; # will display 2

merge multiple arrays with their same key value in PHP

Direct Output

If you just need to output your array directly to the page, you could do something like this:

foreach($original_array['category_id'] as $key => $categoryid) {
$product = $original_array['product'][$key];
$category = $original_array['category'][$key];

echo "{$categoryid} , {$product} , {$category}\n";
}

This is pretty simple and only requires a single foreach loop. This is useful if you do not need to use this data multiple times in different places.



Formatted Array Output

If you may need to use this later in code, I would create a new array with the data in the loop above.
This is probably overkill if you don't ever need to access this data again.

$output = [];
foreach($original_array['category_id'] as $key => $categoryid) {
$product = $original_array['product'][$key];
$category = $original_array['category'][$key];

$output[] = ['category_id' => $categoryid, 'product' => $product, 'category' => $category];
}

This would make $output be something like

Array
(
[0] => Array
(
[category_id] => 1
[product] => Apple
[category] => Fruit
)

[1] => Array
(
[category_id] => 2
[product] => Cow
[category] => Meat
)

)

Which, of course, is trivial to output in your required format.

foreach($output as $values) {
echo "{$values['categoryid']} , {$values['product']} , {$values['category']}";
}

Merge two arrays of both values and make as one array like as one key other value in php

You can use array_combine, which

Creates an array by using one array for keys and another for its values

Like so:

$result = array_combine($arr1, $arr2)

The output:

Array
(
[x-api-key] => cb2312b123113b321b
[username] => john
[password] => 1234567
)

Test the code here.

Merge two arrays by multiple key value pairs

This would be a simple approach:

<?php
$withOfd = [
310 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'districts' => "...",
'main_city' => "Jeddah",
'sub_city' => "Jeddah",
'ofd' => 25
],
311 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'districts' => "...",
'main_city' => "Jeddah",
'sub_city' => "Khulais",
'ofd' => 1
]
];

$withoutOfd = [
260 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'main_city' => "Jeddah",
'sub_city' => "Jeddah",
'districts' => "...",
'delivered' => 16,
'undelivered' => 3,
'returnedToowner' => 0
],
261 => [
'walker_id' => 158,
'walker' => "158 ??????????",
'main_city' => "Jeddah",
'sub_city' => "Khulais",
'districts' => "...",
'delivered' => 0,
'undelivered' => 1,
'returnedToowner' => 0
]
];

$result = [];
array_walk($withOfd, function($with) use ($withoutOfd, &$result) {
array_walk ($withoutOfd, function($without) use (&$result, $with) {
if ( $with['walker_id'] == $without['walker_id']
&& $with['main_city'] == $without['main_city']
&& $with['sub_city'] == $without['sub_city']) {
$result[] = array_merge($with, $without);
}
});
});
print_r($result);

The output obviously is:

Array
(
[0] => Array
(
[walker_id] => 158
[walker] => 158 ??????????
[districts] => ...
[main_city] => Jeddah
[sub_city] => Jeddah
[ofd] => 25
[delivered] => 16
[undelivered] => 3
[returnedToowner] => 0
)

[1] => Array
(
[walker_id] => 158
[walker] => 158 ??????????
[districts] => ...
[main_city] => Jeddah
[sub_city] => Khulais
[ofd] => 1
[delivered] => 0
[undelivered] => 1
[returnedToowner] => 0
)
)

UPDATE:

As a result of the comments and the discussion in the chat another variant which preserves entries in either array which do not have corresponding entries:

$result = [];
array_walk($withOfd, function($with, $withKey) use (&$withOfd, &$withoutOfd, &$result) {
array_walk ($withoutOfd, function($without, $withoutKey) use (&$withOfd, &$withoutOfd, &$result, $with, $withKey) {
if ( $with['walker_id'] == $without['walker_id']
&& $with['main_city'] == $without['main_city']
&& $with['sub_city'] == $without['sub_city']) {
$result[] = array_merge($with, $without);
unset($withOfd[$withKey]);
unset($withoutOfd[$withoutKey]);
}
});
});
$result = array_merge($result, $withOfd, $withoutOfd);

php combine two array's subarrays where value of certain key is equal

Try this:

foreach($arr_b as $b_item) {
foreach($arr_a as $key => &$a_item) {
if ($b_item['usrsID'] == $a_item['usrsID']) {
$a_item['id'] = $b_item['usrsID'];
$a_item['avatarID'] = $b_item['avatarID'];
}
}
}

Your output of $_arr_a will be:

Array
(
[0] => Array
(
[ID] => 5
[usrsID] => 3
[tid] => 19
[txtid] => 22
[id] => 3
[avatarID] => 22
)

[1] => Array
(
[ID] => 6
[usrsID] => 1
[tid] => 19
[txtid] => 23
[id] => 1
[avatarID] => 1
)

[2] => Array
(
[ID] => 7
[usrsID] => 2
[tid] => 19
[txtid] => 24
[id] => 2
[avatarID] => 3
)

[3] => Array
(
[ID] => 8
[usrsID] => 1
[tid] => 19
[txtid] => 25
[id] => 1
[avatarID] => 1
)

)


Related Topics



Leave a reply



Submit