Home > Software design >  Where two id matching, erase the value in two arrays
Where two id matching, erase the value in two arrays

Time:09-29

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 ?

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

CodePudding user response:

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']];
    }
}

CodePudding user response:

Check out this one:

 $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]
        ]
    ];
    
    foreach ($data2['custom'] as $d) {
     foreach ($data as $key => $cf) {     
           if ($cf['id'] == $d['id']):
              $data2['custom'][$key]['value'] = $cf['value'];
           endif;
        }
    }
    
    print_r($data2);

CodePudding user response:

You could try to iterate the first array and put the values of it inside the second. If they're always the same size.

Using a foreach go trough the elements on the first array and get the values:

$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]
            ]
        ];
        
        
foreach ($data as $key=>$singleData){
    $data2['custom'][$key]['value'] = $singleData['value'];
}

var_dump($data2);

The ouptup from var_dump() will be:

array(1) {
  ["custom"]=>
  array(2) {
    [0]=>
    array(3) {
      ["id"]=>
      int(123)
      ["name"]=>
      string(7) "Custom1"
      ["value"]=>
      string(4) "Text"
    }
    [1]=>
    array(3) {
      ["id"]=>
      int(124)
      ["name"]=>
      string(7) "Custom2"
      ["value"]=>
      int(12)
    }
  }
}
  • Related