0

How would I get an array position in php?

I have an array:

$arr = array("mp3", "wma");

and a for loop:

for($i = 0; $i < count($_FILES); $i++ ) {
{
    $_POST['extention_file'.$i] = pathinfo($_FILES[ 'file'.$i]['name'], PATHINFO_EXTENSION); // getting file extension here
    // i know this in java script: 
    //    flag_extFile = file.name.split(".").pop();
    //    flag_extFile = flag_extFile.toLowerCase();
    //    flag_extCheck = arr.indexOf(flag_extFile);
    // alert(flag_extCheck); we will get array index here.
    // how we can do the same functionality in php
}

How can I achieve this in php?

Edited: I need the result without using a foreach loop or any other loop. In JavaScript I could write arr.indexOf(flag_extFile);, I need the same thing in php.

4
  • What is expected result? Commented Jun 16, 2014 at 4:54
  • where do u want to use $arr? Commented Jun 16, 2014 at 4:56
  • 1
    how can i achieve this in php What does this refer to here? Commented Jun 16, 2014 at 4:57
  • if you can see the js code. the result will be if flag_extFile is mp3 then flag_extCheck value will be '0' Commented Jun 16, 2014 at 4:58

2 Answers 2

5

You can use array_search() to return the array key:

for($i = 0; $i < count($_FILES); $i++ ) {
    $ext = pathinfo($_FILES[ 'file'.$i]['name'], PATHINFO_EXTENSION);
    $_POST['extention_file'.$i] = $ext;
    echo 'Key index is: ' . array_search($ext, $arr);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here the solution

 $arr = array("mp3", "wma","cd");         

 echo array_search("cd",$arr);

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.