PHP Development notes series (9)-array (1)

Source: Internet
Author: User
Tags array definition
PHP Development notes series (9)-array (1 )??? Recently, when I was working on a project, I often needed to process associated arrays. I found many built-in array processing functions in PHP, which are very convenient to use and efficient. I need to repeat them, as the ninth article in the PHP Development notes series (XAMPP + PhpEclipse + XDebug), the PHP Development notes series (9)-array (1)

??? Recently, when I was working on a project, I often needed to process associated arrays. I found many built-in array processing functions in PHP, which are very convenient to use and efficient. I need to repeat them, as the ninth article in the PHP Development notes series (XAMPP + PhpEclipse + XDebug), the PHP Development notes series (9)-array (1) records operations related to the PHP array.


??? 1. Php array definition

??? The Php array is similar to the Map concept in JAVA. elements in the array are distinguished by a special identifier, which is called a Key. each Key corresponds to a Value ). Therefore, a combination of Key and Value forms an element in the array. Elements in the Php array are flexible. each element does not have to be of the same type, such as integer or string.


??? 2. Php array assignment

???? Php keys can be numeric keys or associative keys ). The value Key has no real relationship with the value, but the value is in the position of the array, and the associated key points to the value.

?

$data[0] = '1st value'; $data[1] = '2nd value'; $data[2] = '3rd value';$data['1st'] = '1st value'; $data['2nd'] = '2nd value'; $data['3rd'] = '3rd value';echo $data[0];echo $data['3rd'];

?

??? 3. multi-dimensional array of Php

???? Php arrays can be checked in to form multi-dimensional arrays. You can define it as follows:

$data['school1']['grade1'] = 'grade 1'; $data['school1']['grade2'] = 'grade 2';$data['school1']['grade3'] = 'grade 3';echo $data['school1']['grate1'];

?

??? 4. create an array

??? In addition to using the above method to create an array, you can also create an array by using the constructor array (), as shown below:

// Create an empty array $ data = array (); // create a value key non-empty array $ numeric_data = array ('1st value', '2nd value ', '3rd value'); // create a non-empty array of the association key $ map_data = array ('1st' => '1st value', '2nd' => '2nd value ', '3rd '=> '3rd value'); // create an array $ multi_map_data = array ('S1' => array ('G1' => 'grade1 ', 'G2 '=> 'grade2'), 's2' => array ('g3 '=> 'grade3', 'G4 '=> 'grade4 '));

?

??? 5. use list () to extract an array

??? The list () function is similar to array (), but it can extract multiple values from a number Group in one operation and assign values to multiple variables at the same time. This construction is particularly useful when extracting information from a database or file.


??? For example, if a file named school.txt needs to read information from the file, each row of the file contains the name, grade, class, student name, and other information. each item is separated by commas (,). For example

file:array-list.txtSchool1,grade1,class1,jackSchool1,grade1,class2,dannySchool1,grade2,class1,mikeSchool1,grade2,class2,lilySchool2,grade1,class1,dickSchool2,grade1,class2,marySchool2,grade2,class1,johnySchool2,grade2,class2,smart... ...
?

?

??? You can use list () in a simple loop to read each row, assign each part of the data to the variable, and format and output the data as needed. the code is as follows:

?

file: array-list.phpurl: http://localhost:88/array/array-list.php
 ';    }        fclose($fp);?>
?

??? 6. test whether the variable is an array.

??? When using arrays, you sometimes need to use a specific variable to determine whether it is an array. The built-in function is_array () can determine whether the variable is an array. if it is TRUE, FALSE is returned.

?

file: is_array.phpurl: http://localhost:88/array/is_array.php
 1);        echo is_array($arr) ? 'TRUE' : 'FALSE';    echo '
'; echo is_array($arr1) ? 'TRUE' : 'FALSE'; echo '
'; echo is_array($arr2) ? 'TRUE' : 'FALSE'; echo '
'; ?>

?

??? 7. output array

??? There are many ways to output arrays. you can use foreach, for, and while to traverse array elements. the code is as follows:

file: output-array.phpurl: http://localhost:88/array/output-array.php
 ';    }        echo '======================'.'
'; $i = 0; while ($i < count($data)) { echo $data[$i].'
'; $i++; } echo '======================'.'
'; for ($i = 0; $i < count($data); $i++) { echo $data[$i].'
'; } echo '======================'.'
'; $map['1st'] = '1st value'; $map['2nd'] = '2nd value'; $map['3rd'] = '3rd value'; foreach ($map as $key => $value) { echo $key.':'.$value.'
'; } ?>

?

??? Write it here today and continue the next day.


?? Address: http://ryan-d.iteye.com/blog/1566123

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.