Results 1 to 3 of 3

Thread: easy multidimensional array problem

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default easy multidimensional array problem

    I can't seem to get the following to work:
    PHP Code:
    <?php
    $ar
    [0][0]="mess";
    echo
    "$ar[0][0]";
    ?>
    My end goal is to try to turn an array into a multidimensional array. Say I have an array

    $ar=array(me ss, w e, as df, e d);

    I want to explode it into a multidimensional array where $ar[0][0]=me and $ar[0][1]=ss.

    Any ideas on these two problems?

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    <?php
    $ar=array('me ss','w e','as df','e d');
    $ar[0]=explode(' ',$ar[0]);
    print_r($ar);
    ?>
    The above will turn an array into a multidimensional array.

    Still don't know why
    PHP Code:
    <?php 
    $ar
    [0][0]="mess"
    echo
    "$ar[0][0]"
    ?>
    is not working.

    print($arry[0][0]); works though. I'll probably figure it out quickly enough. Just lazy tonight I guess.

    EDIT: for some reason removing the quotes in the echo allows the echo to work just fine.
    Last edited by james438; 09-13-2008 at 06:14 AM.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Adding quotes around a variable where they are not needed is pointlessly inefficient. You've already got the value you want, but then you're creating a string and interpolating the value into it — in order to get back the same value.

    In this case, it didn't work because array-style variable access is not supported in double quotes by default. You have to wrap it in braces to distinguish it from the rest of the string: print "{$ar[0][0]}";.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. The Following User Says Thank You to Twey For This Useful Post:

    james438 (09-13-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •