Home > Backend Development > PHP Tutorial > 玩转php数组(1)

玩转php数组(1)

WBOY
Release: 2016-06-13 12:51:53
Original
854 people have browsed it

玩转php数组(一)

<?php

//====初始化创建方式=====// 

$stu1['name']='binghui';
$stu1['sex']='男';
$stu1['age']='20';

print_r($stu1);
/*
Array
(
    [name] => binghui
    [sex] => 男
    [age] => 20
)
*/
// 初始化方式,便于运行中,临时增加一个单元
$stu1['height']='170';

print_r($stu1);
/*
Array
(
    [name] => binghui
    [sex] => 男
    [age] => 20
    [height] => 170
)

*/

//===array结构来创建=====// 

$stu1=array('name'=>'binghui','sex'=>'男','age'=>'20');

print_r($stu1);
/*
结果
Array
(
    [name] => binghui
    [sex] => 男
    [age] => 20
)
*/
?>
Copy after login
<?php

//// 数组创建,之懒人方式 
$flower = array('梅','兰','竹','菊');  
print_r($flower); 
/*
结果:
Array
(
    [0] => 梅
    [1] => 兰
    [2] => 竹
    [3] => 菊
)

*/
//马虎者创建的数组
$stu['学号'] = 234;
$stu['姓名'] = '李三';
$stu[] = 174;
$stu['年级'] = '高二';
$stu[] = '衡水';
print_r($stu);
/*
结果:
Array
(
    [学号] => 234
    [姓名] => 李三
    [0] => 174
    [年级] => 高二
    [1] => 衡水
)
*/
/*** 
如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值 + 1。// 上例从5+1开始 
如果当前还没有整数索引,则键名将为 0。// 在$yan中得到体现 
如果指定的键名已经有值了,该值将被覆盖。(见下例) 
***/ 
$butty[] = '林志玲';  // key = 0 
$butty[] = '仓井空';  // key = 1 
$butty[1] = '武藤兰'; // 指明key = 1单元,值为武藤兰,这其实是再次把1号柜的内容修改为"武藤兰" 
$butty[3] = '小泽xxx'; 
print_r($butty); 
/*
Array
(
    [0] => 林志玲
    [1] => 武藤兰
    [3] => 小泽xxx
)

*/
// 上面这3个例子,讨论清楚键的增长关系. 
$arr = array('中','华','民','国'); // 0 1 2 3 
print_r($arr); 

unset($arr[3]); 
print_r($arr); 

$arr[] = '族'; 
print_r($arr); // 键是:0124,删掉了一个单元,键的增长不受该影响,继续往上增长.
/*
Array
(
    [0] => 中
    [1] => 华
    [2] => 民
    [3] => 国
)
Array
(
    [0] => 中
    [1] => 华
    [2] => 民
)
Array
(
    [0] => 中
    [1] => 华
    [2] => 民
    [4] => 族
)

*/
?> 
Copy after login



 

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template