1

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:',
                ),
            ),
        );

2 Answers 2

1

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:
                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.