0

I have an order form that creates 3 arrays:

  • Product IDs
  • Quantities
  • Line Total (quantity x product price)

I have a pivot table between Product and Sale named Product_Sale, that also features the columns quantity and line_total.

The first thing I am struggling to get working is to combine all three arrays into a sensible single array. The below is what I have at the moment:

    $keys = $request->product_id;
    $quantities = $request->quantity;
    $line_totals = $request->line_total;

    $order = array();

    foreach ($keys as $key => $id) {
            $order[$key] = array(
                'quantity'  => $quantities[$id],
                'line_total' => $line_totals[$id]
            );
    }

    print_r ($order);

I get an error here: undefined offset: 53

Any help very much appreciated - thank you!

===update:

var_dump of arrays:

PRODUCT IDS
array (size=51)
  10 => string '10' (length=2)
  5 => string '5' (length=1)
  53 => string '53' (length=2)
  15 => string '15' (length=2)
  19 => string '19' (length=2)
  39 => string '39' (length=2)
  27 => string '27' (length=2)
  29 => string '29' (length=2)
  48 => string '48' (length=2)
  30 => string '30' (length=2)
  23 => string '23' (length=2)
  43 => string '43' (length=2)
  16 => string '16' (length=2)
  14 => string '14' (length=2)
  36 => string '36' (length=2)
  51 => string '51' (length=2)
  8 => string '8' (length=1)
  25 => string '25' (length=2)
  28 => string '28' (length=2)
  24 => string '24' (length=2)
  22 => string '22' (length=2)
  38 => string '38' (length=2)
  26 => string '26' (length=2)
  49 => string '49' (length=2)
  41 => string '41' (length=2)
  12 => string '12' (length=2)
  31 => string '31' (length=2)
  20 => string '20' (length=2)
  4 => string '4' (length=1)
  6 => string '6' (length=1)
  17 => string '17' (length=2)
  47 => string '47' (length=2)
  44 => string '44' (length=2)
  50 => string '50' (length=2)
  34 => string '34' (length=2)
  7 => string '7' (length=1)
  35 => string '35' (length=2)
  46 => string '46' (length=2)
  40 => string '40' (length=2)
  11 => string '11' (length=2)
  33 => string '33' (length=2)
  21 => string '21' (length=2)
  45 => string '45' (length=2)
  18 => string '18' (length=2)
  42 => string '42' (length=2)
  52 => string '52' (length=2)
  37 => string '37' (length=2)
  32 => string '32' (length=2)
  3 => string '3' (length=1)
  9 => string '9' (length=1)
  13 => string '13' (length=2)

QUANTITIES
array (size=51)
  0 => string '0' (length=1)
  1 => string '0' (length=1)
  2 => string '0' (length=1)
  3 => string '0' (length=1)
  4 => string '0' (length=1)
  5 => string '0' (length=1)
  6 => string '0' (length=1)
  7 => string '0' (length=1)
  8 => string '0' (length=1)
  9 => string '2' (length=1)
  10 => string '2' (length=1)
  11 => string '0' (length=1)
  12 => string '0' (length=1)
  13 => string '0' (length=1)
  14 => string '0' (length=1)
  15 => string '0' (length=1)
  16 => string '0' (length=1)
  17 => string '0' (length=1)
  18 => string '0' (length=1)
  19 => string '0' (length=1)
  20 => string '0' (length=1)
  21 => string '0' (length=1)
  22 => string '0' (length=1)
  23 => string '0' (length=1)
  24 => string '0' (length=1)
  25 => string '0' (length=1)
  26 => string '0' (length=1)
  27 => string '0' (length=1)
  28 => string '0' (length=1)
  29 => string '0' (length=1)
  30 => string '0' (length=1)
  31 => string '0' (length=1)
  32 => string '0' (length=1)
  33 => string '0' (length=1)
  34 => string '0' (length=1)
  35 => string '0' (length=1)
  36 => string '0' (length=1)
  37 => string '0' (length=1)
  38 => string '0' (length=1)
  39 => string '0' (length=1)
  40 => string '0' (length=1)
  41 => string '0' (length=1)
  42 => string '0' (length=1)
  43 => string '0' (length=1)
  44 => string '0' (length=1)
  45 => string '0' (length=1)
  46 => string '0' (length=1)
  47 => string '0' (length=1)
  48 => string '1' (length=1)
  49 => string '0' (length=1)
  50 => string '0' (length=1)

LINE TOTALS
array (size=51)
  0 => string '0.00' (length=4)
  1 => string '0.00' (length=4)
  2 => string '0.00' (length=4)
  3 => string '0.00' (length=4)
  4 => string '0.00' (length=4)
  5 => string '0.00' (length=4)
  6 => string '0.00' (length=4)
  7 => string '0.00' (length=4)
  8 => string '0.00' (length=4)
  9 => string '38.00' (length=5)
  10 => string '42.00' (length=5)
  11 => string '0.00' (length=4)
  12 => string '0.00' (length=4)
  13 => string '0.00' (length=4)
  14 => string '0.00' (length=4)
  15 => string '0.00' (length=4)
  16 => string '0.00' (length=4)
  17 => string '0.00' (length=4)
  18 => string '0.00' (length=4)
  19 => string '0.00' (length=4)
  20 => string '0.00' (length=4)
  21 => string '0.00' (length=4)
  22 => string '0.00' (length=4)
  23 => string '0.00' (length=4)
  24 => string '0.00' (length=4)
  25 => string '0.00' (length=4)
  26 => string '0.00' (length=4)
  27 => string '0.00' (length=4)
  28 => string '0.00' (length=4)
  29 => string '0.00' (length=4)
  30 => string '0.00' (length=4)
  31 => string '0.00' (length=4)
  32 => string '0.00' (length=4)
  33 => string '0.00' (length=4)
  34 => string '0.00' (length=4)
  35 => string '0.00' (length=4)
  36 => string '0.00' (length=4)
  37 => string '0.00' (length=4)
  38 => string '0.00' (length=4)
  39 => string '0.00' (length=4)
  40 => string '0.00' (length=4)
  41 => string '0.00' (length=4)
  42 => string '0.00' (length=4)
  43 => string '0.00' (length=4)
  44 => string '0.00' (length=4)
  45 => string '0.00' (length=4)
  46 => string '0.00' (length=4)
  47 => string '0.00' (length=4)
  48 => string '11.00' (length=5)
  49 => string '0.00' (length=4)
  50 => string '0.00' (length=4)
5
  • If you tell us what the suitable format for eloquent is: you will get more people that can help you. Commented Aug 22, 2015 at 15:45
  • Also what is wrong with the array you have produced? Commented Aug 22, 2015 at 15:47
  • @RiggsFolly - have edited the question. don't worry too much about the Eloquent attach at the moment, I think that will be simple once in the correct format. Thank you. Commented Aug 22, 2015 at 16:08
  • it looks like you are trying to get a value from either quantities or line_totals that does not exist. try var_dump all your arrays and compare theeir keys Commented Aug 22, 2015 at 16:54
  • @mdamia Some values for quantities of line_totals won't exist, as they are zero (see question, updated with var_dump). Commented Aug 23, 2015 at 17:54

2 Answers 2

1

Ok, I note some things in your code..

First, the product ids array consists of a matching key value for every item. This suggests that the $key and $id are made from the same source and are the same thing. In that case you can ditch your key value from your foreach. Saves some code and makes it more clear. Also try to use good variable names; $request->product_id is singular but it returns multiple as an array. Better can call it $request->product_ids; Same goes for $order = array();.

Futher I see that the product ids array doesn't have all the keys listed in the other two arrays. In this case your code always will crash if that key is not found. Better to use some check before assigning values to variables.

Here is a save way to assign the values:

$keys        = $request->product_id;
$quantities  = $request->quantity;
$line_totals = $request->line_total;

$orders = [];

foreach($keys as $key => $id) 
{
    // do we have all the data?
    if( !isset($quantities[ $id ]) || !isset($line_totals[ $id ]) )
    { // nopes.
        // log error or someting about missing key..

        continue; // skip this 'bad' one and try the next.
    }

    // we have all the data, save the order.
    $orders[ $key ] = [
        'quantity'   => $quantities[ $id ],
        'line_total' => $line_totals[ $id ]
    ];
}

Hope this helps!

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

1 Comment

good point about the keys, I updated my answer, used in_array instead.
1

have to check that product id exist in quantities or line totals

$keys = $request->product_id;
$quantities = $request->quantity;
$line_totals = $request->line_total;

$order = array();

foreach ($keys as $key => $id) {
   if( in_array($key,$quantities)  && in_array($key,$line_totals) ) {

        $order[$key] = array(
            'quantity'  => $quantities[$key],
            'line_total' => $line_totals[$key]
        );
   }
}

print_r ($order);

2 Comments

Getting close! But - "Indirect modification of overloaded property Illuminate\Http\Request::$product_id has no effect"
Glad I could help somehow. Sven nailed it, I updated my answer.

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.