1

I have a multidimensional array like this which I converted from JSON:

Array (

    [1] => Array (
        [name] => Test
        [id]   => [1]
    )

    [2] => Array (
        [name] => Hello
        [id]   => [2]
    )
)

How can I return the value of id if name is equal to the one the user provided? (e.g if the user typed "Test", I want it to return "1")

Edit: Here's the code that works if anyone wants it:

$array = json_decode(file_get_contents("json.json"), true);
foreach($array as $item) {
    if($item["name"] == "Test")
        echo $item["id"];
}
1
  • and what have you tried? Commented Feb 25, 2014 at 12:25

5 Answers 5

1

The classical solution is to simply iterate over the array with foreach and check the name of each row. When it matches your search term you have found the id you are looking for, so break to stop searching and do something with that value.

If you are using PHP 5.5, a convenient solution that works well with less-than-huge data sets would be to use array_column:

$indexed = array_column($data, 'id', 'name');
echo $indexed['Test']; // 1
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this function

function searchObject($value,$index,$array) {
    foreach ($array as $key => $val) {
       if ($val[$index] === $value)
            return $val;
    }
    return null;
}

$MyObject= searchObject("Hello","name",$MyArray);
$id = $MyObject["id"];

Comments

0

You can do it manually like, in some function:

   function find($items, $something){
       foreach($items as $item)
       {
          if ($item["name"] === $something)
             return $item["id"];
       }
       return false;
    }

Comments

0

here is the solution

$count = count($array);
$name = $_POST['name']; //the name which user provided
for($i=1;$i<=$count;$i++)
{
  if($array[$i]['name']==$name)
  {
   echo $i;
   break;
  }
}

enjoy

7 Comments

Using for instead of foreach is almost always a bad idea. In this case it makes the code unnecessarily brittle: if the input changes slightly it will stop working (or worse, working correctly all of the time).
yeah you are right .. but I like to give the answers which are easy to understant for the asker ... @Jon
@NishantSolanki Well, I've never understood for loops :P The output: Unexpected $end on line 14
@Will ..there is a problem in your array structure... [id]=>[2] ... the value should not be in square brackets.. it should be in commas ..
@NishantSolanki: I don't see how for is easier to understand than foreach. The manual itself says "The foreach construct provides an easy way to iterate over arrays."
|
0

Try this:

$name = "Test";

foreach($your_array as $arr){
  if($arr['name'] == $name){
     echo $arr['id'];
  }
}  

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.