0

I trying to get the minimum values from the any column contains "xx" in the column name. I think there is a problem in the name of the column maybe because they start with digits.

Below is my code:

<?php
$array = array(
 array(
  'id' => 1,
  '10xx' => 14,
  '11xx' => 32,
  '12xx' => 4
 ),

  array(
   'id' => 2,
  '10xx' => 13,
  '11xx' => 36,
  '12xx' => 41
 )
);



foreach($array as $item)
{
 $lowestKey = '';
 foreach($item as $key => $value)
 {


  if(strpos($key, 'xx') === 0)
  {

   if($lowestKey == '')
   {
    $lowestKey = $key;
   }
   else
   {
    if($value < $item[$lowestKey])
    {
     $lowestKey = $key;
    }
   }
  }
 }

 echo 'lowest for id ' . $item['id'] . ': ' . $item[$lowestKey] . "\n";
}
?>
4

1 回答 1

1

这是工作代码,

<?php

$array = array(
    array(
        'id' => 1,
        '10xx' => 14,
        '11xx' => 32,
        '12xx' => 4
    ),
    array(
        'id' => 2,
        '10xx' => 13,
        '11xx' => 36,
        '12xx' => 41
    )
);



foreach ($array as $item) {
    $lowestKey = '';
    foreach ($item as $key => $value) {
        if (strstr($key, 'xx')) {
            if ($lowestKey == '') {
                $lowestKey = $key;
            } else {
                if ($value < $item[$lowestKey]) {
                    $lowestKey = $key;
                }
            }
        }
    }

    echo 'lowest for id ' . $item['id'] . ': ' . $item[$lowestKey] . "\n";
}
?>

而不是strpos()使用strstr()

于 2013-10-07T10:44:16.170 回答