Php array and simple tutorial on using instances (easy to understand) _ PHP Tutorial

Source: Internet
Author: User
Php array and simple tutorial on using instances (easy to understand ). Note the text in the remarks section ~ Copy the code as follows :? Php instantiate an array $ arrayarray (). The array consists of the following two parts: array (key name [key] value [value]). Note the text in the comment section ~

The code is as follows:

// Instantiate an array
$ Array = array ();
// The array consists of the following two parts:
// Array (key name [key] => value [value])
// Key names and indexes
// The type can be int or string. [What is int? query the php Manual]
// You can write it like this
// $ Array = array (0 => 'A', 1 => 'B ');
// You can also write
// Array automatically adds the index key name. The default value is that the int value starts from 0.
$ Array = array ('A', 'B ');
// Echo cannot be used for testing. only print_r can be used to print arrays.
Print_r ($ array );
// The output result is Array ([0] => a [1] => B)
// It can be seen that if you do not set the key name [key], it will automatically add the key
// You can change the key at will.
$ Array = array (3 => 'A', 5 => 'B ');
Print_r ($ array );
// Result Array ([3] => a [5] => B)
// If you want to read the array content, you can do this
Echo $ array [3];
// The result is.
// Here echo is used because the echo output can be directly used as long as it is not an array.
// The key can be a string
$ Array = array ('a' => 'A', 'BB '=>' B ');
Print_r ($ array );
// The result is Array ([aa] => a [bb] => B)
// You can also echo $ array ['A']. Note that strings are enclosed by quotation marks.
// The value [value] can be a variable or an array
$ Array = array (0 => array ('A', 'B'), 1 => array ('C', 'D '));
Print_r ($ array );
// The result is Array ([0] => Array ([0] => a [1] => B) [1] => Array ([0] => c [1] => d ))
// This is called a two-dimensional array.
// Read the content.
Echo $ array [0] [1];
// The result is B.
// Of course, it can also contain more arrays
$ Array = array (0 => array ('A', 'B'), array ('C', 'D ')), 1 => array ('e', 'F'), array ('G', 'H ')));
// It looks a little messy and you need to understand it slowly
// Return to the actual application to instantiate a rent
$ Array = array ();
// Simulate an SQL loop. most SQL statements use a while loop. here I will make a simple for 10 cycles.
Echo'
'; Echo'
';
For ($ I = 0; $ I <= 10; $ I ++ ){
$ Array [] = array ('name' => 'My name'. $ I, 'age' => 'My age'. $ I );
// $ Array [] brackets are used to generate 10 arrays 0-10, respectively.
// If it is $ array = array ('name' => 'My name'. I, 'age' => 'My age'. I );
// Only one array is returned. The last one replaces the previous one.
}
Print_r ($ array );
// Result Array ([0] => Array ([name] => My name 0 [age] => my age 10) [1] => Array ([name] => My name 1 [age] => my age 11) [2] => Array ([name] => My name 2 [age] => my age 12) [3] => Array ([name] => My name 3 [age] => my age 13) [4] => Array ([name] => My name 4 [age] => my age 14) [5] => Array ([name] => My name 5 [age] => my age 15) [6] => Array ([name] => My name 6 [age] => my age 16) [7] => Array ([name] => My name 7 [age] => my age 17) [8] => Array ([name] => My name 8 [age] => my age 18) [9] => Array ([name] => My name 9 [age] => my age 19) [10] => Array ([name] => My name 10 [age] => my age 110 ))
// How to use it?
?>


    Foreach ($ array as $ value ){
    Echo'
  • '. $ Value ['name'].' | '. $ value ['age'].'
  • ';
    }
    ?>

// Html code is added to the preceding result.
// If you want to perform operations on the array, such as filtering out a total of 10 arrays in the above $ array, you need to delete the one with the key 3.
Unset ($ array [3]);
Print_r ($ array );
// You will see that 3 has no
// The above is a simple usage. next time I will talk about some advanced
?>

Bytes ~ The code is as follows :? Php // instantiate an array $ array = array (); // The array consists of the following two parts: // array (key name [key] = value [value]) // key...

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.