PHP Development Notes Series (ix)-Array (i)

Source: Internet
Author: User
PHP Development Note Series (ix)-Array (i)

??? Recently in the project, often need to use the correlation array processing, found that PHP has a lot of self-contained array processing functions, easy to use, and efficient, re-organized, as a "PHP Development Note Series (XAMPP+PHPECLIPSE+XDEBUG)" the Nineth chapter, "PHP Development Note Series (ix)-Array (a)", records the PHP array related operations.


??? 1. Array definitions for PHP

??? An array of PHP is similar to the concept of map in Java, where elements in an array are distinguished by a special identifier, called a key, and each key corresponds to a value. A combination of a key (key) and a value (value), therefore, makes up an element in the array. The elements in the PHP array are flexible, and each element does not have to be of the same type, such as integers, strings, and so on.


??? 2. Array Assignment for PHP

???? The key for PHP can be a numeric key (numerical), or it can be an association key (associative). The numeric key does not really relate to the value, just the position of the value in 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. A multidimensional array of PHP

???? An array of PHP can be checked in to form a multidimensional array. Can be defined in the following ways:

$data [' school1 '] [' grade1 '] = ' Grade 1 '; $data [' school1 '] [' grade2 '] = ' Grade 2 '; $data [' School1 '] [' grade3 '] = ' Grade 3 '; Echo $data [' school1 '] [' grate1 '];

?

??? 4. Creating an array

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

Create an empty array $data = Array ();//create numeric key non-empty array $numeric_data = Array (' 1st value ', ' 2nd value ', ' 3rd value ');//Create Association key non-empty array $map_data = a Rray (' 1st ' = ' 1st value ', ' 2nd ' = ' 2nd value ', ' 3rd ' = ' 3rd value ');//create array $multi_map_data = Array (' s1 ' = Array (' G1 ' = ' grade1 ', ' g2 ' = ' grade2 '), ' S2 ' =>array (' g3 ' = ' grade3 ', ' g4 ' = ' grade4 '))  ;

?

??? 5. Extracting an array using list ()

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


??? For example, a file school.txt need to read information from the file, each line of the file contains the learning name, grade, class, student name and other information, each use "," to split, such as

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 () to read each row through a simple loop, assign each part of the data to a variable, format and output the data as needed, and the code is as follows:

?

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

??? 6. Test whether variables are arrays

??? When working with arrays, it is sometimes necessary to use a particular variable as an array. The built-in function Is_array () can tell if a variable is an array, or FALSE if it returns true.

?

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

?

??? 7. Output array

??? There are many ways to output an array, and you can use foreach, for, and while to iterate through the array elements 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 this today and go on another day.


?? This 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.