arok
arok

Reputation: 1845

How to get the minimum value from array row

I trying to get the minimum values from the any column contains "xx" in the column name.

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";
}
?>

Upvotes: 1

Views: 2315

Answers (6)

mickmackusa
mickmackusa

Reputation: 47874

  1. Iterate each row/subarray with a foreach() loop or array_walk().
  2. Extract and display the id (first element) value with array_shift().
  3. Call min() on the remaining values in the respective subarray to determine the lowest value.

No conditional expressions. No unnecessary variables. Clean, concise, and effective.

Code: (Demo)

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

array_walk($array, function($row) {
    echo array_shift($row) , " : " , min($row) , "\n";
});

Output:

1 : 4
2 : 13

Upvotes: 0

user2842104
user2842104

Reputation: 7

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

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

    $lowestKey = '';

    foreach($array as $arra){
        foreach ($arra as $key=>$value){
            if ($key == 'id'){
                if(($value < $lowestKey )||( $lowestKey== '')){
                $lowestKey = $value;
                }
            }


        }
    }
   echo $lowestKey;

Upvotes: -1

user2663434
user2663434

Reputation:

function _getNumber($array) {
  return $array['id'];
}
$numbers = array_map('_getNumber', $array);

OR

$numbers = array_map(function($array) {
  return $array['id'];
}, $array);

echo $min = min($numbers);
echo $max = max($numbers);

Upvotes: 2

David Jones
David Jones

Reputation: 4305

Instead of looping again inside just use the min() function.

$lowest_keys = array();

foreach($array as $item)
{
     unset( $item[ 'id' ] );
     $lowest_keys[] = min( $item );
}

Upvotes: 0

undone
undone

Reputation: 7888

function find_lowest($array){
        $new_array = array();
        foreach($array  as $key => $val ){
            if(is_array($val)){
                $new_array[$key] = find_lowest($val);
            }else{
                $new_array[$key] =  $val ;
            }
        }
        return min($new_array);

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

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

Upvotes: 0

opalenzuela
opalenzuela

Reputation: 3171

You have a function already for it:

http://php.net/manual/en/function.min.php

echo min(2, 3, 1, 6, 7);  // 1
echo min(array(2, 4, 5)); // 2

echo min(0, 'hello');     // 0
echo min('hello', 0);     // hello
echo min('hello', -1);    // -1

Combine it with array_values if this fits better your needs.

Upvotes: 3

Related Questions