array-key

Is it okay to use array[key] in PHP?

痴心易碎 提交于 2019-11-26 17:48:10
Is it okay to use array without single or double quotion like $array[key]? I thought it is bad because PHP look for constant first if I don't use single or double quotation. One of my colleagues told me that it does not matter. What do you guys think? It is not considered as OK -- even if it will work in most cases. Basically, when PHP sees this : echo $array[key]; It will search for a constant, defined with define , called key -- and, if there is none, if will take the 'key' value. But, if there is something like this earlier in your code : define('key', 'glop'); It will not take echo $array[

PHP - Get key name of array value

﹥>﹥吖頭↗ 提交于 2019-11-26 10:13:09
问题 I have an array as the following: function example() { /* some stuff here that pushes items with dynamically created key strings into an array */ return array( // now lets pretend it returns the created array \'firstStringName\' => $whatEver, \'secondStringName\' => $somethingElse ); } $arr = example(); // now I know that $arr contains $arr[\'firstStringName\']; I need to find out the index of $arr[\'firstStringName\'] so that I am able to loop through array_keys($arr) and return the key

array_key_exists is not working

点点圈 提交于 2019-11-26 08:30:35
问题 array_key_exists is not working for large multidimensional array. For ex $arr = array( \'1\' => 10, \'2\' => array( \'21\' => 21, \'22\' => 22, \'23\' => array( \'test\' => 100, \'231\' => 231 ), ), \'3\' => 30, \'4\' => 40 ); array_key_exists(\'test\',$arr) returns \'false\' but it works with some simple arrays. 回答1: array_key_exists does NOT work recursive (as Matti Virkkunen already pointed out). Have a look at the PHP manual, there is the following piece of code you can use to perform a

Is it okay to use array[key] in PHP?

橙三吉。 提交于 2019-11-26 05:38:19
问题 Is it okay to use array without single or double quotion like $array[key]? I thought it is bad because PHP look for constant first if I don\'t use single or double quotation. One of my colleagues told me that it does not matter. What do you guys think? 回答1: It is not considered as OK -- even if it will work in most cases. Basically, when PHP sees this : echo $array[key]; It will search for a constant, defined with define, called key -- and, if there is none, if will take the 'key' value. But,