Iterating over a complex Associative Array in PHP
Nest two foreach
loops:
foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}
foreach loop over multidimensional associative array
You seem to be going about everything right..but if you are going to be using the start
/end
/rate
values directly, you shouldn't need the second foreach
loop. Something like this should get you going:
$values = array();
foreach($this->taxBand as $key => $band) {
$calculation = ($band['end'] - $band['start']) / $band['rate'];
$values[$key] = $calculation;
}
return $values;
I'm doubting this is the work you plan to do, but the point is you can access the inner associative array $band
directly.
If you were to use a sub-loop for whatever reason, you could do something like this:
foreach($this->taxBand as $key => $band) {
// Set variables for this band
foreach($band as $subKey => $value) {
switch($subKey) {
case 'start': // do something
break;
case 'end': // do something
break;
case 'rate': // do something
break;
}
}
// Calculations finished
}
complex multidimentional associative array process with foreach
I think you can really simplify this quote a bit. If you know that this will always be the structure then you can jump right down into the hotels and then into the rooms.
foreach($arr['search']['lr_rates']['hotel'] as $hotel) {
// You can access all of the keys in the hotel array here
foreach($hotel['hotel_rooms'] as $room) {
// Do stuff with the room array
}
}
I would recommend either building your insert script on the fly and calling the database just once for the write, or if you are updating then using a transaction. As the number of rooms gets larger you will slow your script down with a bunch of writes to disk.
php loop through complex posts
You can use Recursive
function like this.
<?php
$arr = array(
"name"=> "mmmkkkk",
"phones"=> array(
"01553338897" ,
"09090909098"
),
"address"=> "107 ostras., Germany",
"assistant"=> array(
"name" => array(
"kmkkm",
"komar"
),
"phone"=> array(
"01043338897" ,
"09099090090"
)
)
);
function rec($arr) {
foreach($arr as $key => $p_value)
{
if (is_array($p_value)) {
rec($p_value);
} else {
echo $key.":".$p_value."\n";
}
}
}
rec($arr);
?>
php loop through complex posts
You can use Recursive
function like this.
<?php
$arr = array(
"name"=> "mmmkkkk",
"phones"=> array(
"01553338897" ,
"09090909098"
),
"address"=> "107 ostras., Germany",
"assistant"=> array(
"name" => array(
"kmkkm",
"komar"
),
"phone"=> array(
"01043338897" ,
"09099090090"
)
)
);
function rec($arr) {
foreach($arr as $key => $p_value)
{
if (is_array($p_value)) {
rec($p_value);
} else {
echo $key.":".$p_value."\n";
}
}
}
rec($arr);
?>
Problem Iterating through a multi-dimensional array in PHP
The id is part of the array, as you can access it like $value['id']
In the second foreach to prevent confusion you should select a different name for the key and the value.
Try it like this:
foreach($usmDet as $key => $value){
if(is_array($value)){
foreach($value as $k => $v){
echo $v['id'] . "<br>";
}
}
}
Result:
74696
1525
73401
210
Php demo
To get all the values for key "id" when multiple nested arrays, you could use array_walk_recursive
$ids = [];
array_walk_recursive($usmDet, function($value, $key) use (&$ids){
if ($key === "id") {
$ids[] = $value;
}
});
print_r($ids);
Php demo
Related Topics
How to Create Codeigniter Batch Insert Array
Selecting Multiple Array Elements
Why Should I Use Templating System in PHP
Determining What Classes Are Defined in a PHP Class File
Resize Images in PHP Without Using Third-Party Libraries
Why Should I Fix E_Notice Errors
New Lines (\R\N) Are Not Working in Email Body
Unexpected Character in Input: '\' (Ascii=92) State=1
Regex Backreference to Match Different Values
PHP Pdo VS Normal MySQL_Connect