muffinjello
muffinjello

Reputation: 158

Get the number of arrays in a two dimensional array

I'm currently using this feature from redux array for wordpress, and what it's letting my users do is to make an array, filled with a customizable amount of nested arrays (each nested array represents one slide.)

I'm not the best at php, and this is a pretty simple question, but, if I run them through something like foreach($mybigarray as $value), would the $value become each sub array, so I could print all of the values with the sub-arrays via this method:

foreach($mybigarray as $value){
  echo $value['1'];
  echo $value['2'];
}

If this is false, how would I go about running each sub-array (of an unknown amount) through an if statement. Any "best-practices" I should know about while doing this?

Thanks!

Output from example array:

Array ( 
  [0] => Array (
    [title] => Title #1
    [description] => Sub Title Text #1
    [url] => Link #1
    [sort] => 1
    [attachment_id] => 1461
    [image] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains.jpg
    [height] => 1200
    [width] => 1920
    [thumb] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains-150x150.jpg
  )[1] => Array (
    [title] => Title #2
    [description] => Sub Title Text #2
    [url] => Link #2
    [sort] => 2
    [attachment_id] => 1461
    [image] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains.jpg
    [height] => 1200
    [width] => 1920
    [thumb] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains-150x150.jpg
  )[2] => Array (
    [title] => Title #3
    [description] => Sub Title Text #3
    [url] => link #3
    [sort] => 3
    [attachment_id] => 1461
    [image] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains.jpg
    [height] => 1200
    [width] => 1920
    [thumb] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains-150x150.jpg
  )
)

Upvotes: 0

Views: 59

Answers (1)

Zaenille
Zaenille

Reputation: 1384

if I run them through something like foreach($mybigarray as $value), would the $value become each sub array

Yes, each $value would become the sub-array.

However, as you said, if it has a customizable amount of nested arrays (array in an array in an array...), you have to either write your own recursive multi-array reader, or check ones that are already there.

Try this one for instance : http://www.php.net//manual/en/class.recursivearrayiterator.php

By the way, if you meant customizable amount of nested arrays as in one giant array with lots of one level arrays, then foreach should do you justice.

Upvotes: 1

Related Questions