0
Array
(
[page] => 1
[results] => Array
    (
        [0] => Array
            (
                [adult] => 
                [backdrop_path] => /9rZg1J6vMQoDVSgRyWcpJa8IAGy.jpg
                [id] => 680
                [original_title] => Pulp Fiction
                [release_date] => 1994-10-14
                [poster_path] => /dM2w364MScsjFf8pfMbaWUcWrR.jpg
                [popularity] => 6.4840769584183
                [title] => Pulp Fiction
                [vote_average] => 7.8
                [vote_count] => 942
            )

        [1] => Array
            (
                [adult] => 
                [backdrop_path] => 
                [id] => 109005
                [original_title] => Pulp Fiction Art
                [release_date] => 2005-01-01
                [poster_path] => /yqbnCy6YCc9VY8cnkHGEgiylXey.jpg
                [popularity] => 0.2
                [title] => Pulp Fiction Art
                [vote_average] => 0
                [vote_count] => 0
            )

    )

[total_pages] => 1
[total_results] => 2
)

So this is my multidimensional array that i need help with. So my problem is how to get [original_title] with foreach. Because i never done a multidimensional foreach can someone explain me would be grateful. Thanks

1

4 Answers 4

4

You only need one foreach...

foreach($array['results'] as $subArray){
    echo $subArray['original_title'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

And if you are unsure whether "results" will be in the array or not be sure to check with an isset before running the loop.
1

To answer your question, you would do something like this:

foreach($var['results'] as $result)
{
    echo $result['original_title'];
}

Based upon what you have posted, and assuming that the array you posted is in a variable called $var.

If you did have a situation where you had an array of arrays of arrays, for example, like this:

Array (
    Array(
        Array(
            "title" => "foo",
            "data" => "bar"
        ),
        ...
    ),
    ...
)

Then you would do a multi-dimensional foreach something like this:

foreach($var as $inner)
{
    foreach($inner as $innerInner)
    {
        echo $innerInner['title'];
    }
}

It's also worth noting that, because of how references work, it's generally a much better idea to use objects instead of arrays for storing data like this.

2 Comments

Keeping data in arrays is absolutely fine (even though using properties in PHP 5.4/5.5 can save memory) there is nothing about references here.
It's fine, but if you use objects then you can do some cool things that will potentially reduce the size of your code, and make it perform better and be easier to read. Of course, this is all subjective, but I don't think the suggestion that it might be something to look into is incorrect.
0

First of all your Multidimensional Array should look something like:

$ary = array(
  'page' => 1,
  'results' => array(
    array(
      'adult' => null,
      'backdrop_path' => '/9rZg1J6vMQoDVSgRyWcpJa8IAGy.jpg',
      'id' => 680,
      'original_title' => 'Pulp Fiction',
      'release_date' => '1994-10-14',
      'poster_path' => '/dM2w364MScsjFf8pfMbaWUcWrR.jpg',
      'popularity' => 6.4840769584183,
      'title' => 'Pulp Fiction',
      'vote_average' => 7.8,
      'vote_count' => 942
    ),
    array(
      'adult' => null, 
      'backdrop_path' => null,
      'id' => 109005,
      'original_title' => 'Pulp Fiction Art',
      'release_date' => '2005-01-01',
      'poster_path' => '/yqbnCy6YCc9VY8cnkHGEgiylXey.jpg',
      'popularity' => 0.2,
      'title' => 'Pulp Fiction Art',
      'vote_average' => 0,
      'vote_count' => 0
    )
  )
)

To just access 'original_title' it's $ary['results'][1]['original_title'];. To run a foreach loop on this it would look like:

foreach($ary as $value){
  if(is_array($value)){
    foreach($value as $val){
      foreach($val as $i => $v){
        //$i holds key
        //$v holds value
      }
    }
  }
}

Comments

0

Try this:

foreach($MyArray as $Name => $Value) {
    echo $Name.' = '.$Value.'<br />';
}

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.