1

I have the following array which gets populated with database values (except for 'PickItems' in this example):

$params = array(
    'DeliveryCompanyName' => $row['company_name'],
    'DeliveryAddress1' => $row['address1'],
    'DeliveryAddress2' => $row['address2'],
    'DeliveryCity' => $row['city'],
    'DeliveryCounty' => $row['province'],
    'DeliveryPostCode' => $row['postcode'],
    'DeliveryCountry' => $row['country'],
    'DeliveryPhone' => $row['phone'],
    'DeliveryContact' => $row['first_name'] . " " . $row['last_name'],
    'OutboundRef' => $row['order_number'],
    'PickItems' => array(
        array(
            'SKUNumber' => 'SKU',
            'Quantity' => '1',
            'Comments' => 'Comments'
        )
    ),
    'BranchID' => '',
    'CustRef' => $row['order_number']
);

Within this array, is an item called "PickItems" which contains an array of array's which are each seperate products ordered.

I am trying to wrap my head around using a while loop on another database query to populate these "PickItems".

Here is the code which I started off writing in order to loop through the database that contains the individual items for the order, and I need to put these into an array and put that array into the main array. The part I am struggling with is putting these values and keys into the array, I thought array_push was the way to go but another answer on another question says you can't do this.

if ($result2 = $mysqli->query("SELECT * FROM order_items WHERE client_id = {$client} AND main_order_id = {$row['platform_order_id']}")) {

    $Items = array();

    while($row2 = mysqli_fetch_assoc($result2)) {
        //Add the following values to the items array
        //'SKUNumber' => $row2['sku'],
        //'Quantity' => $row2['quantity'],
        //'Comments' => $row2['comments']
    }
}

I suspect that the main array needs to change to something like this? :

$params = array(
    'DeliveryCompanyName' => $row['company_name'],
    'DeliveryAddress1' => $row['address1'],
    'DeliveryAddress2' => $row['address2'],
    'DeliveryCity' => $row['city'],
    'DeliveryCounty' => $row['province'],
    'DeliveryPostCode' => $row['postcode'],
    'DeliveryCountry' => $row['country'],
    'DeliveryPhone' => $row['phone'],
    'DeliveryContact' => $row['first_name'] . " " . $row['last_name'],
    'OutboundRef' => $row['order_number'],
    'PickItems' => array($Items),
    'BranchID' => '',
    'CustRef' => $row['order_number']
);

2 Answers 2

2

You can simply push it in your while loop:

array_push($params['PickItems'], array(
    'SKUNumber' => $row2['sku'],
    'Quantity' => $row2['quantity'],
    'Comments' => $row2['comments']
));

Output

'PickItems' => array(
    array(
        'SKUNumber' => 'SKU',
        'Quantity' => '1',
        'Comments' => 'Comments'
    ),
    array(
        'SKUNumber' => 'SKU 2', // Value from $row2['sku']
        'Quantity' => '2', // Value from $row2['quantity']
        'Comments' => 'Comments2' // Value from $row2['comments']
    ),
),

For more information: http://php.net/manual/en/function.array-push.php

Sign up to request clarification or add additional context in comments.

2 Comments

i asked myself the same thing
Perfect! I got myself into a right muddle working this out. I did upvote you to bring it back to 0. Will mark as answer asap.
-1

To add to an array, inside an array, just do this!

For an associative key value array:

$params['PickItems']['whatever'] = $row2;

Or for a numeric array:

$params['PickItems'][] = $row2;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.