skip to Main Content

`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
      ]
    ]

2

Answers


  1. 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
      }
    }
    
    Login or Signup to reply.
  2. 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’.


    An extended version of above code that finds and reports the maximum value for each of the given dimensions separately:

    <?php
    $input = [
      1 => [ 
        'Home' => [
          'Weight (kg)' => 150.0,
          'Length (cm)' => 120.0,
          'Width (cm)' => 120.0,
          'Height (cm)' => 60.0,
          'Worth (€)' => 555,
        ],
      ],
      2 => [
        'Office' => [
          'Weight (kg)' => 300.0,
          'Length (cm)' => 120.0,
          'Width (cm)' => 120.0,
          'Height (cm)' => 60.0,
          'Worth (€)' => 666,
        ],
      ],
      3 => [
        'Home' => [
          'Weight (kg)' => 500.0,
          'Length (cm)' => 120.0,
          'Width (cm)' => 120.0,
          'Height (cm)' => 100.0,
          'Temperature (°C)' => 55,
        ],
        'Office' => [
          'Weight (kg)' => 1000.0,
          'Length (cm)' => 120.0,
          'Width (cm)' => 80.0,
          'Height (cm)' => 220.0,
        ],
      ],
      5 => [
        'Condo' => [
          'Weight (kg)' => 1250.0,
          'Length (cm)' => 160.0,
          'Width (cm)' => 120.0,
          'Height (cm)' => 220.0,
        ],
      ],
      6 => [
        'Beach' => [
          'Weight (kg)' => 1250.0,
          'Length (cm)' => 260.0,
          'Width (cm)' => 120.0,
          'Height (cm)' => 220.0,
        ],
      ],
    ];
    
    $max = [];
    array_walk($input, function ($entry, $entryKey) use (&$max) {
      array_walk($entry, function($loc, $locKey) use (&$max, $entryKey) {
        array_walk($loc, function($val, $dimKey) use (&$max, $entryKey, $locKey) {
          if (!array_key_exists($dimKey, $max) || $max[$dimKey]['val'] < $val) {
            $max[$dimKey] = [
              'key' => $entryKey,
              'loc' => $locKey,
              'val' => $val,
            ];
          }
        });
      });
    });
    
    foreach ($max as $dim => $val) {
      printf(
        "Dimension '%s' has a maximum value '%.2f' with location '%s' is in element with key '%d'.n", 
        $dim, $val['val'], $val['loc'], $val['key']
      );
    }
    

    The output is:

    Dimension ‘Weight (kg)’ has a maximum value ‘1250.00’ with location ‘Condo’ is in element with key ‘5’.

    Dimension ‘Length (cm)’ has a maximum value ‘260.00’ with location ‘Beach’ is in element with key ‘6’.

    Dimension ‘Width (cm)’ has a maximum value ‘120.00’ with location ‘Home’ is in element with key ‘1’.

    Dimension ‘Height (cm)’ has a maximum value ‘220.00’ with location ‘Office’ is in element with key ‘3’.

    Dimension ‘Worth (€)’ has a maximum value ‘666.00’ with location ‘Office’ is in element with key ‘2’.

    Dimension ‘Temperature (°C)’ has a maximum value ‘55.00’ with location ‘Home’ is in element with key ‘3’.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search