A. D.
A. D.

Reputation: 758

Search multi-dimensional array and return the parent key

I have a multi-dimensional array $games_array that looks like this:

<?php

$games_array = array(
  "game-one" => array(
    "name" => "Game One",
    "download_id" => "gameone",
    "file" => "./games/files/Game One.zip"
  ),
  "game-two" => array(
    "name" => "Game Two",
    "download_id" => "gametwo",
    "file" => "./games/files/Game Two.zip"
  )
);

?>

For example, to access the first game's name, I'd use $games_array["game-one"]["name"] which works fine.

Okay, now to the problem: I have a value, for example gameone, that corresponds to download_id (which is a key that every game in $games_array has).

Now I want to find out the key, in this example game-one or game-two, of the array that contains this value for the key download_id. That works.

What I do in the code below is iterate over $games_array and search each game for the value (in the code below gameone). If it's found, the key for that value is returned.

The next thing I do (if ($key_found) { ...) is to try and find out the key file's value by using the array in which the value for which I originally searched was found, and save it in $file.

Unfortunately $file is always empty and I don't know why.

<?php

$key = "";
$key_found = false;
$search_for_value = "gameone"; // search for game's download id in array
$file = "";

foreach($games_array as $game_id => $game_data) {
  $key = array_search($search_for_value, $game_data);
  echo "Searching for value <b>" . $search_for_value . "</b> in sub-array <b>" . $game_id . "</b>...<br />";
  if ($key === FALSE) {
    echo "Search returned FALSE<br /><br />";
  } else if ($key === NULL) {
    echo "Search returned NULL<br /><br />";
  } else {
    echo "\$key <b>" . $key . "</b> found! <br /><br />";
    $key_found = true;
  }
  if ($key_found) {
    // Key "download_id" found. Now search the parent array for the found key and use the
    // returned result as the new key to access the "file" value in the found game's id in $games_array
    $file = $games_array[array_search($key, $game_id)]["file"];
    echo "The key <b>" . $key . "</b> was found.<br/>";
    echo "\$file = " . $file . "<br />";
    echo "Exiting loop.<br /><br />";
    break;
  }
}

$file = $games_array[$games_data]["file"];
echo "Checking if the file \"" . $file . "\" exists...<br />";
echo (file_exists($file) ? "File \"" . $file . "\" exists." : "File \"" . $file . "\" does not exist.");

?>

I hope you understand my problem and can help me. I'd appreciate it very much... I'm really stuck here.

Upvotes: 1

Views: 1559

Answers (1)

Erik
Erik

Reputation: 1057

If you already know that you'll be searching in download_id, then you are making this code far more complicated than it needs to be. I'm not sure if there's a real answer to your question other than trying a completely different approach.

Instead of using all these array_search calls, you can directly check the value of the column you know you're looking for, like this:

  foreach( $games_array as $game_id => $game_data ) {
    if( $game_data["download_id"] == $search_for_value ) {
      $file = $game_data["file"];
      break;
    }
  }

Upvotes: 2

Related Questions