i have the following array written in PHP. I need a way to print values by name, and if possible, do calculations with specific values (by looping through).
[MAIN] => Array
(
[FIRST] => Array
(
[0] => 500
[1] => 1000
[2] => 1500
)
[SECOND] => Array
(
[Name] => Nick
[State] => None
)
)
For example, by using the array above i'd like to print only the NAME of the "SECOND" array but without looping through every index (without using an index at all), and add each value of the "FIRST" array by looping throu them.
Thank you!
$result = 0;
foreach ($array['MAIN']['FIRST'] as $val) {
$result += $val;
}
echo $result;
The first question is like this:
echo $array['MAIN']['SECOND']['Name'];
You have an array of an array of an array so you have to dereference values as such.
The second one would look something like this:
$var = 0;
for($i = 0; $i < 3; $i++) {
$var += $array['MAIN']['FIRST'][$i];
}
echo $var;