Jump to content

loop php multidimensional array


ale1981
Go to solution Solved by Ch0cu3r,

Recommended Posts

It's late in the day and I can't figure this one out, I know it's going to be pretty easy!

 

I have a form which sends the following array;

 

array:2 [

"names" => array:3 [

0 => "Default"

1 => "100ml"

2 => "150ml;"

]

"prices" => array:3 [

0 => "12.99"

1 => "9.99"

2 => "14.99"

]

 

I want to loop the array and do an insert using this array like so, the index from "names" and "prices";

INSERT INTO DB VALUES('Default',12.99);
INSERT INTO DB VALUES('100ml',9.99);
INSERT INTO DB VALUES('150ml',14.99);

Thanks

Link to comment
Share on other sites

  • Solution

Use a foreach loop to iterative over the names array, using the array key to get the corresponding value in the prices array

// loop over the names array
foreach($array['names'] as $key => $name)
{
    // use the array key to reference the corresponding value in the prices array
    $price = $array['prices'][$key];

   // do insert query
}

Change $array to be your variable that contain the names and prices array

Link to comment
Share on other sites

Use a foreach loop to iterative over the names array, using the array key to get the corresponding value in the prices array

// loop over the names array
foreach($array['names'] as $key => $name)
{
    // use the array key to reference the corresponding value in the prices array
    $price = $array['prices'][$key];

   // do insert query
}

Change $array to be your variable that contain the names and prices array

 

Thanks Ch0cu3r that worked, I knew there would be a simple answer!

 

I would like to see your form.

 

I have 2 input fields that use the same variable array, e.g.

<input type="text" name="productVariations[name][]" />
<input type="text" name="productVariations[price][]" />

Is there a different way?

Link to comment
Share on other sites

I have 2 input fields that use the same variable array, e.g.

<input type="text" name="productVariations[name][]" />
<input type="text" name="productVariations[price][]" />

Is there a different way?

 

There is always a different way, the question is if there is a better way or not - and in this case there is. I was actually going to speak to the same problem that I'm sure benanamen was going to address, but decided against it once Ch0cu3r responded.

 

You should not have data where there is an assumption of correlation based on position. This is bad form and will eventually lead to bugs. Instead, the correlated data should be structured such that the correlation is specifically defined. In this case you have pairs of fields that should be correlated within the structure. Using a multidimensional array was the right choice - but I would swap the keys so that the records are logically structured. But, instead of "[]" you will need to define the key.

 

E.g.

<input type="text" name="productVariations[0][name] />
<input type="text" name="productVariations[0][price]" />
 
<input type="text" name="productVariations[1][name] />
<input type="text" name="productVariations[1][price]" />
 
<input type="text" name="productVariations[2][name] />
<input type="text" name="productVariations[2][price]" />

I'm sure I've left off some content that goes between the fields and, ideally, the output would be generated within a loop. using that structure, the array of the post data would look like this

productVariations array (
  0 => ("name" => "Default", "price" => "12.99"),
  1 => ("name" => "100ml", "price" => "9.99"),
  2 => ("name" => "150ml", "price" => "14.99")
)

 

Then the code to process the data could look like this

foreach($productVariationsAry as $record)
{
    $name = $record['name'];
    $price = $record['price'];
 
   // do insert query
}

FYI: It would also be more efficient to create ONE insert query to insert all records at one.

Edited by Psycho
  • Like 2
Link to comment
Share on other sites

There is always a different way, the question is if there is a better way or not - and in this case there is. I was actually going to speak to the same problem that I'm sure benanamen was going to address, but decided against it once Ch0cu3r responded.

 

You should not have data where there is an assumption of correlation based on position. This is bad form and will eventually lead to bugs. Instead, the correlated data should be structured such that the correlation is specifically defined. In this case you have pairs of fields that should be correlated within the structure. Using a multidimensional array was the right choice - but I would swap the keys so that the records are logically structured. But, instead of "[]" you will need to define the key.

 

E.g.

<input type="text" name="productVariations[0][name] />
<input type="text" name="productVariations[0][price]" />
 
<input type="text" name="productVariations[1][name] />
<input type="text" name="productVariations[1][price]" />
 
<input type="text" name="productVariations[2][name] />
<input type="text" name="productVariations[2][price]" />

I'm sure I've left off some content that goes between the fields and, ideally, the output would be generated within a loop. using that structure, the array of the post data would look like this

productVariations array (
  0 => ("name" => "Default", "price" => "12.99"),
  1 => ("name" => "100ml", "price" => "9.99"),
  2 => ("name" => "150ml", "price" => "14.99")
)

 

Then the code to process the data could look like this

foreach($productVariationsAry as $record)
{
    $name = $record['name'];
    $price = $record['price'];
 
   // do insert query
}

FYI: It would also be more efficient to create ONE insert query to insert all records at one.

 

Thanks for the detailed response.

 

I am adding the fields dynamically so I do not know how many the user is going to have, I guess I could use a bit of JS to determine this.

 

Yes the SQL query will be one query, I used the above as an example of what I needed.

 

 

Thanks for all responses.

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.