-3

For instance, in the following code, how to get the position -order- of a given element inside the array:

<?php
$cars=array("Volvo","BMW","Toyota","Tesla","Volkswagen");
//for Volvo, an echo statement should return 1
//for BMW, an echo statement should return 2
//for Toyota, an echo statement should return 3 ... and so on ...
?>

Update: After receiving some useful contributions regarding the implementation of search_array(), I was wondering if I can apply the same for arrays contained inside another array. vardump() for a multidimensional array shows me the following:

array (size=3)
  'home' => 
    array (size=6)
      'label' => 
        object(Magento\Framework\Phrase)[5814]
          private 'text' => string 'Home' (length=4)
          private 'arguments' => 
            array (size=0)
              ...
      'title' => 
        object(Magento\Framework\Phrase)[5815]
          private 'text' => string 'Go to Home Page' (length=15)
          private 'arguments' => 
            array (size=0)
              ...
      'link' => string 'http://example.com/example.html' (length=23)
      'first' => boolean true
      'last' => null
      'readonly' => null
  'category4' => 
    array (size=6)
      'label' => string 'Transport' (length=9)
      'link' => string 'http://example.com/example.html' (length=23)
      'title' => null
      'first' => null
      'last' => null
      'readonly' => null
  'category686' => 
    array (size=6)
      'label' => string 'Transport' (length=15)
      'link' => string '' (length=0)
      'title' => null
      'first' => null
      'last' => boolean true
      'readonly' => null

How to get in this case the position of category4 in regard to the array of size=3?

Eduardo
  • 53
  • 8

3 Answers3

4

array_search() will let you find the position of an element or it returns FALSE if the element was not found. Read more about it at http://php.net/manual/en/function.array-search.php

From the documentation, this is what can be returned from this function:

Return Values

Returns the key for needle if it is found in the array, FALSE otherwise.

And here is an example:

<?php
$cars=array("Volvo","BMW","Toyota","Tesla","Volkswagen");
//for Volvo, an echo statement should return 1
//for BMW, an echo statement should return 2
//for Toyota, an echo statement should return 3 ... and so on ...

$volvoPosition = array_search("Volvo", $cars);

if ($volvoPosition !== false) {
  // Volvo position was found at index/position 0 of the array.
  print $volvoPosition; // This gives the value 0
} else {
  // "Volvo" was never found in the array.
}
?>
asiby
  • 3,229
  • 29
  • 32
  • This does not return the output requested by OP – Andreas Mar 05 '19 at 18:54
  • so add 1 to the call – dmikester1 Mar 05 '19 at 18:55
  • 1
    Unless the return is false or a string – Andreas Mar 05 '19 at 18:56
  • @Andreas you need to read the documentation using the link I have provided. This is the function that returns the position of an element in an array. If Eduardo wants to decide that arrays start at 1, than so be it. I am sure that at this point he know how to increment a variable in PHP. Teach a man how to fish. I am showing him the full range of what he can do using array_search. Oh, and I am not sure where you got your information from, but this function can never return a string. – asiby Mar 05 '19 at 19:02
  • 1
    @asiby _but this function can never return a string_ - Ever heard about associative arrays? – Alon Eitan Mar 05 '19 at 19:06
  • So you are completely ignoring the @Eduardo's example then. How is the $cars array an associative array? Everything I said was in the context of this question. – asiby Mar 05 '19 at 19:10
  • OK, but since, as you said, that you're teaching a man how to fish, then you need to be clear about that :) After your edit this answer is good enough for my upvote – Alon Eitan Mar 05 '19 at 19:13
  • 1
    Ok. Sorry for the confusion then. I should have added an example at first. I the example, I have purposely avoided incrementing the result. Because if he intends to your that result to grab stuff from the array, then it won't work. Check this out https://i.imgur.com/VRSkSGd.jpg LOL – asiby Mar 05 '19 at 19:17
  • Array_search returns strings if the array is associative as Alon says. In my opinion there are better ways to get the output OP wants than to add one to the return of array_search. But since this is a obvious duplicate and OP has done no research at all then I'm not going to waste myself – Andreas Mar 05 '19 at 19:22
  • Hi; many thanks for your contributions; is array_search() also applicable if i had an array containing three arrays, and i wanted to know the position of a given sub array within the main array? – Eduardo Mar 05 '19 at 19:41
0

As the example is very simple, meaning, is just an array of strings, then you may use array_flip, which is going to return an array that flips the keys with the values, so we can do:

$cars = ["Volvo","BMW","Toyota","Tesla","Volkswagen"];
$flipped = array_flip($cars);
//for Volvo, => int(0)
var_dump($flipped['Volvo']);
//for BMW, => int(1)
var_dump($flipped['BMW']);

Also remember that the array start with 0 and not with 1, then the index of "Volvo" is 0 and not 1.

Alfchee
  • 345
  • 1
  • 5
  • 14
0

Yes you can use array_search(), but array_search() returns the index of the element which is start from 0, so you can do like below,

this is your array

$cars=array("Volvo","BMW","Toyota","Tesla","Volkswagen");
echo (array_search("Volvo",$cars)) + 1; //you can getting 1 as output