-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?

1
  • Did you try to search the array? Commented Mar 5, 2019 at 18:53

3 Answers 3

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.
}
?>
Sign up to request clarification or add additional context in comments.

10 Comments

This does not return the output requested by OP
so add 1 to the call
Unless the return is false or a string
@asiby but this function can never return a string - Ever heard about associative arrays?
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 i.imgur.com/VRSkSGd.jpg LOL
|
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.