Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Where two id matching, erase the value in two arrays

I have two arrays like this :

$data = [
    0 => ['id' => 123, 'value' => 'Text'],
    1 => ['id' => 124, 'value' => 12]
];
$data2 = [
    "custom" => [
        0 => ['id' => 123, 'name' => 'Custom1', 'value' => null],
        1 => ['id' => 124, 'name' => 'Custom2', 'value' => null]
    ]
];

I would like to put the value in $data in the value of $data2 instead of "null" values where the ID are matching.

How can I do this ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I try to do this but does not work :

foreach ($data2['custom'] as $d) {
    foreach ($data as $cf) {
        if ($cf['id'] == $d['id']):
            $cf['value'] = $d['value'];
        endif;
    }
}

Thanks

>Solution :

Here is a suggestion, first transform the $data array into an array with the id as a key to make the following process simple.

Then process over the $data2 array and in the foreach us the & reference indicator, so that amendments inside the foreach loop are applied to the original $data2 array and not the copy of the $data2 array that would normally be created as part of a foreach loop

$data = [   ['id' => 123, 'value' => 'Text'],['id' => 124, 'value' => 12] ];
$data2 = [ "custom" => [   
            ['id' => 123, 'name' => 'Custom1', 'value' => null],
            ['id' => 124, 'name' => 'Custom2', 'value' => null]]    
];

// transform $data into an array with a useful key
foreach( $data as $d){
    $useful[$d['id']] = $d['value'];
}

foreach ( $data2['custom'] as &$data ) {
    // first check that the id exists in the new useful array
    if ( isset($useful[$data['id']]) ) {
        // if it does apply the value to  the data2 array
        $data['value'] = $useful[$data['id']];
    }
}
    
print_r($data2);

RESULT

Array
(
    [custom] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [name] => Custom1
                    [value] => Text
                )
            [1] => Array
                (
                    [id] => 124
                    [name] => Custom2
                    [value] => 12
                )
        )
)

In reply to your comment about doing it without using the reference in the loop, yes like this

foreach ( $data2['custom'] as $idx => $data ) {
    if ( isset($useful[$data['id']]) ) {
        $data2['custom'][$idx]['value'] = $useful[$data['id']];
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading