Arrays and Complex Indices

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bigoxygen@gmail.com

    Arrays and Complex Indices

    Hi...
    the following line doesn't give its intended effect
    $selected[ $out['month'] ] = 'SELECTED';

    but this line will:
    $selected[ $out['month'] + 0 ] = 'SELECTED';

    How do I make sure $out['month'] is evaluated correctly without added a
    0 to guide php?

    thanks

  • Matt Mitchell

    #2
    Re: Arrays and Complex Indices


    <bigoxygen@gmai l.com> wrote in message
    news:1108577616 .056573.98280@o 13g2000cwo.goog legroups.com...[color=blue]
    > Hi...
    > the following line doesn't give its intended effect
    > $selected[ $out['month'] ] = 'SELECTED';
    >
    > but this line will:
    > $selected[ $out['month'] + 0 ] = 'SELECTED';
    >
    > How do I make sure $out['month'] is evaluated correctly without added a
    > 0 to guide php?
    >
    > thanks
    >[/color]

    what do you get for
    print_r($out)

    and
    print_r($select ed)

    and how do you populate the arrays in the first place?

    Matt


    Comment

    • bigoxygen@gmail.com

      #3
      Re: Arrays and Complex Indices

      $out:
      Array ( [course_id] => [html_title] => Change Profile [trail] => Home >
      Change Profile:

      [hooks] => Back to Home [day] => 12 [month] => 05 [year] => 1983
      [birmonth] => )

      1

      $selected

      Array ( [1] => [2] => [3] => [4] => [5] => SELECTED [6] => [7] => [8]
      => [9] => [10] => [11] => [12] => )

      1

      Comment

      • Matt Mitchell

        #4
        Re: Arrays and Complex Indices


        <bigoxygen@gmai l.com> wrote in message
        news:1108579335 .805576.162660@ g14g2000cwa.goo glegroups.com.. .[color=blue]
        > $out:
        > Array ( [course_id] => [html_title] => Change Profile [trail] => Home >
        > Change Profile:
        >
        > [hooks] => Back to Home [day] => 12 [month] => 05 [year] => 1983
        > [birmonth] => )[/color]
        [color=blue]
        > $selected
        >
        > Array ( [1] => [2] => [3] => [4] => [5] => SELECTED [6] => [7] => [8]
        > => [9] => [10] => [11] => [12] => )[/color]

        Right, got it.

        When PHP looks at $selected[$out['month']], it evaluates $selected['05'] by
        the looks of it, i.e. the 'month' value in $out is a string, not a number.
        To fix this, try either
        1 - set the value as a number in the first place:
        $out['month'] = 5;

        2 - cast the value to an integer
        $selected[int($out['month']] = 'SELECTED';

        Hope this helps

        Matt


        Comment

        • Chung Leong

          #5
          Re: Arrays and Complex Indices

          "Matt Mitchell" <m_a_t_t_remove _the_underscore s@metalsponge.n et> wrote in
          message news:uKNQd.1003 43$B8.61613@fe3 .news.blueyonde r.co.uk...[color=blue]
          > 2 - cast the value to an integer
          > $selected[int($out['month']] = 'SELECTED';[/color]

          Should be $selected[(int) $out['month']]. int() is a function called "int".


          Comment

          • Matt Mitchell

            #6
            Re: Arrays and Complex Indices


            "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
            news:Xt6dnQW2Bu pofI7fRVn-1A@comcast.com. ..[color=blue]
            > "Matt Mitchell" <m_a_t_t_remove _the_underscore s@metalsponge.n et> wrote in
            > message news:uKNQd.1003 43$B8.61613@fe3 .news.blueyonde r.co.uk...[color=green]
            >> 2 - cast the value to an integer
            >> $selected[int($out['month']] = 'SELECTED';[/color]
            >
            > Should be $selected[(int) $out['month']]. int() is a function called
            > "int".
            >
            >[/color]

            Good point. Wasn't thinking.

            Matt


            Comment

            Working...