0

I have this multidimensional associative array:

$store = array(
array("id" => 13,"store_id" => 4,"name" => "trumpet","type" => "trumpet567"),
array("id" => 15,"store_id" => 3,"name" => "piano","type" => "piano689"),
array("id" => 33,"store_id" => 1,"name" => "flute","type" => "flute267"),
array("id" => 77,"store_id" => 3,"name" => "violin","type" => "violin324"),
array("id" => 78,"store_id" => 2,"name" => "guitar","type" => "guitar364"),
array("id" => 91,"store_id" => 3,"name" => "accordion","type" => "accordion763"));

and a variable:

$instrument="guitar";

I need to get store_id for $instrument. I've tried so many things but I've got no solution :(

1
  • Why don't you just use a database ? Commented Feb 18, 2013 at 19:44

1 Answer 1

4
$instrument = 'guitar';
$storeId = null;

foreach ($store as $row) {
    if ($row['name'] == $instrument) {
        $storeId = $row['store_id'];
        break;
    }
}

echo $storeId; // Will echo 2.
Sign up to request clarification or add additional context in comments.

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.