0

I have a PHP array, say,

$array = Array("Name1","Name2","Name3","Name4","Name5");

I want to find the position of these names in the array.

I want to return 0 for Name1 and 2 for Name3.

How can I do this?

1

3 Answers 3

1

Do you mean:


$key = array_search('Name1', $array);

Ref: array_search

Sign up to request clarification or add additional context in comments.

Comments

0
$pos = array_search("Name3", $array); 

Should be what you are looking for, note that to be safe, result checking should be using === (three equal signs) so that those returning 0 (if you are looking for thing in the first element) is also checked for type when you are comparing them in an if statement

if(array_search($name, $array) === 0)

Comments

0

Something like that:

<?php
   $array = Array("Name1", "Name2", "Name3", "Name4", "Name5");
   $searchValue = "Name1";
   $keys = array_keys($array, $searchValue);

   // test it
   print_r($keys);
?> 

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.