Web Analytics Made Easy -
StatCounter Array Problem - CodingForum

Announcement

Collapse
No announcement yet.

Array Problem

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

  • 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; Aug 21, 2011, 09:18 PM.

  • #2
    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; Aug 21, 2011, 11:01 PM.

    Comment


    • #3
      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.
      PHP Code:
      header('HTTP/1.1 420 Enhance Your Calm'); 
      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

      Comment

      Working...
      X