Jump to content

How do I extract this multi-dimensional array ?


vincej

Recommended Posts

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'];
} ?>


Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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]
            );
    }
}

Link to comment
Share on other sites

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 !

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.