Rob
Rob

Reputation: 13

array_search returns NULL/blank

I have such array

<?php
$a = array(
    "apple"=>"uniqid1",
    "apple"=>"uniqid2",
    "apple"=>"uniqid3",
    "bannana"=>"uniqid4"
    );

echo array_search("uniqid1", $a, true);

?>

When I search for "uniqid4" it returns "bannana" (all good)

But when I search for "uniqid1" it returns blank/NULL (I know there are duplicates - but array is as it is)

My question is :

How to search for "uniqid1" or "uniqid2" or "uniqid3" and get "apple" everytime?

based on first to answers:

If i reverse array:

$a=array(

"uniqid1"=>"apple",
"uniqid2"=>"apple",
"uniqid3"=>"apple",
"uniqid4"=>"bannana");

how to search uniqid3or uniqid2 or uniqid1

echo array_search("uniqid3",$a, true);

and get apple everytime?

Upvotes: 1

Views: 126

Answers (3)

user14929188
user14929188

Reputation:

As ArSeN said, you cannot use the same apple index multiple times. In doing so, PHP will understand that only the third exists, the first two will be ignored because they use the same key.

Maybe there is a way to make this work, and it would be by changing keys and values, then keys would become values.

The code would look like this:

<?php

$array = array(
    'uniqid1' => 'apple',
    'uniqid2' => 'apple',
    'uniqid3' => 'apple',
    'uniqid4' => 'bannana'
);

echo $array['uniqid1'];
echo '<br>';
echo $array['uniqid2'];
echo '<br>';
echo $array['uniqid3'];
echo '<br>';
echo $array['uniqid4'];

?>

Output

apple
apple
apple
bannana

As you can see, keys have become values, and it is now possible to obtain these values by indicating their index

Upvotes: 0

Raskul
Raskul

Reputation: 2209

this is your array

$a=array(
"apple"=>"uniqid1",
"apple"=>"uniqid2",
"apple"=>"uniqid3",
"bannana"=>"uniqid4");

it is exactly like this array:

$a=array(
"apple"=>"uniqid3",
"bannana"=>"uniqid4");

you are overwriting "apple" 2 more times. so it's logical if you search for uniqid1 and uniqid2 you will get null, but if you search for uniqid3 and uniqid4 you will get an answer.

after you defined an array, you give "apple" => "uniqid1" but in the next line, you overwrite it an give "apple" another value, "apple" => "uniqid2"

after that you give third value to "apple" again "apple" => "uniqid3"

so "apple" has this value: "uniqid3"

so "uniqid1" and "uniqid2" do not exist in your array any more.

Upvotes: 0

ArSeN
ArSeN

Reputation: 5248

You can not use the same index multiple times. Defining it multiple times will overwrite the value at that index, which leads to only the last value that is assigned (uniqid3 in this case) still remaining.

You can check this like so:

$a=array(

"apple"=>"uniqid1",
"apple"=>"uniqid2",
"apple"=>"uniqid3",
"bannana"=>"uniqid4");

var_dump($a);

And you will see there are actually only two entries:

array(2) {
  ["apple"]=>
  string(7) "uniqid3"
  ["bannana"]=>
  string(7) "uniqid4"
}

If you flip the values (use the values as keys and vice versa) this would work fine and you could search for apple then.

Upvotes: 0

Related Questions