I have the following array. Please ignore the syntax, because i copied it form source.
<?php
$rowData = Array
(
[1] = Array
(
[0] = Buffalo
[1] = Tampa Bay
[2] = -7
[3] = favorite
[4] = 0
[5] = 46
)
[2] = Array
(
[0] = Minnesota
[1] = Tennessee
[2] = 3
[3] = favorite
[4] = 1
[5] = 33
)
[3] = Array
(
[0] = Green Bay
[1] = Cincinnati
[2] = 3
[3] = favorite
[4] = 1
[5] = 33
)
[4] = Array
(
[0] = Jacksonville
[1] = Buffalo
[2] = 4
[3] = underdog
[4] = 1
[5] = 54
)
);
?>
What I want to do is loop through each array and if the [4] entry is =1 perform one function on that array, and if the [4] entry is =0 perform a different function. Im not sure how to identify each one in a loop..
foreach ($rowData as $row => $tr)
{
//if [4] is equal to 1
if()
{
}
//if [4] is equal to 0
elseif()
{
}
}
If you want to perform some functionality on the sub arrays of $rowData
in order to get an updated version after the loop has finished you need to do this:
echo '<pre>',print_r($rowData),'</pre>';
foreach ($rowData as &$tr) // the & sign will pass the sub array $tr as a reference
{
//if [4] is equal to 1
if($tr[4] == 0)
{
execute_function1($tr);
}
//if [4] is equal to 0
elseif($tr[4] == 0)
{
execute_function2($tr);
}
}
// again you need to pass the sub array as a reference in order to make sure that the functionality you are going to apply to the $tr in the following functions will be also applied to the respective $tr of the $rowData array
execute_function1(&$tr){ .. };
execute_function2(&$tr){ .. };
echo '<pre>',print_r($rowData),'</pre>';
I used to echo
statements (one before the loop and one after) so you can see how your $rowData
array changes.
Try this:
foreach($rowData as $array)
{
if($array[4] == 1)
//some action
else
//another ction
}
You can do like this, but dont't forget to test if $tr[4] exists:
foreach ($rowData as $row => $tr)
{
//Test if the key 4 exists
if(isset($tr[4])) {
//Switch value
switch($tr[4]) {
case 1:
//Do action...
break;
case 0:
//Do action...
break;
default:
//Do nothing...
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.