Reputation: 223
I have an associative array of counts with brand_slug
keys and a 2d array to which I would like to append related count data.
$countArray = [
"abu-garcia" => 1,
"daiwa" => 4,
"shimano" => 4
];
$dataArray = [
[
"brand_id" => 36,
"brand_name" => "Abu Garcia",
"brand_slug" => "abu-garcia"
],
[
"brand_id" => 41,
"brand_name" => "Daiwa",
"brand_slug" => "daiwa"
],
[
"brand_id" => 41,
"brand_name" => "Daiwa",
"brand_slug" => "daiwa"
],
[
"brand_id" => 39,
"brand_name" => "Shimano",
"brand_slug" => "shimano"
],
[
"brand_id" => 39,
"brand_name" => "Shimano",
"brand_slug" => "shimano"
],
];
I want to create a new array which should look like this:
[
[
"brand_id" => 36,
"brand_name" => "Abu Garcia",
"brand_slug" => "abu-garcia",
"count" => 1
],
[
"brand_id" => 41,
"brand_name" => "Daiwa",
"brand_slug" => "daiwa",
"count" => 4,
],
[
"brand_id" => 41,
"brand_name" => "Daiwa",
"brand_slug" => "daiwa",
"count" => 4
],
[
"brand_id" => 39,
"brand_name" => "Shimano",
"brand_slug" => "shimano",
"count" => 4
],
[
"brand_id" => 39,
"brand_name" => "Shimano",
"brand_slug" => "shimano",
"count" => 4
],
];
Upvotes: -2
Views: 1065
Reputation: 48073
The task of adding a new column to all rows in a 2d array based on an associative lookup/mapping array is most elegantly done with array_map()
and the array union operator (+
).
Code: (Demo)
var_export(
array_map(
fn($row) => $row + ['count' => $countArray[$row['brand_slug']]],
$dataArray
)
);
There is also nothing wrong with modifying the data array by reference in a classic foreach()
loop . (Demo)
foreach ($dataArray as &$row) {
$row['count'] = $countArray[$row['brand_slug']];
}
var_export($dataArray);
As a consideration not presented in the asker's sample data, if a row's value is not found in the mapping array, use the null coalescing operator to fallback to a default value.
['count' => $countArray[$row['brand_slug']] ?? $someDefault]
Or
$row['count'] = $countArray[$row['brand_slug']] ?? $someDefault;
Upvotes: 0
Reputation:
array_merge Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
Example #array_merge() example
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Upvotes: -1
Reputation: 970
In a simplest way you can do like this. Suppose your first array name is $first_array and second one is $second_array.
just loop the second one like
foreach($second_array as $index => $item)
{
$second_array[$index]['count'] = $first_array[$item['brand_slug']];
}
echo "<pre>";
print_r($second_array);
Upvotes: 0
Reputation: 2347
I think I understand what you're trying to do...
If these are examples of your arrays:
$CountArray = array("abu-garcia"=>1,
"daiwa"=>4,
"shimano"=>4);
$DataArray = array(
array("brand_id"=>36, "brand_name"=>"Abu Garcia", "brand_slug"=>"abu-garcia"),
array("brand_id"=>41, "brand_name"=>"Daiwa", "brand_slug"=>"daiwa"),
array("brand_id"=>39, "brand_name"=>"Shimano", "brand_slug"=>"shimano")
);
Then loop through your second array to build a new array, and adding the count
key from the first array like this:
$i = 0;
foreach($DataArray as $ItemArray){
foreach($ItemArray as $Key=>$Value){
$NewArray[$i][$Key] = $Value;
$NewArray[$i]['count'] = 0;
if(isset($CountArray[$ItemArray['brand_slug']])){
$NewArray[$i]['count'] = $CountArray[$ItemArray['brand_slug']];
}
}
ksort($NewArray[$i]);
$i++;
}
print_r($NewArray);
Upvotes: 1