1

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?

1
  • Because if $pos is 0 it is considered false. Commented May 31, 2019 at 9:24

3 Answers 3

2

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; 
Sign up to request clarification or add additional context in comments.

Comments

2

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
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.