Jump to content

Recommended Posts

I've tried a few different things, I'm getting more comfortable with arrays, but..

$_SESSION['cart'] = array($specialid => '1', 'mild steel');

All this will do is make my array like this:

Array

(

    [7] => 1

    [8] => mild steel

)

Somehow I want more values for [7] without creating 8, since I had to reserve all of the keys for ids. Trying to make it possible something like:

Array

(

    [7] => '1', 'mild steel', 'etc'

    [8] => '2', 'stainless steel'

)

Also I'm sure there might be a special way to go about getting the extra values, if something like this is possible? Possibly inside the foreach loop.

Sounds like you're after a multidimensional array. In essence, each element of the array is an array itself. Consider something like this:

 

$array[0] = array('foo','bar');
$array[1] = array('blah');
$array[2] = array(array(1,2,3),4,5);
echo '<pre>.print_r($array,1).'</pre>';

 

As you'll see by the last element, there's no reason why we're limited to 2 dimensions

Cool, tried out this:

$_SESSION['cart'] = array($specialid => array('1', 'mild steel'));

which easily gave me:

Array

(

    [7] => Array

        (

            [0] => 1

            [1] => mild steel

        )

 

)

Which, I'm gonna hope all I have to do is run two loops to grab all the information. Thanks

Is this method:

  foreach ($_SESSION['cart'] as $id => $array2){
    	print "item: $id<br />";
foreach ($array2 as $row => $item){

		if ($row == 0){
		print "Qty: $item<br />";
		}
		if ($row == 1){
		print "Material: $item<br />";
		}

}

  }

 

The best for getting and using the items? I don't think an if statement should be necessary, but I'm not entirely sure. This resulted with:

 

item: 7

Qty: 1

Material: mild steel

Using:

$_SESSION['cart'][$id][mildsteel] = array(qty => 1, size => 4.5);

 

I get this array:

Array
(
    [8] => Array
        (
            [mildsteel] => Array
                (
                    [qty] => 1
                    [size] => 4.5
                )

            [stainless] => Array
                (
                    [qty] => 1
                    [size] => 4.5
                )

        )

)

 

Now I am trying to control it so that I can display the cart correctly, which for this let's say there's a table displaying each item. For each material of each item I need to show a new row. The foreach I have below will not work correctly.

  foreach ($_SESSION['cart'] as $id){
  foreach ($id as $material){
  foreach ($material as $row){

print "ID: $id, Meterial: $material, Qty: $row[0], Size: $row[1]";

}

}

        }

 

Basically need to somehow use that array to do something like this:

 

ID: 8, Material: mildsteel, QTY: 1, Size, 4.5

ID: 8, Material: stainless, QTY: 1, Size, 4.5

 

and for it to be able to do this for every item ID and each different Material under each id. Hope this makes sense, and I'm still playing with it to see if I can control it.

 

I think I am getting closer with this:

 

  foreach ($_SESSION['cart'] as $id => $material){

foreach ($material as $row){

print "ID: $id, Meterial: $material, Qty: $row[0], Size: $row[1]";

}

    }

 

but still nope, that's showing:

ID: 8, Meterial: Array, Qty: , Size:

ID: 8, Meterial: Array, Qty: , Size:

Well I got this:

  foreach ($_SESSION['cart'] as $id => $array2){
  foreach ($array2 as $material => $array3){
  foreach ($array3 as $key => $key2){


    echo "$id $material $key $key2<br />";

    }


}
}

Which turns out to be:

 

8 mildsteel qty 1

8 mildsteel size 4.5

8 stainless qty 1

8 stainless size 4.5

 

Which is obviously not right, need it to do something like

 

8 mildsteel qty 1 size 4.5

8 stainless qty 1 size 4.5

 

Still trying to get it under control though, anyone?

Sigh, now I'm to this:

  foreach ($_SESSION['cart'] as $id => $array2){
  foreach ($array2 as $material => $array3){
      echo "$id $material ";

  foreach ($array3 as $key => $key2){

echo " $key2";

    }
echo "<br />";

}
}

 

which displays

 

8 mildsteel 1 4.5

8 stainless 1 4.5

 

but I obviously still can't control my quantity and size in to their own variables.. just a general $key2, not sure how to break it down better.

Now,

foreach ($_SESSION['cart'] as $id => $array2){
  foreach ($array2 as $material => $array3){
      echo "$id $material ";

  foreach ($array3 as $key => $key2){

if ($key == qty) {
echo "QUANTITY: $key2";
} else {
echo " $key: $key2";
}

    }
echo "<br />";

}
}

 

Results:

8 mildsteel QUANTITY: 1 size: 4.5

8 stainless QUANTITY: 1 size: 4.5

 

Though I think I should still have some sort of more control over it than I am having right now? I know SOMEONE has a solution to this!

 

 

 

edit: changed the inside foreach,

  foreach ($_SESSION['cart'] as $id => $array2){
  foreach ($array2 as $material => $array3){
      echo "$id $material ";

  foreach ($array3 as $key => $key2){

if ($key == qty) {
$qty = $key2;
}
if ($key == size) {
$size = $key2;
}

    }
echo "$size $qty<br />";

}
}

 

So that gives me a slight bit more flexibility but I am almost sure there has got to be a way to do this without using the if statements?

Assuming you're still working with your data like this:

 

Array
(
    [8] => Array
        (
            [mildsteel] => Array
                (
                    [qty] => 1
                    [size] => 4.5
                )

            [stainless] => Array
                (
                    [qty] => 1
                    [size] => 4.5
                )

        )

)

 

The following should work for you:

 

<?php
foreach($array as $id => $materials){ 
    foreach($materials as $material => $details){
        echo "ID: $id Material: $material ";
        foreach($details as $detail => $value){
            echo "$detail: $value";
        }
        echo '<br />';
    }
}
?>

That does work, however I need to be able to control the values a little better. For instance, the quantity needs to be able to go in to a textbox and the size will be displayed somewhere else. Is there a solid way for me to control this information better?

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.