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

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...