Reputation: 11
I would like to know how to loop through a multidimensional array and here is my code and some explanation on what I am trying to achieve here
My Array:-
$new_info = array(
"Serial_Numbers" => array(101, 102, 103, 104),
"Costs" => array(10, 9, 8, 8)
);
What I want is to get the value for array 1 and array 2 by using it's key which are currently set to, as you can see in the code above, "Serial_Numbers" and "Costs". It might sound a bit confusing so let me show what I plan to do with the values I get from these arrays:-
$con=mysqli_connect("localhost","root","root","saved_new");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect, error is: " . mysqli_connect_error();
}
$new_info = array(
"Serial_Numbers" => array(101, 102, 103, 104),
"Costs" => array(10, 9, 8, 8)
);
foreach ($new_info as $insert_info) {
$sql="INSERT INTO new_table (serial_no, cost)
VALUES
('$insert_info['Serial_Numbers'], $insert_info['Costs']')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "All records added" . "<br />";
}
mysqli_close($con);
?>
Let me sum it up again for a better understanding; I want to add all the values from the array called 'Serial_Numbers' to the database in the column called 'serial_numbers' and all the values from the array called 'Costs' to the column called 'costs' in my database and the reason I would like to use a foreach loop to achieve this is that I want it to go through both of those arrays at the same time and keep going until it runs out of values, that way, I can have the information updated in my database according to the correct Serial Number and it's Cost.
Hope it makes sense now, please let me know if you want me to add anything else to this.
Upvotes: 0
Views: 187
Reputation: 312
if your serial number is unique,then you can use the normal array like this.
$new_info = array(101=>10,102=>9,103=>8,104=>9);
foreach($new_info as $serial_number => $cost) {
}
if the serial number is not unique,then use the approach suggested by Barmar.
Upvotes: 0
Reputation: 2426
Try this:
for ( $i = 0; $i <= count($new_info['Serial_Numbers']); $i++ )
{
echo $new_info['Serial_Numbers'][$i].'<br />';
echo $new_info['Costs'][$i];
}
This will count the amount of entries in your subarray and you then can access each index by $i.
Upvotes: 1
Reputation: 780688
You need to loop over both sub-arrays in parallel, not with nested loops, so you get all the corresponding elements as pairs, not a cross-product.
foreach ($new_info['Serial_Numbers'] as $i => $serial) {
$cost = $new_info['Costs'][$i];
$sql = "INSERT INTO new_table (serial_no, cost) VALUES ('$serial', $cost)";
...
}
It would be better if you reorganized the data structure to keep related elements together:
$new_info = array(array('Serial_Number' => 101, 'Cost' => 10),
array('Serial_Number' => 102, 'Cost' => 9),
array('Serial_Number' => 103, 'Cost' => 8),
array('Serial_Number' => 104, 'Cost' => 8));
This seems to be what you're expecting in your loop.
Upvotes: 3