PHP Array Summary chapter (i) _php tips

Source: Internet
Author: User

Array
1. The subscript of an array is an integer value or a string type.
Eg1. The key of the indexed array is ___, and the key of the associative array is ___.
2. When the string is indexed, enclose the quotation marks. A constant or variable cannot be compiled without quotation marks.
In PHP, a string without quotes will automatically generate a raw string, and PHP may define the constant later, unfortunately, if you have the same name in your code, the string is then assigned a new value.
eg2.<?php
Show All Errors
Error_reporting (E_all);
$arr = Array (' Fruit ' => ' apple ', ' veggie ' => ' carrot ');
That's right
Print $arr [' fruit ']; Apple
Print $arr [' Veggie ']; Carrot
Not correct. This works but also throws a PHP error of
Level e_notice because of undefined constant named fruit
//
Notice:use of undefined constant fruit-assumed ' fruit ' in ...
Print $arr [fruit]; Apple
Let ' s define a constant to demonstrate what ' s going on. We
Would assign value ' veggie ' to a constant named fruit.
Define (' fruit ', ' veggie ');
Notice the difference now
Print $arr [' fruit ']; Apple
Print $arr [fruit]; Carrot
The following is okay as it ' s inside a string. Constants are not
Looked for within strings so no e_notice error here
print "Hello $arr [fruit]"; Hello Apple
With one exception, braces surrounding arrays within strings
Allows constants to is looked for
Print "Hello {$arr [fruit]}"; Hello Carrot
Print "Hello {$arr [' Fruit ']}"; Hello Apple
This is not work, results in a parse error such as:
Parse error:parse error, expecting t_string ' or t_variable ' or t_num_string '
This is course applies to using autoglobals in strings as
print "Hello $arr [' Fruit ']";
Print "Hello $_get[' foo ']";
Concatenation is another option
Print "Hello". $arr [' Fruit ']; Hello Apple
?>
3. Key value problem
$a [' color '] = ' red ';
$a [' taste '] = ' sweet ';
$a [' shape '] = ' round ';
$a [' name '] = ' apple ';
$a [] = 4; Key would be 0
$b [] = ' a '; Key would be 0
$b [] = ' B '; Key would be 1
$b [] = ' C '; Key would be 2
switching = Array (ten,//key = 0
5 => 6,
3 => 7,
' A ' => 4,
One,//key = 6 (maximum of integer-indices was 5)
' 8 ' => 2,//key = 8 (integer!)
' => ',//key = ' 02 '
0 =>//The value of is overwritten by 12
);
<?php
$multi _array = Array ("Red",
"Green",
=> "Blue", "Yellow" => Array ("Apple", 9 => "pear", "banana", "Orange" => Array ("Dog", "Cat", "Iguana"));
?>
A $multi _array[' yellow ' [' apple '][0]
B $multi _array[' blue '][0][' orange '][1]
C $multi _array[3][3][2]
D $multi _array[' yellow ' [' orange '] [' cat ']
E. $multi _array[' yellow ' [' Orange '][1]
--------------------------------to be continued------
4.array_walk
5.var_dump
6.array_intersect
7.array_sum
8.array_count_values
9.array_flip
10.natsort
11.ksort (), Asort (), Krsort (), sort (), Usort ()
12.array_reverse ()
13.array_merge
14.reset
-------------------------------to be continued------
15.array_combine
16array_count_values
17.array_diff
18.array_filter
19.array_search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.