Hướng dẫn undefined index array php
As noted my comments on the original post, after fixing the missing bracket (assuming typo) in your array declaration, the function works correctly, if called correctly as Basic debugging aside, let me help you tidy up this code a bit. First,
You'd use it like this:
Here, the use of a constant for your dictionary is a basic but very efficient way to handle a global need for pre-defined data. Constants are parsed at compile-time, while variable declarations are parsed at runtime. They are available in all contexts. And you definitely don't want to have the whole A more elegant way to handle this would be to create a class, which will allow you to avoid the language function parameter. (Class constants and properties are like "local globals", available to all methods in the class.) Here's a sample application of your case as a class, where the dictionary is a class constant, and the language is a class property:
If you expect to have a large dictionary that needs to be accessed in other contexts, or simply wish to centralize your language reference, then you may want to define it as a regular constant outside the class, as in the first example. I've used a class constant here mostly for educational purposes. Hope this helps you forward and towards cleaner code. 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. Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic. SyntaxSpecifying with array() An array
can be created using the array() language construct. It takes any number of comma-separated array( key => value, key2 => value2, key3 => value3, ... ) The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e.
Example #1 A simple array
The key can either be an int or a string. The value can be of any type. Additionally the following key casts will occur:
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten. Example #2 Type Casting and Overwriting example
The above example will output: array(1) { [1]=> string(1) "d" } As all the keys in the above example are cast to PHP arrays can contain int and string keys at the same time as PHP does not distinguish between indexed and associative arrays. Example #3 Mixed int and string keys
The above example will output: array(4) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" [100]=> int(-100) [-100]=> int(100) } The key is optional. If it is not specified, PHP will use the increment of the largest previously used int key. Example #4 Indexed arrays without key
The above example will output: array(4) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(5) "hello" [3]=> string(5) "world" } It is possible to specify the key only for some elements and leave it out for others: Example #5 Keys not on all elements
The above example will output: array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [6]=> string(1) "c" [7]=> string(1) "d" } As you can see the last value Example #6 Complex Type Casting and Overwriting example This example includes all variations of type casting of keys and overwriting of elements.
The above example will output: array(7) { [1]=> string(1) "g" [-1]=> string(1) "d" ["01"]=> string(1) "e" ["1.5"]=> string(1) "f" [0]=> string(1) "h" [""]=> string(1) "j" [2]=> string(1) "l" } Accessing array elements with square bracket syntax Array elements can be
accessed using the Example #7 Accessing array elements
The above example will output: string(3) "bar" int(24) string(3) "foo"
Example #8 Array dereferencing
Creating/modifying with square bracket syntaxAn existing array can be modified by explicitly setting values in it. This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ( $arr[key] = value; $arr[] = value; // key may be an int or string // value may be any value of any type If $arr doesn't exist yet or is set to
To change a certain value, assign a new value to that element using its key. To remove a key/value pair, call the unset() function on it.
Array destructuring Arrays can be destructured using the
$foo, $bar, $baz] = $source_array; Array destructuring can be used in foreach to destructure a multi-dimensional array while iterating over it.
$source_array as [$id, $name]) { Array elements will be ignored if
the variable is not provided. Array destructuring always starts at index
$baz; // prints "baz" As of PHP 7.1.0, associative arrays can be destructured too. This also allows for easier selection of the right element in numerically indexed arrays as the index can be explicitly specified.
$three; // prints 3$source_array = ['foo', 'bar', 'baz'];// Assign the element at index 2 to the variable $baz Array destructuring can be used for easy swapping of two variables.
$b, $a] = [$a, $b];
Useful functionsThere are quite a few useful functions for working with arrays. See the array functions section.
The foreach control structure exists specifically for arrays. It provides an easy way to traverse an array. Array do's and don'tsWhy is |