Click to See Complete Forum and Search --> : count rows foreach in inner array?


tjmcd
11-25-2007, 06:38 PM
Hey all,
You know I have RT'd the FM until I am blue in the face (maybe I need a new FM?) and poured over every tutorial I can find, but as is ALL-TOO-OFTEN the case neither the examples in the FM nor those in the tutorials are EVER a good analogy for the real world problem I am trying to solve.

Given the multidimensional array below, I can manage a count of the first level arrays as:
$section_count = count($colorOpts);
//then ...
for ( $section = 0; $section < $section_count; $section++)
{ bla bla bla } but please, how would I get a ROW count FOREACH of the third level arrays contained therein? <?php //colorOpts.php
$colorOpts=array(
//colOpts1
array( array( Title => "RGB"),
array( "Red",
"Green",
"Blue"
)
),
//colOpts2
array( array( Title => "CMYK"),
array( "Cyan",
"Magenta",
"Yellow",
"Black"
)
),
//colOpts3
array( array( Title => "Pastels"),
array( "Pink",
"Mauve",
"Lavender",
"Teal"
)
),

);
?>RGB_options_count would =3 , CMYK_options_count_ would =4 etc.
Of course, it is entirely possible that I have the syntax of the array all wrong to begin with so... ???:eek:

NogDog
11-25-2007, 08:03 PM
$counts = array();
foreach($colorOpts as $opt)
{
$counts[$opt[0]] = count($opt[1]);
}
// show result:
printf("<pre>%s</pre>\n", print_r($counts, 1));

However, I would probably simplify the array a bit by using the title as the key of the first array dimension:

$colorOpts=array(
"RGB" => array(
"Red",
"Green",
"Blue"
),
"CMYK" => array(
"Cyan",
"Magenta",
"Yellow",
"Black"
),
"Pastels" => array(
"Pink",
"Mauve",
"Lavender",
"Teal"
)
);

Then the counting would be modified to:

$counts = array();
foreach($colorOpts as $title => $opt)
$counts[$title] = count($opt);
printf("<pre>%s</pre>\n", print_r($counts, 1));

tjmcd
11-25-2007, 09:29 PM
Thank You NogDog!!!
I would be embarrassed to say how long I struggled with this.
Will play around with it both ways just to better my understanding.
Wait --- What's that? -- Ooh... a flicker of light... could it be?
Just a glimpse... a -- a fleeting glimmer of a clue? ;)

Greatly Appreciated.

tjmcd
11-26-2007, 07:56 AM
Well, sadly
your first suggestion (for my original array contsruct) throws "Warning: Illegal offset type" for this line:
" $counts[$opt[0]] = count($opt[1]); " which I really wanted to ask about anyway.
Only recently I learned about variable variables $$ but have never seen anything like this. (variable array???)
Please, can you explain the logic there?

NogDog
11-26-2007, 10:20 AM
Try this:

$counts[$opt[0]['Title']] = count($opt[1]);

Since the foreach is on the main array, on each iteration $opt will be the next level array within it. E.g.: on the first iteration, $opt will be equivalent to $colorOpts[0]. Since the title value would be $colorOpts[0][0]['Title'], within the foreach loop it could instead be referred to as $opt[0]['Title']. As my objective was to use that value as an array key for the $counts array, I am putting that array element reference in as the key. So....

$counts[$opt[0]['Title']] = count($opt[1]);
...is equivalent to...

$key = $opt[0]['Title'];
$counts[$key] = count($opt[1]);