Page 1 of 1

Multidimensional Arrays

Posted: Tue Oct 14, 2003 6:01 pm
by jbatty
I am trying to do some manipulation on this array but i cant seem to get it to work.

Code: Select all

Array ( ї0] => Array ( їitem] => їquestionId] => 1a їquestion] => What is the capital city of England їchoice] => London Cardiff Edinburgh Dublin ) ї1] => Array ( їitem] => їquestionId] => 2a їquestion] => Which of these countries do not belong to the Scandanavia? їchoice] => Sweden Poland Norway Denmark ) ї2] => Array ( їitem] => їquestionId] => 1b їquestion] => London is located in which Country? їchoice] => England Scotland Ireland Isle of Man ) )
just to check the array i used the PHP function

Code: Select all

<?php
print_r(array_count_values($arrayname));
?>
this is mean to count the number of elements in the array. I am expecting to get 3 as the number of elements but the result i am getting is
Array ( )
Is there seperate way to count multidimension arrays? Please help see what i am doing wrong. Any advice is appreciated.
Regards

Posted: Tue Oct 14, 2003 6:08 pm
by JAM
Perhaps because...

Code: Select all

Warning:  array_count_values(): Can only count STRING and INTEGER values!
You error_level is to low for debugging.

Edited:

Might help, if I understood you correct.

Code: Select all

// print_r the level2 values
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            print_r(array_count_values($v));
        }
    }

Posted: Sun Oct 19, 2003 9:03 am
by jbatty
Thanks Jam for your reply; I tried the code but i got the following

Code: Select all

Array ( &#1111; ] => 1 &#1111;1a ] => 1 &#1111;What is the capital city of England ] => 1 &#1111;London Cardiff Edinburgh Dublin ] => 1 ) Array ( &#1111; ] => 1 &#1111;2a ] => 1 &#1111;Which of these countries do not belong to the Scandanavia? ] => 1 &#1111;Sweden Poland Norway Denmark ] => 1 ) Array ( &#1111; ] => 1 &#1111;1b ] => 1 &#1111;London is located in which Country? ] => 1 &#1111;England Scotland Ireland Isle of Man ] => 1 )
I think the array has 3 levels. I tried to write another inner foreach loop. but i just got a blank page.

Posted: Sun Oct 19, 2003 9:20 am
by volka
much easier to see if the output preserves linebreaks and indents

Code: Select all

echo '<pre>'; print_r($arrayname); echo '</pre>';

Posted: Sun Oct 19, 2003 4:57 pm
by jbatty
thanks volka, using your suggestion i got the following.

Code: Select all

Array
(
    &#1111;0] =&gt; Array
        (
            &#1111;item] =&gt; 
            &#1111;questionId] =&gt; 1a
            &#1111;question] =&gt; What is the capital city of England?
            &#1111;choice] =&gt; London
   Cardiff
   Edinburgh
   Dublin
    )

    &#1111;1] =&gt; Array
        (
            &#1111;item] =&gt; 
            &#1111;questionId] =&gt; 2a
            &#1111;question] =&gt; Which of these countries do not belong to the Scandanavia?
            &#1111;choice] =&gt; Sweden
   Poland
   Norway
   Denmark
   
        )

    &#1111;2] =&gt; Array
        (
            &#1111;item] =&gt; 
            &#1111;questionId] =&gt; 1b
            &#1111;question] =&gt; London is located in which Country?
            &#1111;choice] =&gt; England
   Scotland
   Ireland
   Isle of Man
   
        )
)
when i use the following code to to assess the 2 level arrays,

Code: Select all

<?php
foreach ($myarray as $k => $v) { 
        if (is_array($v)) { 
		echo '<pre>'; print_r($v); echo '</pre>';

            //print_r(array_count_values($v)); 
        } 
    } 

?>
i got

Code: Select all

Array
(
    &#1111;item] =&gt; 
    &#1111;questionId] =&gt; 1a
    &#1111;question] =&gt; What is the capital city of England?
    &#1111;choice] =&gt; London
   Cardiff
   Edinburgh
   Dublin
)

Array
(
    &#1111;item] =&gt; 
    &#1111;questionId] =&gt; 2a
    &#1111;question] =&gt; Which of these countries do not belong to the Scandanavia?
    &#1111;choice] =&gt; Sweden
   Poland
   Norway
   Denmark
)

Array
(
    &#1111;item] =&gt; 
    &#1111;questionId] =&gt; 1b
    &#1111;question] =&gt; London is located in which Country?
    &#1111;choice] =&gt; England
   Scotland
   Ireland
   Isle of Man 
)
it does appear that there is one more sublevel.