A
A
Alexander Valutin2019-08-26 11:29:58
Arrays
Alexander Valutin, 2019-08-26 11:29:58

How to create an identical array when a condition is met?

I go into $array and compare the id's of its elements with the elements of $myArray. How to create a new array, the same as $array, so that entries from $array get into it, provided that the id from $array matches the elements of myArray, and if there is no match, then such an entry from the $array array does not get into the new array ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ordec, 2019-08-26
@Ordec

$array = [
    0 => [
        'ts1' => '2019-08-15 21:00:14',
        'id' => 3288376327,
        'src_number' => 79311112174,
        'nnp_city_id' => 115125,
        'nnp_is_mob' => ' ',
        'dst_number' => 79260963990,
        'orig' => 1,
        'nnp_number_range_id' => 1598484,
        'price' => -0.17,
        'ts2' => 131,
        'cnt' => 1,
        'geo' => 'москва и московская область (mobile)',
        'tsf1' => '2019-08-15 21:00:14',
        'mktime' => 1565902814,
        'is_total' => ' ',
        'tsf2' => '00:02:1']
];

$array[] = [
    'ts1' => '2019-08-15 21:00:16',
    'id' => 3288376242,
    'src_number' => 79311110763,
    'nnp_city_id' => 115125,
    'nnp_is_mob' => ' ',
    'dst_number' => 79311110763,
    'orig' => ' ',
    'nnp_number_range_id' => 2100966,
    'price' => 0.00,
    'ts2' => 0,
    'cnt' => 1,
    'geo' => 'Москва и Московская область (Mobile)',
    'tsf1' => '2019-08-15 21:00:16',
    'mktime' => 1565902816,
    'is_total' =>' ' ,
    'tsf2' => '00:00:00'
];


$myArray = [
    1 => 3289870066,
    76 => 3289881012,
    151 => 3289882074,
    226 => 3289884874,
    301 => 3289892663,
    302 => 3288376242,
    303 => 3288376327 ];


function returnArray($myArray, $array){
    $newArray = [];
    for ($i = 0; $i < count($array); $i++){
        for ($j = 0; $j < count($myArray); $j++){
            if ($array[$i]['id'] === $myArray[$j]) $newArray[] = $array;
        }
    }
    return $newArray;
}

I didn't test it, but I need a nested loop here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question