Home > Back-end >  php in_array between 2 arrays
php in_array between 2 arrays

Time:06-11

I am trying to determine which elements coincide in 2 arrays. The issue is that only the last element in my second array is selected instead of all 3. What am I missing here?

<?php
    
$containers = [
    0 => ['id'=>'1', 'name'=>'Peta'],
    1 => ['id'=>'2', 'name'=>'Epta'],
    3 => ['id'=>'3', 'name'=>'Fras'],
    4 => ['id'=>'4', 'name'=>'Maxs'],
    5 => ['id'=>'5', 'name'=>'Gtay'],
    6 => ['id'=>'6', 'name'=>'Prat'],
    7 => ['id'=>'7', 'name'=>'Drat'],
];

$invoices = [
    0 => ['id'=>'1', 'name'=>'Lebo'],
    1 => ['id'=>'3', 'name'=>'Efta'],
    2 => ['id'=>'4', 'name'=>'Gadr'],
];

foreach ($containers as $container) {
    
    foreach ($invoices as $invoice) {
    
        if (in_array($container['id'], $invoice)) {
            $selected = 'selected';
        } else {
            $selected = '';
        }
    
    }
    
    echo $container['name'].' -> '.$selected.'<br>';
    
} ?>

CodePudding user response:

Add "break" after found in array.

<?php

$containers = [
    0 => ['id' => '1', 'name' => 'Peta'],
    1 => ['id' => '2', 'name' => 'Epta'],
    3 => ['id' => '3', 'name' => 'Fras'],
    4 => ['id' => '4', 'name' => 'Maxs'],
    5 => ['id' => '5', 'name' => 'Gtay'],
    6 => ['id' => '6', 'name' => 'Prat'],
    7 => ['id' => '7', 'name' => 'Drat'],
];

$invoices = [
    0 => ['id' => '1', 'name' => 'Lebo'],
    1 => ['id' => '3', 'name' => 'Efta'],
    2 => ['id' => '4', 'name' => 'Gadr'],
];

foreach ($containers as $container) {

    foreach ($invoices as $invoice) {

        if (in_array($container['id'], $invoice)) {
            $selected = 'selected';
            break;
        } else {
            $selected = '';
        }
    }

    echo $container['name'] . ' -> ' . $selected . '<br>';
} ?>

CodePudding user response:

Seems the easiest solution was to create a new array:

foreach ($invoices as $invoice) {
        $compare_id[] = $invoice['id'];
    } 
    
    foreach ($containers as $container) {
        
        if (in_array($container['id'], $compare_id)) {
            $selected = 'selected';
        } else {
            $selected = '';
        }
        
        echo $container['name'].' -> '.$selected.'<br>';
        
    }

CodePudding user response:

$containers = [
    [ 'id' => '1', 'name' => 'Peta' ],
    [ 'id' => '2', 'name' => 'Epta' ],
    [ 'id' => '3', 'name' => 'Fras' ],
    [ 'id' => '4', 'name' => 'Maxs' ],
    [ 'id' => '5', 'name' => 'Gtay' ],
    [ 'id' => '6', 'name' => 'Prat' ],
    [ 'id' => '7', 'name' => 'Drat' ],
];

$invoices = [
    [ 'id' => '1', 'name' => 'Lebo' ],
    [ 'id' => '3', 'name' => 'Efta' ],
    [ 'id' => '4', 'name' => 'Gadr' ],
];

$result = array_filter($containers, fn($item) => in_array($item['id'], array_column($invoices, 'id')));

print_r($result);

Output:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Peta
        )

    [2] => Array
        (
            [id] => 3
            [name] => Fras
        )

    [3] => Array
        (
            [id] => 4
            [name] => Maxs
        )

)
  • Related