Jump to content

PHP arrays


billli

Recommended Posts

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

Link to comment
Share on other sites

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
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
Share on other sites

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?

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.