Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用函数和FOR循环创建JSON_Php_Json - Fatal编程技术网

Php 使用函数和FOR循环创建JSON

Php 使用函数和FOR循环创建JSON,php,json,Php,Json,我试图使用for循环和函数创建json响应,但似乎无法使其正常工作。如何做到这一点 PHP 函数的作用是:将PHP值转换为json值。例如,从PHP数组中,它可以创建该数组的JSON表示 $count = 0; foreach ($bookings as $booking){ $item = get_users_custom($user,$count); $item[$count]['booking_id']=$booking->id; $count++; } echo json_enco

我试图使用for循环和函数创建json响应,但似乎无法使其正常工作。如何做到这一点

PHP

函数的作用是:将PHP值转换为json值。例如,从PHP数组中,它可以创建该数组的JSON表示

$count = 0;
foreach ($bookings as $booking){
$item = get_users_custom($user,$count);
$item[$count]['booking_id']=$booking->id;
$count++;
}
echo json_encode($item);


function get_users_custom($user,$count=0){
$feed[$count]['user_id']=$user->id; 
return $feed;
}

您应该创建一个简单的php数组,并使用JSON\u encode()


where is json??在循环中每次运行时,项目都会被覆盖,因为$Item将只保存最后一次预订。每次预订都会覆盖它。
$count = 0;
foreach ($bookings as $booking){
$item = get_users_custom($user,$count);
$item[$count]['booking_id']=$booking->id;
$count++;
}
echo json_encode($item);


function get_users_custom($user,$count=0){
$feed[$count]['user_id']=$user->id; 
return $feed;
}