billli Posted May 25, 2008 Share Posted May 25, 2008 Hi, for example: $a=array("one", "two", "three"); $b=array("one"=>"one", "two"=>"two", "three"=>"three"); how should the elements in the array be accessed? echo $a[1]; echo $a['1']; echo $a["1"]; echo $b[one]; echo $b['one']; echo $b["one"]; i know these works, but which one is the standard? thanks Quote Link to comment Share on other sites More sharing options...
Xurion Posted May 25, 2008 Share Posted May 25, 2008 Either: $array[1] or $array['one'] or $array["one"] Using $array[one] won't cause an error, but it will put a PHP notice in the error logs, an assume you mean 'one'. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted May 25, 2008 Share Posted May 25, 2008 You can assign a key to an array. <?php $array_1 = array("Value 1", "Value 2", "Value 3"); $array_2 = array("One" => "Value 1", "Two" => "Value 2", "Three" => "Value 3"); $array_3 = array("One" => "Value 1", "Two" => array( "One" => "Value 1", "Two" => "Value 2", "Three" => "Value 3" ), "Three" => "Value 3"); ?> If you've assigned a string as a key, then it should be called as a string. print $array_2["Three"]; // Value 3. If you haven't assigned a key to an array or the key you've assigned is an integer then you call it as one. print $array_1[2] // Value 2, also used for selecting individual values from arrays. If you have a nested array, the same applies. print $array_3[1]; // Value 1. print $array_3["Two"]["Three"]; // Value 3. print $array_3["One"]; // Value 1. print $array_3["One"][2]; // Value 2. print $array_3[2]["One"] // Value 1. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 25, 2008 Share Posted May 25, 2008 When you us $array[one], php takes about 20 or more times longer to reference an array value because it must first determine if one is a predefined constant by looking in the table of predefined constants, then when it does not find an entry it goes through the error response code (notices, warnings, and errors are all errors, but of different levels) to figure out if it should log and/or display the error, then executes the code necessary to write to the log file and/or output to the browser, then it checks if 'one' exists as an index in $array[], repeat the error response code if not found, and finally reference or set the value. By using only $array['one'] or $array["one"], the php only needs to check if 'one' exists as an index in $array[], repeat the error response code if not found, and reference or set the value. Edit: For each of your examples - echo $a[1]; // correct echo $a['1']; // works but php converts the string '1' to a number, taking some extra time. echo $a["1"]; // same as '1' + parsing double-quoted strings takes slightly longer than single-quote echo $b[one]; // see my comments above echo $b['one']; // correct echo $b["one"]; // correct + parsing double-quoted string takes slightly longer than single-quote Quote Link to comment Share on other sites More sharing options...
billli Posted May 25, 2008 Author Share Posted May 25, 2008 Thanks for all the replies, How should the following be accessed? $ka=1; $kb="one"; $a=array("one", "two", "three"); $b=array("one"=>"one", "two"=>"two", "three"=>"three"); echo $a[$ka]; echo $b["$kb"]; which one would be the standard one? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 25, 2008 Share Posted May 25, 2008 Use these - echo $a[$ka]; // $ka contains the number 1 echo $b[$kb]; // $kb is a string containing "one" These work but in this example would be slower than the above as the variable inside of the double-quoted string requires extra processing - echo $a["$ka"]; // "$ka" evaluates to "1", which is then converted to the number 1 echo $b["$kb"]; // "$kb" evaluates to "one" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.