Web Analytics Made Easy -
StatCounter Help? - CodingForum

Announcement

Collapse
No announcement yet.

Help?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help?

    How do i spesify an array key when adding array elements dynamically in php?

    E.g.

    I am doing:

    $blah="hello";

    $array1[$blah][] = "hello2";

    But when i do

    print_r($array1);

    it does not do

    hello => hello2

    but

    [0] => hello2

    how can i work around this?

  • #2
    PHP Code:
    $array1[$blah]['hello'] = "hello2"
    shoud do it.

    Also, if you are defining an array, you can also do:

    PHP Code:

    $array 
    = array(
         
    'key1' => 'value1',
         
    'key2' => 'value2',
         
    'key3' => 'value3'
    );

    which will make an array like:

    $array['key1']  = 'value1'

    ect
    hope this helps
    PHP Weekly - A PHP Developers Resource
    PHP 5.1.4 and Ruby on Rails web hosting
    Moderator of PHP and Work offers and Requests
    Install Apache/PHP/MySQL
    (by marek_mar) | TinyPlugin Architecture

    Comment


    • #3
      Re: Help?

      Originally posted by R0bin
      How do i spesify an array key when adding array elements dynamically in php?

      E.g.

      I am doing:

      $blah="hello";

      $array1[$blah][] = "hello2";

      But when i do

      print_r($array1);

      it does not do

      hello => hello2

      but

      [0] => hello2

      how can i work around this?
      You need to read our posting guidelines section 2 before creating any more topics.
      Spookster
      CodingForum Supreme Overlord
      All Hail Spookster

      Comment


      • #4
        I read the guidelines, and could not think of anything more descriptive than "Help?", i suppose i could have put "Array Help?"

        missing-score you misunderstand what is wrong,

        I want to put the string index of the array in a variable. Since i am creating the array using dynamic infromation.

        PHP Code:
        $array1[$blah]['hello'] = "hello2"
        Is not what i wanted.

        I wanted

        PHP Code:
        $blah="hello";
        $array1[$blah][] = "hello2";
        $array1[$blah][] = "hello3";
        $blah="helloa";
        $array1[$blah][] = "helloa2";
        $array1[$blah][] = "helloa3";

        So that

        Array(
          
        'hello' => Array(
                            [
        0] = "hello2"
                            
        [1] = "hello2"
                          
        )
          
        'helloa' => Array(
                            [
        0] = "helloa2"
                            
        [1] = "helloa3"
                          
        )

        is what i wanted to happen, but is not happening.

        Comment


        • #5
          try something like

          PHP Code:

          $blah 
          "hello";

          $array[$blah] = array();

          array_push($array[$blah], "value1");
          array_push($array[$blah], "value2"); 
          thats how I would do it.

          Hope Im understanding you this time.
          PHP Weekly - A PHP Developers Resource
          PHP 5.1.4 and Ruby on Rails web hosting
          Moderator of PHP and Work offers and Requests
          Install Apache/PHP/MySQL
          (by marek_mar) | TinyPlugin Architecture

          Comment


          • #6
            Thanks, I will try.


            Thanks very much, it worked perfectly.
            Last edited by R0bin; Feb 21, 2004, 03:48 PM.

            Comment


            • #7
              no problem
              PHP Weekly - A PHP Developers Resource
              PHP 5.1.4 and Ruby on Rails web hosting
              Moderator of PHP and Work offers and Requests
              Install Apache/PHP/MySQL
              (by marek_mar) | TinyPlugin Architecture

              Comment

              Working...
              X