Virb
Virb

Reputation: 1578

Not getting the first value when using "array_search"

Here, I have two multi-dimensional array. As I want to find the value from first array if the value is there in second array or not.

First array:

$abc = array(
    array("fld_channel_selected_item_track_id" => 627905217),
    array("fld_channel_selected_item_track_id" => 616557954)
);

Second array:

$pqr = array(
    array(
        "fld_channel_item_track" => 627905217,
        "fld_channel_item_title" => "Tropical Fantasy"
    ),
    array(
        "fld_channel_item_track" => 616557954,
        "fld_channel_item_title" => "Bday Boys"
    )
);

So, I am searching if the first array value is there in second array value or not:

for($i = 0;$i < count($abc); $i++)
{   
    $pos = array_search($abc[$i]["fld_channel_selected_item_track_id"], array_column($pqr, 'fld_channel_item_track'));
    if($pos) 
    {   
        echo "<pre>";print_r($pqr[$pos]);
    }
}
exit;

The outout is:

<pre>Array
(
    [fld_channel_item_track] => 616557954
    [fld_channel_item_title] => Bday Boys
)

Here, I am not getting the first value in my output.

Where I am wrong in that?

Upvotes: 1

Views: 174

Answers (3)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38532

As already pointed by other answers that,

we know that array_search() returns index value so for first loop it returns 0 and for second it's 1 so when if($pos) is 0 it fails. But you can modify your exiting code with in_array() to achieve what you want. Let's try like below-

for($i = 0;$i < count($abc); $i++)
{ 
    $pos = in_array($abc[$i]["fld_channel_selected_item_track_id"], array_column($pqr, 'fld_channel_item_track'));
    if($pos) 
    {   
        echo "<pre>";print_r($pqr[$i]); // see the index I changed here
    }
}

DEMO: https://3v4l.org/HA9tA

Upvotes: 0

jimmy5312
jimmy5312

Reputation: 429

array_search() returns the position if it found, else will return FALSE

https://www.php.net/manual/en/function.array-search.php

Returns the key for needle if it is found in the array, FALSE otherwise.

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

For the first value, it is actually found, but the result is 0

$pos = 0

So it will fail the if...else check

if ($pos) {
   // $pos = 0 is falsy
}

You need to perform a more explicit comparison

if ($pos !== FALSE) {
    // code here
}

Upvotes: 2

dWinder
dWinder

Reputation: 11642

The problem is array_search (doc) return index -> in your case index 0 so if ($pos) fails...

Returns the key for needle if it is found in the array, FALSE otherwise.

Need to compare result to FALSE

By the way, this will be better way t achieve what you need:

$a = array_column($abc, 'fld_channel_selected_item_track_id');
$b = array_column($pqr, "fld_channel_item_title", "fld_channel_item_track");
foreach($a as $searchId)
    if (isset($b[$searchId])) echo $b[$searchId] . PHP_EOL; 

Upvotes: 2

Related Questions