Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

Compare and Update PHP array

I have two arrays in PHP:

Array 1

Array
(
    [0] => Array
        (
            [autoid] => t35wmkbg
            [task] => Zaphod
            [progress] => incomplete
        )

    [1] => Array
        (
            [autoid] => cwg2yc5q
            [task] => Arthur
            [progress] => incomplete
        )

    [2] => Array
        (
             [autoid] => 85q4bcmy
             [task] => Ford
             [progress] => incomplete
        )

    [3] => Array
        (
             [autoid] => yc5qcwg2
             [task] => Trillian
             [progress] => incomplete
        )
)

Array 2

Array
(
    [0] => Array
        (
            [autoid] => t35wmkbg
            [task] => Zaphod
            [progress] => complete
        )

    [1] => Array
        (
            [autoid] => 85q4bcmy
            [task] => Ford
            [progress] => incomplete
        )

    [2] => Array
        (
            [autoid] => cwg2yc5q
            [task] => Arthur
            [progress] => pending
        )

)

I need to:

  • Step through Array 1, and check if the autoid exists in Array 2.
  • If it does exist, update the progress field to match.

So the ideal output when using the arrays above would be:

Array 3

Array
(
    [0] => Array
        (
            [autoid] => t35wmkbg
            [task] => Zaphod
            [progress] => complete
        )


    [1] => Array
        (
            [autoid] => cwg2yc5q
            [task] => Arthur
            [progress] => pending
        )

    [2] => Array
        (
             [autoid] => 85q4bcmy
             [task] => Ford
             [progress] => incomplete
        )
 
    [3] => Array
        (
             [autoid] => yc5qcwg2
             [task] => Trillian
             [progress] => incomplete
        )

)

I am not sure how best to go about this, if anyone has any thoughts or pointers that would be awesome.

question from:https://stackoverflow.com/questions/65843734/compare-and-update-php-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First change the arr2 so its index is the autoid then you can use it easily to get the progress value. Then just foreach over arr1 and apply changes to progress if they exist

$arr1 = [
            ['autoid' => 't35wmkbg','task' => 'Zaphod','progress' => 'incomplete'],
            ['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'incomplete'],
            ['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
            ['autoid' => 'yc5qcwg2', 'task' => 'Trillian', 'progress' => 'incomplete']
        ];

$arr2 = [
            ['autoid' => 't35wmkbg', 'task' => 'Zaphod', 'progress' => 'complete'],
            ['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
            ['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'pending']
        ];

#first make arr2 indexable on the autoid
foreach ($arr2 as $a) {
    $arr2search[$a['autoid']] = $a;
}

foreach ($arr1 as $a){
    // do we have an autoid in arr2
    if ( isset($arr2search[$a['autoid']])){
        $a['progress'] = $arr2search[$a['autoid']]['progress'];
    }
    $merged[] = $a;
    
}

print_r($merged);

RESULT

Array
(
    [0] => Array
        ([autoid] => t35wmkbg,  [task] => Zaphod,  [progress] => complete)

    [1] => Array
        ([autoid] => cwg2yc5q, [task] => Arthur, [progress] => pending )

    [2] => Array
        ([autoid] => 85q4bcmy,  [task] => Ford,  [progress] => incomplete )

    [3] => Array
        ([autoid] => yc5qcwg2, [task] => Trillian, [progress] => incomplete )
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...