-1

I have 2 arrays like this. Array1:

Array
(
    [abu-garcia] => 1
    [daiwa] => 4
    [shimano] => 4
)
Array 2:
Array
(
    [0] => Array
        (
            [brand_id] => 36
            [brand_name] => Abu Garcia
            [brand_slug] => abu-garcia
        )

    [1] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
        )

    [2] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
        )

    [3] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
        )

    [4] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
        )

    [5] => Array
        (
            [brand_id] => 39
            [brand_name] => Shimano
            [brand_slug] => shimano
        )

    [6] => Array
        (
            [brand_id] => 39
            [brand_name] => Shimano
            [brand_slug] => shimano
        )

    [7] => Array
        (
            [brand_id] => 39
            [brand_name] => Shimano
            [brand_slug] => shimano
        )

I want to create a new array which should look like this:

    Array
(
    [0] => Array
        (
            [brand_id] => 36
            [brand_name] => Abu Garcia
            [brand_slug] => abu-garcia
            [count] => 1
        )

    [1] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
            [count] => 4
        )

    [2] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
            [count] => 4
        )

    [3] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
            [count] => 4
        )

    [4] => Array
        (
            [brand_id] => 41
            [brand_name] => Daiwa
            [brand_slug] => daiwa
            [count] => 4
        )

    [5] => Array
        (
            [brand_id] => 39
            [brand_name] => Shimano
            [brand_slug] => shimano
            [count] => 4
        )

    [6] => Array
        (
            [brand_id] => 39
            [brand_name] => Shimano
            [brand_slug] => shimano
            [count] => 4
        )

    [7] => Array
        (
            [brand_id] => 39
            [brand_name] => Shimano
            [brand_slug] => shimano
            [count] => 4
        )

Both there arrays are created by wordpress. array1 is an output from a function when checking the number of products in the brand cat and other array is giving the brands details. Thanks

kalle
  • 215
  • 3
  • 8

3 Answers3

2

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);
JustBaron
  • 2,319
  • 7
  • 25
  • 37
0

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);
Maya Shah
  • 950
  • 7
  • 17
0

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);
?>