Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Array Problem

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Array Problem

    I am confused with the php arrays styles...
    I have written 3 types of (similar) arrays, but need to know the best and safest way to write the arrays below:

    Write it like:
    PHP Code:
    $mail = array(
    array(
    "URL" => "http://www.example.com/""title" => "example"), 
    array(
    "URL" => "http://www.other.com/""title" => "other"), 
    array(
    "URL" => "http://www.next.com/""title" => "next")
    ); 
    or
    PHP Code:
    $mail = array(array("URL" => "http://www.example.com/""title" => "example"), array("URL" => "http://www.other.com/""title" => "other"), array("URL" => "http://www.next.com/""title" => "next")); 
    or
    PHP Code:
    $mail = Array(Array("URL" => "http://www.example.com/""title" => "example")
    ,Array(
    "URL" => "http://www.other.com/""title" => "other")
    ,Array(
    "URL" => "http://www.next.com/""title" => "next")
    ); 
    Which is the safest way (the recommended way)?

    Any Suggestion Will Be Helpful
    Last edited by Tony M; 08-22-2011 at 03:18 AM.

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    The first one is the easiest for me to read and is a simple indexed array of associative arrays and so I would recommend that. I was taught with lowercase array() and so I stick with that

    Looping through it, if needed, is simple and to get the title of the second row is simply


    PHP Code:
    <?php
    $mail 
    = array(
    array(
    "URL" => "http://www.example.com/""title" => "example"),
    array(
    "URL" => "http://www.other.com/""title" => "other"),
    array(
    "URL" => "http://www.next.com/""title" => "next")
    );

    echo 
    $mail[1]['title'];  //outputs other

    ?>
    Last edited by webdev1958; 08-22-2011 at 05:01 AM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The above three are all equivalent. The first one is the best since it is easy to read, and keeping array in lowercase indicates that it is not an object type.


  •  

    Posting Permissions

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