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 ( ї ] => 1 ї1a ] => 1 їWhat is the capital city of England ] => 1 їLondon Cardiff Edinburgh Dublin ] => 1 ) Array ( ї ] => 1 ї2a ] => 1 їWhich of these countries do not belong to the Scandanavia? ] => 1 їSweden Poland Norway Denmark ] => 1 ) Array ( ї ] => 1 ї1b ] => 1 їLondon is located in which Country? ] => 1 ї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
(
ї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
)
)
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
(
їitem] =>
їquestionId] => 1a
їquestion] => What is the capital city of England?
їchoice] => London
Cardiff
Edinburgh
Dublin
)
Array
(
їitem] =>
їquestionId] => 2a
їquestion] => Which of these countries do not belong to the Scandanavia?
їchoice] => Sweden
Poland
Norway
Denmark
)
Array
(
їitem] =>
їquestionId] => 1b
їquestion] => London is located in which Country?
їchoice] => England
Scotland
Ireland
Isle of Man
)
it does appear that there is one more sublevel.