Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - merge two array with loop and key name

i have two separate arrays $first_one:

Array
(
    [0] => Array
        (
            [_date] => 2019-10-16
            [_number] => 1
            [_order] => 1
            [name] => jack
            [other_ids] => Array
                (
                    [b_id] => 1253
                )

        )

    [1] => Array
        (
            [_date] => 2020-10-11
            [_number] => 2
            [_order] => 2
            [name] => joey
            [other_ids] => Array
                (
                    [b_id] => 1433
                )

        )

)

and the $second_array:

Array
(
    [0] => Array
        (
            [date] => 2019-10-16
            [number] => 1
            [order] => 1
            [name] => jack
            [last_name] => foobar
            [other_ids] => Array
                (
                    [b_id] => 1253
                )

        )

    [1] => Array
        (
            [date] => 2020-10-11
            [number] => 2
            [order] => 2
            [name] => joey
            [last_name] => foobar
            [other_ids] => Array
                (
                    [b_id] => 1433
                )

        )
    [2] => Array
        (
            [date] => 2020-10-28
            [number] => 3
            [order] => 3
            [name] => tom
            [last_name] => foobar
            [other_ids] => Array
                (
                    [b_id] => 1593
                )

        )

)

they are very similar but they came from different api's and they are different in numbers and also some key names.

so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.

so is this possible to do?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
            'name' => 'jack','other_ids' => ['b_id' => 1253]
          ],
          ['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
                'name' => 'joey','other_ids' => ['b_id' => 1433]
          ]
];

$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
        'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
          ],
          [ 'date' => '2019-10-11','number' => '2','order' => '2',
        'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
         ],
         [ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
        'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
         ],
];

// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
    $arr2new[$a['number']] = $a;
}

foreach ($arr1 as $a) {
    if ( array_key_exists($a['_number'], $arr2new) && 
        $a['_order'] == $arr2new[$a['_order']]['order'] )  
    {
        $merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
    }
}
print_r($merged);

RESULT

Array
(
    [0] => Array (
            [name] => jack
            [other_ids] => Array
                (
                    [b_id] => 1253
                )
        )
    [1] => Array (
            [name] => joey
            [other_ids] => Array
                (
                    [b_id] => 1433
                )
        )
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...