1

I just want to obtain the key by searching with the value id_img=17. This is the array:

$array_test = array (
0  => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"),
1  => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"),
2  => array("id_img" => 22, "desciption" => "The last image", "author" => "John Doe"),
);

Thanks for your help.

4
  • So you want to obtain the 1 key in this example (id_img = 17) ? Commented Jul 13, 2013 at 23:00
  • 2
    When asking for help, you should at the same time explain what you tried in order to solve your problem and how it didn't work. Doing otherwise is against the ground rules of StackOverflow and likely to get your question downvoted and/or closed. Commented Jul 13, 2013 at 23:02
  • OK, I tried that: $position = array_search($id_img, $array_test['id_img']); but it doesn't work. Commented Jul 13, 2013 at 23:05
  • Welcome to StackOverflow. As Jon said, you should explain (in the question) what you've tried or at least which part of the documentation you didn't understand in order to get a real question. Community will be happy to help, but will never provide free code according to a specification. Commented Jul 13, 2013 at 23:07

4 Answers 4

1
function getKey($arr,$value){
  foreach($arr as $key=>$element) {
    if($element["id_img"] == $value)
      return $key;
  }
  return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it work very well, I though it was possible to use array_search function to avoid loop.
You're welcome. Just remember next time that SO is for coding help, not coding for you. Additionally, don't forget to accept the best answer to your question.
1

I like to do things without foreach or for loops if possible, out of purely personal preference.

Here's my go for this:

$array_test = array (
    0  => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"),
    1  => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"),
    2  => array("id_img" => 22, "desciption" => "The last image", "author" => "John Doe"),
);

$result = array_filter( $array_test, function( $value )
{
    return $value['id_img'] == 17 ? true : false;
});
$key = array_keys( $result )[0];

print_r( $key );

Instead of loops, I use array_filter() to get only those items in the array that match my rule ( as defined in the Closure's return statement ). Since I know I only have one ID with value of 17 I know I will end up with only one item in the $result array. Then I retrieve the first element from the array keys ( using array_keys( $result )[0] ) - That's the key which holds the id_img = 17 in the original array.

4 Comments

Interesting way without loop. That work, but I don't find how to pass the value 17 as a variable $id_img. I had to put it in global.
No! Please don't use global variables unless it is something you need across your application! It's very bad practice.:) Instead, you can import variables into your function by using use keyword:function( $value ) use ( $id_img ) { function body... }. I recommend that you take a look at php's documentation about anonymous functions if you are interested in this stuff. Anyway, from a performance point of view, using loops in your case would be more efficient: you can always break from a loop once you have found what you are looking for, but you cannot break from anonymous function.
Here's the documentation for anonymous functions: php.net/manual/en/functions.anonymous.php
Thanks for the link, I will test that way to learn more about anonymous function, that I didn't know. About my $id_img, I use it along the code, why in global. I wanted to put the code you purpose in a function because I use it in different part of my code. Anyway, I stopped the loop when the value is found like you suggest, so I limit the power use when hundreds pictures are in the process.
0
<?php
$found=false;
$searched=17;
foreach($array_test as $k=>$data)
 if($data['id_img']==$searched)
   $found=$key;

And you've got your key in $found var or false if it's not found

Comments

0
Try:

$array_test = array (
0  => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"),
1  => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"),
2  => array("id_img" => 22, "desciption" => "The last image", "author" => "John Doe"),
);

$result = null;

foreach($array_test as $key => $val )
{
  if( $val['id_img'] == 17 )
    $result = $key;

}
return $result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.