php | array_map replacement

1.3k views Asked by At

I'm looking for the way to replace my array. My first array below:

$arr1 = Array
(
    [0] => stdClass Object
        (
            [values] => Array
                (
                    [0] => stdClass Object
                        (
                            [field_value] => Green
                            [count] => 0
                        )

                    [1] => stdClass Object
                        (
                            [field_value] => Red
                            [count] => 0
                        )
                )

        )
    [1] => stdClass Object
        (
            [values] => Array
                (
                    [0] => stdClass Object
                        (
                            [field_value] => Plastic
                            [count] => 0
                        )

                    [1] => stdClass Object
                        (
                            [field_value] => Metall
                            [count] => 0
                        )
                )

        )

My second array:

$arr2 = Array
(
    [0] => 2
    [1] => 6
    [2] => 3
    [3] => 4
)

And I would like to get this:

Array
(
    [0] => stdClass Object
        (
            [values] => Array
                (
                    [0] => stdClass Object
                        (
                            [field_value] => Green
                            [count] => 2
                        )

                    [1] => stdClass Object
                        (
                            [field_value] => Red
                            [count] => 6
                        )
                )

        )
    [1] => stdClass Object
        (
            [values] => Array
                (
                    [0] => stdClass Object
                        (
                            [field_value] => Plastic
                            [count] => 3
                        )

                    [1] => stdClass Object
                        (
                            [field_value] => Metall
                            [count] => 4
                        )
                )

        )

I have tried to use array_map function but without any success.

array_map(function($a,$b){$a = $b; return $a;}, $arr1, $arr2);

Thanks!

3

There are 3 answers

0
kamal pal On

Create your own function as needed, and customize as you like, see below example:

function buildMyArray($array1, $array2)
{   

    foreach($array1[0]->values as $key => $value){
        $array1[0]->values[$key]['count'] = $array2[$key];
    }

    return $array1;
}

and you may call it like:

$result = buildMyArray($arr1, $arr2);
1
abh On
$array=$arr[0]->values;
$new_array=array(); 

foreach($array as $key=>$val)
{
    $new_array[$key]=$val;
    $new_array[$key]->count=$arr2[$key];

}


$result=array();
$result[0]->values=$new_array;  
1
xdazz On

If you want to use array_map, then something should be like below:

$arr1[0]->values = array_map(function($v, $k) use ($arr2)  {
    // if not found in $arr2, remain the original value.
    $v->count = isset($arr2[$k]) ? $arr2[$k] : $v->count;
    return $v;
}, $arr1[0]->values, array_keys($arr1[0]->values));