0

I have an array with two values stored for each item, the first one called "ID" and the second one called "en". The ID values are unique integers and the en values are text.

Now I would like to echo a specific item from the array referring to it by its ID.

I tried the below but this goes by the index within the array so it gives me a different item than what I need.

Can someone tell me how I can echo e.g. the item with ID = 10 instead of the item with the array index = 10 (here it currently gives me Value11 instead of Value1) ?

My PHP:

<?php echo $arr[10]["en"]; ?>

The array:

Array ( 
        [0] => Array ([ID] => 10 [en] => Value1 ),
        [1] => Array ([ID] => 11 [en] => Value2 ),
        [2] => Array ([ID] => 12 [en] => Value3 ),
        // ...
        [10] => Array ([ID] => 20 [en] => Value11 ),
        // ...
      )

Many thanks in advance, Mike

2
  • is it always [0] contaion ID=10 or only for example? Commented Jun 15, 2015 at 18:39
  • Thanks - this is just an example. This changes all the time which is why I want to go by my own ID since this stays the same. Commented Jun 15, 2015 at 18:40

2 Answers 2

2

I think you want that when you pass any value it will check that where the value exist at id index of your multidimensional array and give it's key and it's en index value.

<?php

$array = Array ( 
        '0' => Array ('ID' => 10, 'en' => 'Value1' ),
        '1' => Array ('ID' => 11, 'en' => 'Value2' ),
        '2' => Array ('ID' => 12, 'en' => 'Value3' )
        );
function findKeyandValue($array,$value){

   foreach($array as $key=>$val){
    if($val['ID']== $value){
        echo 'key of the value pass='.$key.'and its en value is'.$val['en'].'<br>';
    }
}

}

findKeyandValue($array,10);
?>

Output:- https://eval.in/382010

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot for this as well ! This is great and works well but it seems to be very similar to the answer from splash58. Am I missing something here ?
iIt returns the key of given value and also the en value also.
Thanks for that ! This is very helpful but in this case I just need the en value. :)
it's simple remove $key from echo. and instead of echo put return
For some reason I couldn't get the other solution to work fully, perhaps due to the two dimensions in the array or I made a mistake as it always returned nothing but it worked with your suggestion so I accepted that one and voted splash's comments up. Thanks again !
2
function FindByID($arr, $id) {
  foreach($arr as $item) {
   if ($item['ID'] === $id) return $item['en'];
  } 
  return null;
}

echo FindByID($arr, 11);

14 Comments

@jQuery.PHP.Magento.com you make my code invalid with your else. You may try. Return null must be after foreach
Then pls add { after foreach and after end of foreach. Else it makes your code confusing.
@jQuery.PHP.Magento.com Strange practice to vote against the program to work
1) id is argument of function. You may use any variable or constant $a = 10; FindByID($arr, $a);
2) the same about array - write the name of array you use. It is it does not matter - the different or the same name
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.