1

Function array_search works only with digits and not with variables eg.

 $key = array_search(2345632, $rozm);

But I need it to get the number from the variable like

$key = array_search($ro, $rozm);

But it doesn't work.

1
  • What do you mean by "it doesn't work" ? Getting any errors? Commented Feb 14, 2014 at 10:52

1 Answer 1

2

It actually works.. see here

<?php
$rozm=[23,54,55];
$ro=54;
echo $key = array_search($ro, $rozm); //"prints" 1

Demo

Why it didn't work for you ?

Your $ro variable must be a string.. so just cast it like this

$key = array_search(intval($ro), $rozm);
Sign up to request clarification or add additional context in comments.

1 Comment

There's no requirement that the first parameter must be a string. If there was such a requirement, then how did the first version print 1? array_search() doesn't care about the type of the argument unless you run it with strict parameter set to true (An exception - If needle is a string, the comparison is done in a case-sensitive manner. - but that's not the case here) :)

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.