Jump to content

using count function on a session array variable [SOLVED]


phpbenny

Recommended Posts

Hi

I 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 3

After:

$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 regards

Benny
Link to comment
Share on other sites

Hi again

I'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 regards

Benny
Link to comment
Share on other sites

Hi again

Thanks 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 regards

Benny
Link to comment
Share on other sites

[quote author=phpbenny link=topic=109219.msg440239#msg440239 date=1159080707]
Hi again

Thanks 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 regards

Benny

[/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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

Hi again

Thanks for the help, however according to the PHP manual is should work:

http://www.php.net/manual/en/function.count.php

Please 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 regards

Benny
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Yeaaah perfect it was my value assignment to the $_SESSION that was wrong as you specified.

Thanks alot I missed that totally ;D

However 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 regards

Benny
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.