Home  >  Article  >  Backend Development  >  Explain the methods of PHP array classification and array creation

Explain the methods of PHP array classification and array creation

jacklove
jackloveOriginal
2018-06-08 11:14:361587browse

Array classification and array creation play an important role in PHP.

1. Array classification

1. Index array

Array whose index value is an integer

2.Associative array

Index value For arrays of strings, use strings as indexes, which makes programming more user-friendly!

This is very rare in other programming languages, but it will be widely used in PHP during the development process.

It is extremely convenient to use!

2. Array creation

Creating arrays in PHP is very flexible. Unlike many other programming languages, PHP does not need to

specify the size of the array when creating it. You can store any type of data in the same array without even declaring it before using it.

. You can create an array by directly assigning values ​​to the array elements.

. Use the array() language structure to create an array.

1. Create an array by directly assigning values ​​​​to the array elements

Variable name [index value] = data content; the index value can be an integer or a string, or it must be written (default For the index array) 2. Use the array() language structure to create the array variable name =array(key1=>value1,...);

具体的值,.......);
 $student=array(10,'傻逼',true,60.5);//一维数组 
 var_dump($student); 
 $student1=array( 0=>10, 1=>'傻逼', 2=>true, 3=>60.5 ); 
 var_dump($student1);
  $student2=array( 'num'=>11, 'name'=>'菜逼', 'sex'=>true, 'grade'=>80.5, 10=>'dqwdwqdwq' ); 
  var_dump($student2); ?>
array(1,'傻逼',true,60.5),
    1=>array(2,'菜逼',true,80.5),
    2=>array(3,'坑逼',false,85.5)
);
/*
$students=array(
        array(1,'傻逼',true,60.5),
        array(2,'菜逼',true,80.5),
        array(3,'坑逼',false,85.5)
);
*/ 
var_dump($students);
echo $students[0][1];
?>

This article explains PHP array classification and array creation methods. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Explanation on PHP language tags, command delimiters, and comments



##Explain the predefined super-global array variables of PHP arrays


Explain the use of pdo placeholders in PHP

The above is the detailed content of Explain the methods of PHP array classification and array creation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn