Array in IF statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BaseballGraphs
    New Member
    • Sep 2010
    • 75

    Array in IF statement?

    Hi,

    I am trying to write an IF statement that may occur under several different scenarios. I tried to write an array in which each IF statement would occur, but I am not having any luck.

    Here is the code I have so far:
    Code:
     arsort ( $array );
      $low_to_high = array( "1" => "22", "2" => "23", "3" => "24", "4" => "26", "5" => "27", "6" => "47", "7" => "49", "8" => "50", "9" => "51", "10" => "59", "11" => "61", "12" => "40" );
      if ( $statistic_id = in_array(array_values($low_to_high), $low_to_high))
      asort ( $array );
    Essentially, if $statistic_id equals any of the values in my array, then I want the script to perform asort instead of arsort.

    Any guidance would be highly appreciated!
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Well where do we start. The index is not needed nor are the quotes
    Code:
    $low_to_high = array(22,23,24,26,...40);
    I am not sure what this means
    Code:
    if ( $statistic_id = in_array(array_values($low_to_high), $low_to_high))
    But in_array() returns true or false so $statistic_id will become a boolean.
    You don't need array_values() because in_array() only checks the array values.
    So it looks like you are comparing $low_to_high to itself.
    Which will always return true so the line following if() will always execute ....I think.

    I don't understand what this means
    I tried to write an array in which each IF statement would occur
    Also
    if $statistic_id equals any of the values in my array, then I want the script to perform asort instead of arsort
    What is $statistic_id and what is my array?

    I suggest a step back and rethink what you are tring to do.
    Also study arrays and how the array functions work.

    Comment

    • BaseballGraphs
      New Member
      • Sep 2010
      • 75

      #3
      Hello,

      Sorry for my convoluted question. It appears as though this can be solved easily by doing this:
      Code:
      arsort ($array);
      if ($statistic_id == 22 || $statistic_id == 23 || ... || $statistic_id == n)
      asort ($array);

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        It makes more sense now, but your array idea is tidier
        Code:
        $low_to_high = array(22,23,24,26,27,47,49,50,51..); 
        if(in_array($statistic_id ,$low_to_high))
            asort ($array);

        Comment

        • oranoos3000
          New Member
          • Jan 2009
          • 107

          #5
          hi my friend
          your code that you wrote is seem to be correct only in end line you use

          in_array(array_ values($low_to_ high), $low_to_high)
          must be
          in_array($low_t o_high, $low_to_high)

          be successful

          Comment

          Working...