1

i have one sql query like this

SELECT wp_woocommerce_order_items.order_item_name, wp_woocommerce_order_itemmeta.meta_value FROM wp_woocommerce_order_items INNER JOIN wp_woocommerce_order_itemmeta ON wp_woocommerce_order_items.order_item_id=wp_woocommerce_order_itemmeta.order_item_id WHERE wp_woocommerce_order_items.order_item_type = 'line_item' AND wp_woocommerce_order_itemmeta.meta_key='_qty' AND wp_woocommerce_order_items.order_id=1477

Now this query gives me two results like this

order_item_name                     meta_value
Handicap Accessible Porta Potty         1
Sani Stand Handwashing Station          1

and i want to push this into array with key.

i tried this code for push

while ($row2 = mysql_fetch_array($item_name)) {
    $order_items['name'] = $row2['order_item_name'];
    $order_items['quantity'] = $row2['meta_value'];
}

but above code push only one element. Please help

2
  • 2
    You're not pushing anything there, you're assigning. Try actually writing some code with array_push (or, more simply, []) and see if by some miracle writing the right code makes it work :p Commented Jun 13, 2014 at 10:16
  • you have to use multidimensional array for this Commented Jun 13, 2014 at 10:17

2 Answers 2

2

You can do one of two things:

Make each element of your array into another array like this:

while ($row2 = mysql_fetch_array($item_name)) {
    $order_items[] = array('name' => $row2['order_item_name'],'quantity' => $row2['meta_value']);
}

Or simply make it a two dimensional array pushing each value into a numeric field:

while ($row2 = mysql_fetch_array($item_name)) {
    $order_items['name'][] = $row2['order_item_name'];
    $order_items['quantity'][] = $row2['meta_value'];
}

The first method will result in the following structure:

array(
[0] = array(
    name => name1,
    quantity => qty1),
[1] = array(
    name => name2,
    quantity => qty2)
)

While the second method will give you something like this:

array(
[name] = array(
    [0] => name1,
    [1] => name2
    ),
[quantity] = array(
    [0] => qty1,
    [1] => qty2
    )
);

It really depends on what you want to do with it later and how.

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

Comments

1

First, write the query like this so that the field mapping is already done in the SQL result set:

SELECT wp_woocommerce_order_items.order_item_name name, wp_woocommerce_order_itemmeta.meta_value quantity
FROM wp_woocommerce_order_items 
INNER JOIN wp_woocommerce_order_itemmeta ON wp_woocommerce_order_items.order_item_id=wp_woocommerce_order_itemmeta.order_item_id 
WHERE wp_woocommerce_order_items.order_item_type = 'line_item' AND wp_woocommerce_order_itemmeta.meta_key='_qty' AND wp_woocommerce_order_items.order_id=1477

Then, build the array:

$values = [];
while (($row = mysql_fetch_assoc($item_name)) !== false) {
    $values[] = $row;
}

If you want to build a key-value array (in which case the field mapping is less important):

$values = [];
while (($row = mysql_fetch_assoc($item_name)) !== false) {
    $values[$row['name']] = $row['quantity'];
}

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.