Asked  7 Months ago    Answers:  5   Viewed   34 times

If I have:

$array = array( 'one' =>'value', 'two' => 'value2' );

how do I get the string one back from $array[1] ?

 Answers

52

You don't. Your array doesn't have a key [1]. You could:

  • Make a new array, which contains the keys:

    $newArray = array_keys($array);
    echo $newArray[0];
    

    But the value "one" is at $newArray[0], not [1].
    A shortcut would be:

    echo current(array_keys($array));
    
  • Get the first key of the array:

     reset($array);
     echo key($array);
    
  • Get the key corresponding to the value "value":

    echo array_search('value', $array);
    

This all depends on what it is exactly you want to do. The fact is, [1] doesn't correspond to "one" any which way you turn it.

Wednesday, March 31, 2021
 
Maury
answered 7 Months ago
20

Without making use of loops.

<?php
    $myArrayInit = [1 => 'red', 30 => 'orange', 25 => 'velvet', 45 => 'pink']; //<-- Your actual array
    $offsetKey = 25; //<--- The offset you need to grab

    //Lets do the code....
    $n = array_keys($myArrayInit); //<---- Grab all the keys of your actual array and put in another array
    $count = array_search($offsetKey, $n); //<--- Returns the position of the offset from this array using search
    $new_arr = array_slice($myArrayInit, 0, $count + 1, true);//<--- Slice it with the 0 index as start and position+1 as the length parameter.
    print_r($new_arr);

Output :

Array
(
    [1] => red
    [30] => orange
    [25] => velvet
)
Wednesday, March 31, 2021
 
EnTee
answered 7 Months ago
36

The simplest and likely most efficient way to do this is by iterating the array that holds the sort order and creating a new, sorted array:

$sorted = array();

foreach ($tempArray as $order) {
  if (isset($myArray[$order])) {
    $sorted[$order] = $myArray[$order];
  }
}

print_r($sorted);

This works because associative arrays implicitly have an order of the order in which elements were added to the array.

See it working


EDIT

Any solution involving a sorting function will likely be much less efficient than this. This is because in order to do it you will need to use a function that takes a callback - this already has an implied overhead of the function call.

The sorting functions also work by comparing items, meaning that the complexity any of those solutions will be greater than that of this solution (the complexity of this is simply O(n)). Also, in order to derive the return value for the sorting function you would need to inspect the target array, finding the position of each of the keys being compared, for each comparison, adding even more complexity.

Wednesday, March 31, 2021
 
Akdeniz
answered 7 Months ago
77

I think you are looking for this:

function getIndex($name, $array){
    foreach($array as $key => $value){
        if(is_array($value) && $value['name'] == $name)
              return $key;
    }
    return null;
}
Saturday, May 29, 2021
 
subroutines
answered 5 Months ago
83

Can't you just do:

$resulting_array = $array2 + $array1;

?

Tuesday, June 22, 2021
 
juanrpozo
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :