skip to Main Content

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(MagentoFrameworkPhrase)[5814]
          private 'text' => string 'Home' (length=4)
          private 'arguments' => 
            array (size=0)
              ...
      'title' => 
        object(MagentoFrameworkPhrase)[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?

3

Answers


  1. 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.
    }
    ?>
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search