Sinisterly
<help wanted> best practice to define an array - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: PHP (https://sinister.li/Forum-PHP)
+--- Thread: <help wanted> best practice to define an array (/Thread-help-wanted-best-practice-to-define-an-array)



<help wanted> best practice to define an array - jabberwock - 11-18-2012

what is the way that I could define an array:
1.
PHP Code:
$arrayTest [1] ="one"; $arrayTest [2] ="two";

2.
PHP Code:
$arrayTest = array(1=>"one",2="two");

thanks


RE: <help wanted> best practice to define an array - unixbreak - 11-18-2012

well the main advantage of option 2 is that it ensures that the array has no other unexpected values in it. My personal preference for laying out arrays with a lot of initial entries is:

PHP Code:
$arrayTest = array( 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', );


I also always add a comma at the end of the last entry. PHP will ignore it and I won't forget it when I go to add another item to the end of the list!

so i suggest to use the second option !


RE: <help wanted> best practice to define an array - Mediocrity_mybb_import6293 - 11-18-2012

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
PHP Code:
<?php $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); print_r($a); ?>



RE: <help wanted> best practice to define an array - jabberwock - 11-18-2012

geez guys
i just got fastest reply since ever

Grand thanks both of you


RE: <help wanted> best practice to define an array - unixbreak - 11-18-2012

(11-18-2012, 12:27 AM)jabberwock Wrote: geez guys
i just got fastest reply since ever

Grand thanks both of you

he he
well what can i said ..
may the PHPknowledge be with you =))


RE: <help wanted> best practice to define an array - The Alchemist - 11-18-2012

As said above, you can do that... And just one thing I'd like to tell you..
PHP has a very good advantage... You can set arrays using this :
PHP Code:
$arraytest = range(1,100);

It initializes all the values from 1 to 100 in the array...

You may use it sometimes..


RE: <help wanted> best practice to define an array - LightX - 11-18-2012

Gosh that freakin blue messes with my eyes...