...

View Full Version : Array Problem



Tony M
08-22-2011, 03:15 AM
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:


$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


$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


$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 :thumbsup:

webdev1958
08-22-2011, 04:51 AM
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
$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

?>

Fou-Lu
08-22-2011, 04:57 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum