Jump to content

PHP arrays


billli

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/107154-php-arrays/#findComment-549432
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/107154-php-arrays/#findComment-549464
Share on other sites

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"

Link to comment
https://forums.phpfreaks.com/topic/107154-php-arrays/#findComment-549662
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.