0

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()
  {

  }

}
Shawn Sonnier
  • 513
  • 5
  • 10
  • 28

4 Answers4

0

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.

kidA
  • 1,379
  • 1
  • 9
  • 19
0

Try this:

foreach($rowData as $array)
{
    if($array[4] == 1)
       //some action
    else
       //another ction
}
hamed
  • 7,939
  • 15
  • 60
  • 114
0

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;
           }
        }
    }
Sylvain Martin
  • 2,365
  • 3
  • 14
  • 29
0

You can loop through all JSON Arrays by using a recursive algorithm, and if you encounter an element with a key of 4, depending on the value is 0 or 1 call different functions.

recursiveArray($rowData);

# A recursive function to traverse the $rowData array
function recursiveArray(array $rowData)
{
    foreach ($rowData as $key => $hitElement) {
        # If there is a element left
        if (is_array($hitElement)) {
            # call recursive structure to parse the $rowData
            recursiveArray($hitElement);
        } else {
            if ($key == 4) {
                switch($hitElement) {
                    case 1: 
                        echo 'Calling fn();' . PHP_EOL;
                        break;
                    case 0:
                        echo 'Calling differentfn();' . PHP_EOL;
                        break;
                }
            }
        }
    }
}

Live code -> https://wtools.io/php-sandbox/bFEO

hoangthienan
  • 826
  • 1
  • 9
  • 14