Jump to content


Photo

using count function on a session array variable [SOLVED]


  • Please log in to reply
14 replies to this topic

#1 phpbenny

phpbenny
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 23 September 2006 - 08:56 PM

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

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 23 September 2006 - 09:00 PM

I believe in php V5 session_register is not that good
I name sessions the following way

$_SESSION["a"] = $something
Tell me the problem, I will try tell you the solution

#3 phpbenny

phpbenny
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 23 September 2006 - 09:03 PM

Hi again

And that is exactly what I have done if you look at the "after" example :-)

Best regards

Benny

#4 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 23 September 2006 - 10:08 PM

looking at your example

session_register("a");


As i said
session_register does not work

Try
$_SESSION["a"] = $value;
NOT
session_register("a");
Tell me the problem, I will try tell you the solution

#5 phpbenny

phpbenny
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 23 September 2006 - 10:29 PM

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


#6 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 23 September 2006 - 10:34 PM

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;
Tell me the problem, I will try tell you the solution

#7 phpbenny

phpbenny
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 24 September 2006 - 06:51 AM

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


#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 September 2006 - 07:03 AM

$_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']) ;

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 24 September 2006 - 07:05 AM

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



You're trying to count the size of a mult-dimensionl array. count works only for 1D array.
$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;

Also, count($_SESSION['kurv']) ; will just count how rows there are, not how many items are in the array.

#10 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 24 September 2006 - 07:29 AM

try
<?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"]);
?>


#11 phpbenny

phpbenny
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 24 September 2006 - 07:52 AM

Hi again

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

http://www.php.net/m...ction.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

#12 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 24 September 2006 - 08:24 AM

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!

#13 phpbenny

phpbenny
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 24 September 2006 - 08:29 AM

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

#14 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 24 September 2006 - 08:37 AM

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.

#15 phpbenny

phpbenny
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 24 September 2006 - 09:04 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users