Jump to content

Help counting an array


vmicchia

Recommended Posts

I have an array. When I print it it looks like this:

Array ( [1] => Array ( [Cushion] => Cushion 1 [Fabric] => f-111111 [FabricPrice] => 0 [Fill] => Fiber [button] => none [ContWelt] => none [ContWeltFabric] => none [Zipper] => N [Quantity] => 2 [WeltSize] => [sKU] => c-111111 [Edge] => Knife [Cap] => N [straps] => N [Hinged] => N [Type] => Boxed Button [Price] => 54.25 [Total] => 108.5 ) [2] => Array ( [Cushion] => Cushion 1 [Fabric] => f-111111 [FabricPrice] => 0 [Fill] => Fiber [button] => none [ContWelt] => none [ContWeltFabric] => none [Zipper] => N [Quantity] => 4 [WeltSize] => [sKU] => c-111111 [Edge] => Knife [Cap] => N [straps] => N [Hinged] => N [Type] => Boxed Button [Price] => 54.25 [Total] => 217 ) )

 

Now when I use count() I get a result of 01 when I should get 02 when I use count( ,1) I get a result of 381. I am Using this wrong? the array is stored in

$_SESSION['cushArray']

Link to comment
https://forums.phpfreaks.com/topic/227088-help-counting-an-array/
Share on other sites

No problem.

 

I have tried:

$count = count($_SESSION['cushArray'], 1);
echo $count;

result = 381

 

also

$count = count($_SESSION['cushArray']);
echo $count;

result = 21

 

$count = count($_SESSION['cushArray'], COUNT_RECURSIVE);

echo $count;

result = 38

Something about that seems odd. Can you post the output of this please? The <pre> formatting will make it much easier to see what the structure actually is.

 

echo '<pre>';
print_r($_SESSION['cushArray']);
echo '</pre>';

seemed odd to me too. but here it is:

Array

(

    [1] => Array

        (

            [Cushion] => Cushion 1

            [Fabric] => f-111111

            [FabricPrice] => 0

            [Fill] => Fiber

            [button] => none

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 2

            [WeltSize] =>

            [sKU] => c-111111

            [Edge] => Knife

            [Cap] => N

            [straps] => N

            [Hinged] => N

            [Type] => Boxed Button

            [Price] => 54.25

            [Total] => 108.5

        )

 

    [2] => Array

        (

            [Cushion] => Cushion 1

            [Fabric] => f-111111

            [FabricPrice] => 0

            [Fill] => Fiber

            [button] => none

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 4

            [WeltSize] =>

            [sKU] => c-111111

            [Edge] => Knife

            [Cap] => N

            [straps] => N

            [Hinged] => N

            [Type] => Boxed Button

            [Price] => 54.25

            [Total] => 217

        )

 

)

 

 

 

He is right. There is nothing wrong with that array.  Which leads me to believe that perhaps the session is being overwritten at some point.

Can you please post the entire code.  Everything on the entire page (minus sensitive data) so I can get a better look. Rule out the session

getting overwritten, or some other logic error in the way your structuring it.

<?php
echo '<pre>';
print_r($_SESSION['cushArray']);
echo '</pre>';
$count = count($_SESSION['cushArray'], 1);
echo $count;
$total = 0;
if(isset($_SESSION['cushArray'])){
$total = $total + count($_SESSION['cushArray'][1]['Cushion']);
//echo 'This is the count: '.$count;
}
if(isset($_SESSION['umbArray'])){
$total = $total + count($_SESSION['umbArray']['Umbrella']);
}
if(isset($_SESSION['sampleArray'])){
$total = $total + count($_SESSION['sampleArray']['SKU']);
}
if(isset($_SESSION['cordingArray'])){
$total = $total + count($_SESSION['cordingArray']['Cording']);
}
if(isset($_SESSION['fringeArray'])){
$total = $total + count($_SESSION['fringeArray']['Fringe']);
}
if(isset($_SESSION['fabricArray'])){
$total = $total + count($_SESSION['fabricArray']['Fabric']);
}
if(isset($_SESSION['pillowArray'])){
$total = $total + count($_SESSION['pillowArray']['Pillow']);
}
if(isset($_SESSION['otherArray'])){
$total = $total + count($_SESSION['otherArray']['Product']);
}
echo $total;

if($total != 0){
echo '<div class="carticon"><img src="images/fullcart.png" /></div>';
echo '<br />';
    echo	'<div class="cartstatus">'.$total.' items in cart.</div>';
}else{
echo '<div class="carticon"><img src="images/emptycart.png" /></div>';
echo '<br />';
    echo	'<div class="cartstatus">Cart is empty.</div>';
}
?>

 

I'm only concerned with the first if statement really because it's the only session with any variables at the moment but that is the code from the page.

Archived

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

×
×
  • 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.