Jump to content

Archived

This topic is now archived and is closed to further replies.

phpbenny

using count function on a session array variable [SOLVED]

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

Share this post


Link to post
Share on other sites
I believe in php V5 session_register is not that good
I name sessions the following way

$_SESSION["a"] = $something

Share this post


Link to post
Share on other sites
looking at your example
[quote]
session_register("a");
[/quote]

As i said
session_register does not work

Try
$_SESSION["a"] = $value;
NOT
session_register("a");

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
you have $_SESSION["a"]
Whats that
$_SESSION["a"] = $value

using
$_SESSION["a"];
you have named a bucket a
You now need to fill the bucket with something, using =
$_SESSION["a"] = $a;

Share this post


Link to post
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

Share this post


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

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Hi

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

Benny

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.