Home > front end >  Find the biggest value & return master key
Find the biggest value & return master key

Time:01-13

`Hi,

For example I've such array:

array:3 [
    1 => array:1 [
      "Home" => array:3 [
        "Weight (kg)" => 4.0
        "Length (cm)" => null
        "Width (cm)" => null
      ]
    ]
    2 => array:1 [
      "Home" => array:3 [
        "Weight (kg)" => 3.0
        "Length (cm)" => 3.0
        "Width (cm)" => null
      ]
    ]
    3 => array:1 [
      "Home" => array:3 [
        "Weight (kg)" => 2.0
        "Length (cm)" => 2.0
        "Width (cm)" => null
      ]
    ]
 ]

Need to find a way to get biggest value & that's not a problem, I could find it easily, but how to find biggest value & return i.e. key number 1, cause it has the greatest value from all others. Must be checked with locations i.e. Home, could be an array, need to find in group all maxes & return its parent id to mark as standard elements. Someone could help with that?

Thanks anyway

I've found max value by method:

$max = null;
array_walk_recursive($values, function ($value) use (&$max) {
    if ($max === null || $value > $max) $max = $value;
});
return $max;

But can't find a good way to return this element or it's key:

1 => array:1 [
      "Home" => array:3 [
        "Weight (kg)" => 4.0
        "Length (cm)" => null
        "Width (cm)" => null
      ]
    ]

CodePudding user response:

The lambda you can hand over to the "array_walk" functions can accept two parameters, key and value. That should be the missing link you are looking for.

I slightly modified your approach that way:

<?php
$input = [
  [
    "Home" => [
      "Weight (kg)" => 4.0,
      "Length (cm)" => null,
      "Width (cm)" => null
    ]
  ],
  [
    "Home" => [
      "Weight (kg)" => 3.0,
      "Length (cm)" => 3.0,
      "Width (cm)" => null
    ],
    "Office" => [
      "Weight (kg)" => 3.0,
      "Length (cm)" => 6.0,
      "Width (cm)" => 2.0
    ]
  ],
  [
    "Home" => [
      "Weight (kg)" => 2.0,
      "Length (cm)" => 2.0,
      "Width (cm)" => null
    ]
  ]
];

$max = null;
array_walk($input, function ($entry, $entryKey) use (&$max) {
  array_walk($entry, function($loc, $locKey) use (&$max, $entryKey) {
    $val = max($loc);
    if ($max === null || $val > $max['val']) {
      $max['val'] = $val;
      $max['key'] = $entryKey;
      $max['loc'] = $locKey;
    }
  });
});

printf("Maximum value '%.2f' of location '%s' is in element with key '%d'.", $max['val'], $max['loc'], $max['key']);

The output obviously is:

Maximum value '6.00' of location 'Office' is in element with key '1'.

CodePudding user response:

You could do it this way:

<?php

$array = [
    1 => [
        "Home" => [
            "Weight (kg)" => 4.0,
            "Length (cm)" => null,
            "Width (cm)" => null,
        ],
    ],
    2 => [
        "Home" => [
            "Weight (kg)" => 3.0,
            "Length (cm)" => 3.0,
            "Width (cm)" => null,
        ],
    ],
    3 => [
        "Home" => [
            "Weight (kg)" => 2.0,
            "Length (cm)" => 2.0,
            "Width (cm)" => null,
        ],
    ],
];

$biggestWeight = 0;
$biggestWeightKey = '';

foreach ($array as $key => $data) {
    if ($data['Home']['Weight (kg)'] > $biggestWeight) {
        $biggestWeight = $data['Home']['Weight (kg)'];
        $biggestWeightKey = $key;
    }
}

echo 'Biggest Weight is: ' . $biggestWeight . PHP_EOL;
echo 'Biggest Weight Key is: ' . $biggestWeightKey;

Output:

Biggest Weight is: 4
Biggest Weight Key is: 1

And if you want whole element:

echo '<pre>';
var_dump($array[$biggestWeightKey]);

Output:

array(1) {
  ["Home"]=>
  array(3) {
    ["Weight (kg)"]=>
    float(4)
    ["Length (cm)"]=>
    NULL
    ["Width (cm)"]=>
    NULL
  }
}

CodePudding user response:

    Imagine such data table data:
    
$input = array(
    array("val0"=>"Full pallet","val1"=>"Standard","val2"=>"Weight (kg)","val3"=>"0","val4"=>"1250","val5"=>"Home")
    array("val0"=>"Full pallet","val1"=>"Standard","val2"=>"Length (cm)","val3"=>"0","val4"=>"160","val5"=>"Home")
    array("val0"=>"Full pallet","val1"=>"Standard","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Full pallet","val1"=>"Standard","val2"=>"Height (cm)","val3"=>"0","val4"=>"220","val5"=>"Home")
    array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"150","val5"=>"Home")
    array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"60","val5"=>"Home")
    array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"300","val5"=>"Home")
    array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"60","val5"=>"Home")
    array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"500","val5"=>"Home")
    array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"100","val5"=>"Home")
    array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"1000","val5"=>"Home")
    array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"80","val5"=>"Home")
    array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"220","val5"=>"Home")
    array("val0"=>"Oversized pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"1250","val5"=>"Home")
    array("val0"=>"Oversized pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"260","val5"=>"Home")
    array("val0"=>"Oversized pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
    array("val0"=>"Oversized pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"220","val5"=>"Home")
);
    
    Should be like this:
    
    $input = array(
        array("val0"=>"Oversized pallet","val1"=>"Standard","val2"=>"Weight (kg)","val3"=>"0","val4"=>"1250","val5"=>"Home")
        array("val0"=>"Oversized pallet","val1"=>"Standard","val2"=>"Length (cm)","val3"=>"0","val4"=>"260","val5"=>"Home")
        array("val0"=>"Oversized pallet","val1"=>"Standard","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Oversized pallet","val1"=>"Standard","val2"=>"Height (cm)","val3"=>"0","val4"=>"220","val5"=>"Home")
        array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"150","val5"=>"Home")
        array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Micro pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"60","val5"=>"Home")
        array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"300","val5"=>"Home")
        array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Quarter pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"60","val5"=>"Home")
        array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"500","val5"=>"Home")
        array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Half pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"100","val5"=>"Home")
        array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"1000","val5"=>"Home")
        array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"80","val5"=>"Home")
        array("val0"=>"Euro pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"220","val5"=>"Home")
        array("val0"=>"Full pallet","val1"=>"Regular","val2"=>"Weight (kg)","val3"=>"0","val4"=>"1250","val5"=>"Home")
        array("val0"=>"Full pallet","val1"=>"Regular","val2"=>"Length (cm)","val3"=>"0","val4"=>"160","val5"=>"Home")
        array("val0"=>"Full pallet","val1"=>"Regular","val2"=>"Width (cm)","val3"=>"0","val4"=>"120","val5"=>"Home")
        array("val0"=>"Full pallet","val1"=>"Regular","val2"=>"Height (cm)","val3"=>"0","val4"=>"220","val5"=>"Home")
    );
    
    
    For only one `Element Type` this should only biggest value for only dimension types.
    This could be easily done?
  • Related