phpbenny Posted September 23, 2006 Share Posted September 23, 2006 HiI have the following problem after upgrading from PHP 4 to 5.Before I had (shown in a short example):$a[0]=1;$a[1]=3;$a[2]=4;session_register("a");$items = count($a);// result 3After:$a[0]=1;$a[1]=3;$a[2]=4;$_SESSION["a"];$items = count($_SESSION["a"]);// result 0?Anyone know why its not possible to count this array? I have tested that it is an array and the array contains items?Best regardsBenny Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 23, 2006 Share Posted September 23, 2006 I believe in php V5 session_register is not that goodI name sessions the following way$_SESSION["a"] = $something Quote Link to comment Share on other sites More sharing options...
phpbenny Posted September 23, 2006 Author Share Posted September 23, 2006 Hi againAnd that is exactly what I have done if you look at the "after" example :-)Best regardsBenny Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 23, 2006 Share Posted September 23, 2006 looking at your example[quote]session_register("a");[/quote]As i saidsession_register does not workTry$_SESSION["a"] = $value;NOTsession_register("a"); Quote Link to comment Share on other sites More sharing options...
phpbenny Posted September 23, 2006 Author Share Posted September 23, 2006 Hi againI'm sorry let me be more clear:This part worked fine in PHP 4--- "Before at PHP 4" ---Before I had (shown in a short example):$a[0]=1;$a[1]=3;$a[2]=4;session_register("a");$items = count($a);// result 3--- "Before finished" ------ "After moving to PHP 5" ---After:$a[0]=1;$a[1]=3;$a[2]=4;$_SESSION["a"];$items = count($_SESSION["a"]);// result 0?--- "End of PHP 5" ---Best regardsBenny Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 23, 2006 Share Posted September 23, 2006 you have $_SESSION["a"]Whats that$_SESSION["a"] = $valueusing$_SESSION["a"];you have named a bucket aYou now need to fill the bucket with something, using =$_SESSION["a"] = $a; Quote Link to comment Share on other sites More sharing options...
phpbenny Posted September 24, 2006 Author Share Posted September 24, 2006 Hi againThanks but unfortunately this was an example, not a good one I see:Heres a snippet from the real code (modified):$_SESSION['basket'] = array();$_SESSION["kurv[0][0]"] = 5;$_SESSION["kurv[0][1]"] = 3;$_SESSION["kurv[1][0]"] = 1;$_SESSION["kurv[2][3]"] = 25;If I now:echo count($_SESSION["kurv"]);I get the result 0?Best regardsBenny Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2006 Share Posted September 24, 2006 [code]$_SESSION['basket'] = array();$_SESSION['kurv'][0][0] = 5;$_SESSION['kurv'][0][1] = 3;$_SESSION['kurv'][1][0] = 1;$_SESSION['kurv'][2][3] = 25;echo count($_SESSION['kurv']) ;[/code] Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 24, 2006 Share Posted September 24, 2006 [quote author=phpbenny link=topic=109219.msg440239#msg440239 date=1159080707]Hi againThanks but unfortunately this was an example, not a good one I see:Heres a snippet from the real code (modified):$_SESSION['basket'] = array();$_SESSION["kurv[0][0]"] = 5;$_SESSION["kurv[0][1]"] = 3;$_SESSION["kurv[1][0]"] = 1;$_SESSION["kurv[2][3]"] = 25;If I now:echo count($_SESSION["kurv"]);I get the result 0?Best regardsBenny[/quote]You're trying to count the size of a mult-dimensionl array. count works only for 1D array.[code=php:0]$count = 0;$_SESSION["kurv"][0][0] = 5;$_SESSION["kurv"][0][1] = 3;$_SESSION["kurv"][1][0] = 1;$_SESSION["kurv"][2][3] = 25;foreach($_SESSION["kurv"] as $kurv_row){ foreach($kurv_row as $val) { echo $val . "<br />"; $count++; }}echo "Total items: " . $count;[/code]Also, count($_SESSION['kurv']) ; will just count how rows there are, not how many items are in the array. Quote Link to comment Share on other sites More sharing options...
sasa Posted September 24, 2006 Share Posted September 24, 2006 try[code]<?php$_SESSION["kurv"][0][0] = 5;$_SESSION["kurv"][0][1] = 3;$_SESSION["kurv"][1][0] = 1;$_SESSION["kurv"][2][3] = 25;function multi_count($a) { if (!isset($a)) return 0; if (is_array($a)) foreach ($a as $b) $count += multi_count($b); else $count = 1; return $count;}echo multi_count($_SESSION["kurv"]);?>[/code] Quote Link to comment Share on other sites More sharing options...
phpbenny Posted September 24, 2006 Author Share Posted September 24, 2006 Hi againThanks for the help, however according to the PHP manual is should work:http://www.php.net/manual/en/function.count.phpPlease notice this was working in PHP 4 but moving to PHP 5 I had to change the way session variables are set, instead of "session_register("$var")" it now reads "$_SESSION["$var"]".If I test the session variable by "isset" and "is_array" it validates as true, but using "count" results in 0.And it works fine as long as the variable is not a session variable, no problem.This works fine:<?$k[0][0]=23;$k[1][0]=19;$k[2][0]=6;echo count($k); // results in 3?>Best regardsBenny Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 24, 2006 Share Posted September 24, 2006 use multi_count if you want to count the items in a muti-array$k[0][0] = 5;$k[0][1] = 3;$k[1][0] = 1;$k[2][3] = 25;echo count($k); // results in 3 - not 4! Quote Link to comment Share on other sites More sharing options...
phpbenny Posted September 24, 2006 Author Share Posted September 24, 2006 HiThanks for your suggestion, but that is not what I need.My problem are using count on a session array variable produces the result 0, using count on a "normal" variable as my latest example works fine.Best regardsBenny Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 24, 2006 Share Posted September 24, 2006 The reason it did not work on your session variable is you declared it incorrectly.Incorrect:$_SESSION["kurv[0][0]"] = 5;$_SESSION["kurv[0][1]"] = 3;$_SESSION["kurv[1][0]"] = 1;$_SESSION["kurv[2][3]"] = 25;Correct:$_SESSION["kurv"][0][0] = 5;$_SESSION["kurv"][0][1] = 3;$_SESSION["kurv"][1][0] = 1;$_SESSION["kurv"][2][3] = 25;1D array:$kurv[]2D array:$kurv[][]Whether the array is a session varible or not, if you want to determine how many items, you need to use a function for counting how many items are in multi-dimensional array. count() only works for 1D array. Use multi_count, which sasa did. It takes the total of each items in a 1D array and combine them for the 2D array. Quote Link to comment Share on other sites More sharing options...
phpbenny Posted September 24, 2006 Author Share Posted September 24, 2006 Yeaaah perfect it was my value assignment to the $_SESSION that was wrong as you specified.Thanks alot I missed that totally ;DHowever the count function can also be used on multi dimensionel arrays, but it will only count the "first" array, which is exactly what I need :-)Also you have the option of specifiying a second option to count so it indeed counts all elements recursively.Again great I will be coming back to this board in the future. to help and be helped.Best regardsBenny Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.