vincej Posted April 24, 2012 Share Posted April 24, 2012 Hi - I have an array which is created from a session variable I must have constructed my array wrong, as for the life of me I can not extract the individual values. I have tried innumerable $key=>$value combinations. I must be doing something stupid, maybe it is obvious, but not to me. I'll be darn grateful if you can help me ! Many Thanks MY array looks like this: Array ( [quantity] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 ) [name] => Array ( [0] => Ribs Large pack 20 pieces [1] => 25 Piece Bag [2] => Sirloin Steak [3] => 50 piece bag of Scalops [4] => Sirloin Steak ) [prodid] => Array ( [0] => 17 [1] => 28 [2] => 27 [3] => 25 [4] => 27 ) ) The CodeIgniter form which generated this array looks like this: <?php foreach ( $_SESSION['openorders'] as $key=>$value) { ?> <tr align="center"> <td width="30"><?php $data=array('name' =>'quantity[]','value'=>$value['quantity']); echo form_input($data); ?></td> <td><?php echo $value['prodid']; ?></td> <td><?php echo $value['name'];?></td> <td><?php echo $value['ordervalue']; ?></td> <td><?php echo $value['pricelb']; ?></td> <td align="right"><?php echo form_checkbox('delete','delete',FALSE); echo form_hidden('prodid[]', $value['prodid']); echo form_hidden('name[]', $value['name']); echo form_hidden('orderid', $value['orderid']); $totalordervalue += $value['ordervalue']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/ Share on other sites More sharing options...
MMDE Posted April 24, 2012 Share Posted April 24, 2012 Exactly what do you want it to do? Show us the array you start out with, and how you want to access, store/use the data. Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340032 Share on other sites More sharing options...
MadTechie Posted April 24, 2012 Share Posted April 24, 2012 It would be better to fix the construction of the array.. if not then <?php //update foreach ( $_SESSION['openorders']['name'] as $index=>$name) { //Add $quantity = $_SESSION['openorders']['quantity'][$index]; $name= $_SESSION['openorders']['name'][$index]; $prodid= $_SESSION['openorders']['prodid'][$index]; ?> //then update below <tr align="center"> <td width="30"><?php $data=array('name' =>'quantity[]','value'=>$value['quantity']); echo form_input($data); ?></td> <td><?php echo $prodid; ?></td> <td><?php echo $value['name'];?></td> <td><?php echo $value['ordervalue']; ?></td> <td><?php echo $value['pricelb']; ?></td> <td align="right"><?php echo form_checkbox('delete','delete',FALSE); echo form_hidden('prodid[]', $prodid); echo form_hidden('name[]', $name); echo form_hidden('orderid', $value['orderid']); $totalordervalue += $value['ordervalue']; } ?> Hope that help's Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340033 Share on other sites More sharing options...
vincej Posted April 24, 2012 Author Share Posted April 24, 2012 I take your advice on board regarding fixing the array. I'm still a student of PHP, so not too sure how to accomplish that. Beyond the HTML Form ( code above ) the POST variables are simply put into a new $_SESSION variable such that I can capture changes to quantities as well as eliminate any of the deleted products. I'm still learning so any advice you can give is very gratefully received ! [code] function confirmed_Order(){ $_SESSION['confirmed_order'] = array( 'quantity' => $_POST['quantity'], 'name' => $_POST['name'], 'prodid' => $_POST['prodid'] ); } [/code] Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340039 Share on other sites More sharing options...
vincej Posted April 24, 2012 Author Share Posted April 24, 2012 Thanks MadTechie - I don't think that I have explained my problem very well, as your proposed solution does not work. Let me try again: My session variable, $_SESSION['confirmed_order'] will create an array which looks like this: Array ( [quantity] => Array ( [0] => 1 [1] => 2 ) [name] => Array ( [0] => Ribs Large pack 20 pieces [1] => breasts ) [prodid] => Array ( [0] => 17 [1] => 101 ) ) To answer MMDE , I want to populate a table with the variables. I really am stuck, I don't seem able to extract the variables effectively, and the code that generates these variables is very simple. Perhaps my array is broken but I am unsure how to fix that. Here is the code which creates the array in the first place: function confirmed_Order(){ $_SESSION['confirmed_order'] = array( 'quantity' => $_POST['quantity'], 'name' => $_POST['name'], 'prodid' => $_POST['prodid'] ); } Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340048 Share on other sites More sharing options...
MadTechie Posted April 24, 2012 Share Posted April 24, 2012 So are you using $_SESSION['confirmed_order'] or $_SESSION['openorders'] ? from your code it would seam your using, $_SESSION['openorders'] but the confirmed_Order uses $_SESSION['confirmed_order'] using the same code as a previously posted (but using confirmed_order instead and with some example data) <?php //Example session session_start(); $_SESSION['confirmed_order']['quantity'] = Array(1,1,1,1,1); $_SESSION['confirmed_order']['name'] = Array(' Ribs Large pack 20 pieces','25 Piece Bag','Sirloin Steak','50 piece bag of Scalops','Sirloin Steak '); $_SESSION['confirmed_order']['prodid'] = Array(17,28,27,25,27); //update foreach ( $_SESSION['confirmed_order']['name'] as $index=>$name) { //Add $quantity = $_SESSION['confirmed_order']['quantity'][$index]; $name= $_SESSION['confirmed_order']['name'][$index]; $prodid= $_SESSION['confirmed_order']['prodid'][$index]; echo "name: $name\n"; echo "quantity: $quantity\n"; echo "prodid: $prodid\n"; echo "\n"; } name: Ribs Large pack 20 pieces quantity: 1 prodid: 17 name: 25 Piece Bag quantity: 1 prodid: 28 name: Sirloin Steak quantity: 1 prodid: 27 name: 50 piece bag of Scalops quantity: 1 prodid: 25 name: Sirloin Steak quantity: 1 prodid: 27 May I suggest changing the array build, try this instead confirmed_Order(); //Blar Blar Blar Blar Blar Blar foreach ($_SESSION['confirmed_order'] as $order) { echo "name: " . $order['name'] . "\n"; echo "quantity: " . $order['quantity'] . "\n"; echo "prodid: " . $order['prodid'] . "\n"; echo "\n"; } function confirmed_Order() { if(empty($_POST['name'])) return false; $_SESSION['confirmed_order'] = array(); //clear existing foreach(array_keys($_POST['name']) as $index){ $_SESSION['confirmed_order'][] = array( 'name' => $_POST['name'][$index], 'quantity' => $_POST['quantity'][$index], 'prodid' => $_POST['prodid'][$index] ); } } Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340087 Share on other sites More sharing options...
vincej Posted April 24, 2012 Author Share Posted April 24, 2012 HI MadTechie, THANKYOU VERY MUCH for your help. I'll give your code a try. Yes, perhaps I could have explained things better. What is going on is an 'open order' is reviewed by a customer. Products might be deleted or quantities changed. Then it is resubmitted - now it becomes a 'confirmed order' which goes to the warehouse for picking and where products need weighing. The product weights will be entered and again resubmitted creating yet another session 'final order' which will go back to the front desk where it will be again displayed in a table for payment. I don't want to be writing all these stages to the DB, hence sessions. also I could have 2 - 3 customers being processed at once. I haven't got to the 3rd stage yet - still stuck on stage 2 ! again - thank you .. i can see that it was a long time ago that you were a student of php ! cheers ! Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340155 Share on other sites More sharing options...
xyph Posted April 24, 2012 Share Posted April 24, 2012 Alternately, you can use the good ol for loop. <?php $array = array( 'quantity' => array(3,2,4,5), 'name' => array('Chicken','Cow','Pork','Lamb'), 'prodid' => array(15,37,23,19) ); for( $i = 0, $max = count($array['name']); $i < $max; $i++ ) { echo "We have {$array['quantity'][$i]} of {$array['name'][$i]}. "; echo "It has a product ID of {$array['prodid'][$i]}.<br>"; } ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340159 Share on other sites More sharing options...
vincej Posted April 24, 2012 Author Share Posted April 24, 2012 Hi Mad Techie ! It works Hooray ! Thank you very much. The only sad thing is I'm not sure why it works ! I'll have to try to take it apart and understand it, so can use it in the future ! It looks like nothing nothing I have seen before. Quote Link to comment https://forums.phpfreaks.com/topic/261514-how-do-i-extract-this-multi-dimensional-array/#findComment-1340186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.