array (size=2)
0 =>
array (size=7)
'id' => string '66' (length=2)
'room_name' => string 'Luxury' (length=6)
'no_of_room' => string '0' (length=1)
'price' => string '1000' (length=4)
'description' => string 'dfh dvsrtruub dsgd' (length=19)
'image' => string 'arrow.jpg' (length=9)
'hotel_id' => string '1' (length=1)
1 =>
array (size=7)
'id' => string '65' (length=2)
'room_name' => string 'Deluxe' (length=6)
'no_of_room' => string '4' (length=1)
'price' => string '800' (length=3)
'description' => string 'rybsvssfd' (length=9)
'image' => string '2P2Z4.png' (length=9)
'hotel_id' => string '1' (length=1)
这是我做var_dump时的数组。我想走数组并在每个数组中添加两个元素。我想要的是:
$checkin = "2014-2-4" //i will be changed according to textbox value.
array (size=2)
0 =>
array (size=8)
'id' => string '66' (length=2)
'room_name' => string 'Luxury' (length=6)
'no_of_room' => string '0' (length=1)
'price' => string '1000' (length=4)
'description' => string 'dfh dvsrtruub dsgd' (length=19)
'image' => string 'arrow.jpg' (length=9)
'hotel_id' => string '1' (length=1)
'check_in_date' => null
1 =>
array (size=8)
'id' => string '65' (length=2)
'room_name' => string 'Deluxe' (length=6)
'no_of_room' => string '2' (length=1)
'price' => string '800' (length=3)
'description' => string 'rybsvssfd' (length=9)
'image' => string '2P2Z4.png' (length=9)
'hotel_id' => string '1' (length=1)
'check_in_date' => null
array_walk($jsonArray, create_function('&$subarray', '$subarray[check_in_date] =$check_in;'));
在上面的array_walk函数中,当我使用$ check_in时,它给出了null但不是值。为什么有什么问题或者array_walk我没有用于我的目的。请帮我。提前谢谢。
答案 0 :(得分:0)
适用于PHP 5.3 +
如果您使用的是PHP 5.3+,则可以使用带有use
语法的匿名函数将变量导入函数范围:
array_walk($jsonArray, function (&$subarray) use ($checkin) {
$subarray['check_in_date'] = $checkin;
});
适用于旧版本的PHP
如果您使用的是旧版本的PHP,则可能会因使用create_function()
而陷入困境。您可以使用global
关键字将变量导入函数范围。请注意,这很糟糕,你真的应该考虑升级你的PHP版本,而不是使用这种方法:
array_walk($array, create_function(
'&$subarray',
'global $checkin; $subarray["check_in_date"] = $checkin;'
));