简体   繁体   中英

Using PHP, search an array by key and replace value in another array with value from matched key

I have two arrays:

$array_a =   
array (
    0 => array (
        0 => array (
                'name' => 'name',
                'label' => 'LBL_NAME',
            ),
        1 =>
            array(
                'name' => 'phone_office',
                'label' => 'LBL_PHONE_OFFICE',
            ),
        ),
    1 => array (
        0 =>
            array(
                'name' => 'website',
                'label' => 'LBL_WEBSITE',
            ),
            1 =>
            array(
                'name' => 'phone_fax',
                'label' => 'LBL_FAX',
            ),
        ),
    );

and

$array_b = array(
    'LBL_NAME' => 'Name:',
    'LBL_PHONE_OFFICE' => 'Office phone:',
    'LBL_WEBSITE' => 'Website:',
    'LBL_FAX' => 'Fax number:',
); 

How do I go about replacing the value of [label] with the corresponding value from my second array?

In other words, what I want to end up with is:

$array_a =   
    array (
        0 => array (
            0 => array (
                    'name' => 'name',
                    'label' => 'Name:',
                ),
            1 =>
                array(
                    'name' => 'phone_office',
                    'label' => 'Office phone:',
                ),
            ),
        1 => array (
            0 =>
                array(
                    'name' => 'website',
                    'label' => 'Website:',
                ),
                1 =>
                array(
                    'name' => 'phone_fax',
                    'label' => 'Fax number:',
                ),
            ),
        );

You can do it as following:

foreach($array_a as $elemKey => $elemValue){
    foreach($elemValue as $itemKey => $itemValue){
         if(isset($array_a[$elemKey][$itemKey]['label'])){
              $array_a[$elemKey][$itemKey]['label'] = $array_b[$array_a[$elemKey][$itemKey]['label']];        
         } 
    }
}

print_r($array_a);

This will return:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => name
                    [label] => Name:
                )

            [1] => Array
                (
                    [name] => phone_office
                    [label] => Office phone:
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [name] => website
                    [label] => Website:
                )

            [1] => Array
                (
                    [name] => phone_fax
                    [label] => Fax number:
                )

        )

)

array_walk_recursive is quite handy for that:

echo "\n------------ Table A ----------\n";
print_r($array_a);

function acallback(&$value, $key, $replace) {
    if (key_exists($value, $replace)) {
        $value = $replace[$value];
    } 
}

$status = array_walk_recursive($array_a, 'acallback', $array_b);
if ($status === false) {
    throw new \Exception("array_walk failed");
}

echo "\n--------- Table A Modified ----------\n";
print_r($array_a);

the acallback function can be also anonymous and used this way:

echo "\n------------ Table A ----------\n";
print_r($array_a);

$status = array_walk_recursive($array_a, function(&$value, $key, $replace) {
    if (key_exists($value, $replace)) {
        $value = $replace[$value];
    } 
}
, $array_b);

if ($status === false) {
    throw new \Exception("array_walk failed");
}

echo "\n--------- Table A Modified ----------\n";
print_r($array_a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM