我对PHP数组有一个非常蹩脚的问题,我试图获取[extras] []的所有值,在这种情况下它们是1,2,3,4,这里是print_r($ array)的输出:
Array
(
[0] => Array
(
[name] => form[pickupDate][day]
[value] => 1
)
[1] => Array
(
[name] => form[pickupDate][month]
[value] => 10
)
[2] => Array
(
[name] => form[pickupTime][hour]
[value] => 0
)
[3] => Array
(
[name] => form[returnDate][day]
[value] => 1
)
[4] => Array
(
[name] => form[returnDate][month]
[value] => 1
)
[5] => Array
(
[name] => form[returnTime][hour]
[value] => 0
)
[6] => Array
(
[name] => form[car]
[value] => 1
)
[7] => Array
(
[name] => form[pickupAddress]
[value] =>
)
[8] => Array
(
[name] => form[agency]
[value] => 1
)
[9] => Array
(
[name] => form[extras][]
[value] => 1
)
[10] => Array
(
[name] => form[extras][]
[value] => 2
)
[11] => Array
(
[name] => form[extras][]
[value] => 3
)
[12] => Array
(
[name] => form[extras][]
[value] => 4
)
[13] => Array
(
[name] => form[specialPrice]
[value] =>
)
)
你可以使用这个JSON字符串来播放我的数据,之后将它转换为PHP数组,如下所示:
$request = '[{"name":"form[pickupDate][day]","value":"1"},{"name":"form[pickupDate][month]","value":"8"},{"name":"form[pickupTime][hour]","value":"0"},{"name":"form[returnDate][day]","value":"1"},{"name":"form[returnDate][month]","value":"1"},{"name":"form[returnTime][hour]","value":"0"},{"name":"form[pickupAddress]","value":""},{"name":"form[agency]","value":"1"},{"name":"form[extras][]","value":"1"},{"name":"form[extras][]","value":"2"},{"name":"form[extras][]","value":"3"},{"name":"form[extras][]","value":"4"},{"name":"form[specialPrice]","value":""}]';
$array = json_decode($request,true);
我已经尝试过这个,但它产生的字符串(1)" 4":
$result = array_column($array, null, 'name')['form[extras][]']['value'];
var_dump($result);
答案 0 :(得分:1)
你有一个多维数组,这是一个由多个数组组成的数组。
monitor.sh &
这就是为什么你必须遍历每个元素,例如使用这个函数(你已经在PHP中定义了一个> 5.5.0):
array->[0]-[1]-[2]...
| |
| [name]-[value]
|
array-> [name]-[value]
编辑: 用于根据名称获取特定数组子集的值:
function array_column($array, $value)
{
$result = array();
foreach($array as $element)
{
$result[] = $element[$value];
}
return $result[];
}