目录
一、初始化创建数组:
1. 简单数组:
2. 索引号是字符串数组(对象数组):
3. 数组中增加一个元素:
4. 对象数组转JSON字符串
5. JSON字符串转数组
一、初始化创建数组:
1. 简单数组:
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
返回结果:I like Volvo, BMW and Toyota.
2. 索引号是字符串数组(对象数组):
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
返回结果:Peter is 35 years old.
3. 数组中增加一个元素:
// 简单数组添加元素到数组末尾
array_push($cars, '比亚迪'); //对象数组末尾增加元素$newElement = array('sensor' => '55');
$arr = array_merge($arr, $newElement);
4. 对象数组转JSON字符串
$jsonStr = json_encode($age);
echo $jsonStr
输出结果:
{"Peter": "35","Ben": "37","Joe": "43","sensor": "55"
}
5. JSON字符串转数组
$data = json_decode($jsonStr)
echo $data['sensor']
输出结果:55
至于修改数组的值就很简单了,按照索引号修改就可以了。