Home > other >  Output of multidimensional arrays in PHP
Output of multidimensional arrays in PHP

Time:10-20

i want to get the lowest value of an array(). Now I have 2 problems:

Problem 1: I can't define a 2D Array and get a illegal offset error.

Problem 2: When i found the lowest value I need to know who it is.

Example: Monday has a value of 5 and Tuesday has a value of 8. Now

I need to know if Monday or Tuesday has the lowest value and then I need the weekday of that value.

Here is my code:

$x = array(  
    ["Monday"]      => array($_REQUEST["Monday"]),
    ["Tuesday"]     => array($_REQUEST["Tuesday"]),
    ["Wednesday"]   => array($_REQUEST["Wednesday"]),
    ["Thursday"]    => array($_REQUEST["Thursday"]),
    ["Friday"]      => array($_REQUEST["Friday"]),
    ["Saturday"]    => array($_REQUEST["Saturday"]),
    ["Sunday"]      => array($_REQUEST["Sunday"])
);

CodePudding user response:

I think you are saying you are having problems creating an array with the day names with the values passed in from the form, your array definition was a little off.

Is this what you want?

$x = array(  
    "Monday"      => $_REQUEST["Monday"],
    "Tuesday"     => $_REQUEST["Tuesday"],
    "Wednesday"   => $_REQUEST["Wednesday"],
    "Thursday"    => $_REQUEST["Thursday"],
    "Friday"      => $_REQUEST["Friday"],
    "Saturday"    => $_REQUEST["Saturday"],
    "Sunday"      => $_REQUEST["Sunday"]
);

Now to get the minimum and tell you which day that is

$x = array(  
    "Monday"      => 4,
    "Tuesday"     => 3,
    "Wednesday"   => 2,
    "Thursday"    => 1,
    "Friday"      => 6,
    "Saturday"    => 7,
    "Sunday"      => 8
);

print_r(array_keys($x, min($x))[0]);

RESULT

Thursday
  • Related