简体   繁体   中英

Selective loop through multidimensional array with if and foreach?

I have a two dimensional array which is used for calculation. How do I loop through it so that I can check my IF clause against ONLY the elements with same index.

I think it should be done with foreach, but I am unable to make it work. I want to check IF clause against $array[1][0], $array[2][0], $array[3][0] ...etc

Many thanks for help!

<?php

//variables

$variable1 = 50;
$variable2 = 100;

$array = array( array(50, 125 , 15),
                array(30, 75 , 25),
                array(50, 25 , 7) 
             );
echo "<pre>";
print_r($array);
echo "<pre>";

if ($variable1 > $array[0][0]) {
    $reserve[] = $variable1 - $array[0][0];
    $surplus[] = 0;
} elseif ($variable1 == $array[0][0]) {

    $reserve[] = 0;
    $surplus[] = 0;
} 
 else {
    $surplus[] = $array[0][0] - $variable1;
    $reserve[] = 0;

}

echo "<pre>";
print $reserve[0];
echo "<pre>";

echo "<pre>";
print $surplus[0];
echo "<pre>";

?>

Here is complete code, based on the partial solution. Any ideas why it doesn'work ? I get an error in foreach line.

<?php
error_reporting(E_ALL);
$con=mysqli_connect("localhost","root","","base1");
// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT * FROM details";
$result=mysqli_query($con,$sql);


while ($data = mysqli_fetch_array($result, MYSQLI_NUM)) {

    /////////////////////

    }
    // Free result set
mysqli_free_result($result);

mysqli_close($con);
/////////////////////////////////////////////
?><?php


$variable1= 50;


$reserve = array();
$surplus = array();
foreach($data as $key=>$next)
{
   if ($variable1> $array[$key][0])
   {
      $reserve[] = $variable1- $array[$key][0];
      $surplus[] = 0;
   }
   elseif($variable1== $array[$key][0])
   {

      $reserve[] = 0;
      $surplus[] = 0;
   } 
   else 
   {
      $surplus[] = $array[$key][0] - $variable1;
      $reserve[] = 0;
   }
}

echo "<pre>";
print_r($reserve);
echo "<pre>";

echo "<pre>";
print_r($surplus);
echo "<pre>";

?>

I may have got completely the wrong end of the stick here. Try something like so:

//variables

$variable1 = 50;
$variable2 = 100;

$array = array( array(50, 125 , 15),
            array(30, 75 , 25),
            array(50, 25 , 7) 
         );

echo "<pre>";
print_r($array);
echo "<pre>";

foreach($array as $key=>$next)
{
   if ($variable1 > $array[$key][0])
   {
      $reserve[] = $variable1 - $array[$key][0];
      $surplus[] = 0;
   }
   elseif($variable1 == $array[$key][0])
   {

      $reserve[] = 0;
      $surplus[] = 0;
   } 
   else 
   {
      $surplus[] = $array[$key][0] - $variable1;
      $reserve[] = 0;
   }
}

echo "<pre>";
print_r($reserve);
echo "<pre>";

echo "<pre>";
print_r($surplus);
echo "<pre>";

I've tried foreach and it works:

foreach ($array as $subarray) {

    if ($variable1 > $subarray[0]) {
        $reserve[] = $variable1 - $subarray[0];
        $surplus[] = 0;
    } elseif ($variable1 == $subarray[0]) {

        $reserve[] = 0;
        $surplus[] = 0;
    } else {
        $surplus[] = $subarray[0] - $variable1;
        $reserve[] = 0;
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM