Php 向json文本字符串添加动态数据
我有这个json文本字符串Php 向json文本字符串添加动态数据,php,json,Php,Json,我有这个json文本字符串 { "properties_form_data": { "user_id":"edhdh83883737364623", "property_owner":"rammstein", "property_name": "rockers", &
{
"properties_form_data": {
"user_id":"edhdh83883737364623",
"property_owner":"rammstein",
"property_name": "rockers",
"property_address":"hard metal",
"property_coordinates": true,
"property_type": true,
"property_country": true,
"property_district": true,
"property_region": "description",
"property_city": "description",
"property_profile": "description",
"checkin_time": "description",
"checkin_out_time": "description which is "afraid"",
"pets_allowed": false,
"age_restriction_for_checkin": true,
"property_images":{"property_s3_image_links":"1.jpg,2.jpg,3.jpg","date_updated":"95356373773"},
"property_amenities":{"together":true,"prospering":true,"touring_right_now":false},
"sorrounding_areas":[{
"album_id": "23883838dhhskk89968654s",
"album_name":"Rammstein",
"date_taken":"35356373773"
}],
"restaurants":[{
"album_id": "23883838dhhskk89968654s",
"album_name":"Rammstein",
"date_taken":"35356373773"
}],
"date_inserted":"hard metal",
"updated_on": true
}
}
<?php
$no = '';
$h = rand(6,99999);
$newarr = array(6,9,8,0,7);
print_r($newarr);
for ($x = 0; $x <= 10; $x++) {
$no = '{
"album_id": "'.$x.'",
"album_name":"Rammstein",
"date_taken":"35356373773"
}';
}
$jl ='{
"properties_form_data": {
"user_id":"edhdh83883737364623",
"property_owner":"rammstein",
"property_name": "'.$h.'",
"property_address":"hard metal",
"property_coordinates": true,
"property_type": true,
"property_country": true,
"property_district": true,
"property_region": "description",
"property_city": "description",
"property_profile": "description",
"checkin_time": "description",
"checkin_out_time": "description which is "afraid"",
"pets_allowed": false,
"age_restriction_for_checkin": true,
"property_images":{"property_s3_image_links":"1.jpg,2.jpg,3.jpg","date_updated":"95356373773"},
"property_amenities":{"together":true,"prospering":true,"touring_right_now":false},
"sorrounding_areas":['.$no.'],
"restaurants":[{
"album_id": "23883838dhhskk89968654s",
"album_name":"Rammstein",
"date_taken":"35356373773"
}],
"date_inserted":"hard metal",
"updated_on": true
}
}';
echo '<pre>';
echo $jl;
echo '</pre>';
?>
我从客户端获取了一些数据,我希望将其添加到字符串中,然后对其进行编码并将其存储起来。我接收了几个数据集作为数组,我还希望通过循环和编码生成的json文本字符串来添加这些数据集
{
"properties_form_data": {
"user_id":"edhdh83883737364623",
"property_owner":"rammstein",
"property_name": "rockers",
"property_address":"hard metal",
"property_coordinates": true,
"property_type": true,
"property_country": true,
"property_district": true,
"property_region": "description",
"property_city": "description",
"property_profile": "description",
"checkin_time": "description",
"checkin_out_time": "description which is "afraid"",
"pets_allowed": false,
"age_restriction_for_checkin": true,
"property_images":{"property_s3_image_links":"1.jpg,2.jpg,3.jpg","date_updated":"95356373773"},
"property_amenities":{"together":true,"prospering":true,"touring_right_now":false},
"sorrounding_areas":[{
"album_id": "23883838dhhskk89968654s",
"album_name":"Rammstein",
"date_taken":"35356373773"
}],
"restaurants":[{
"album_id": "23883838dhhskk89968654s",
"album_name":"Rammstein",
"date_taken":"35356373773"
}],
"date_inserted":"hard metal",
"updated_on": true
}
}
<?php
$no = '';
$h = rand(6,99999);
$newarr = array(6,9,8,0,7);
print_r($newarr);
for ($x = 0; $x <= 10; $x++) {
$no = '{
"album_id": "'.$x.'",
"album_name":"Rammstein",
"date_taken":"35356373773"
}';
}
$jl ='{
"properties_form_data": {
"user_id":"edhdh83883737364623",
"property_owner":"rammstein",
"property_name": "'.$h.'",
"property_address":"hard metal",
"property_coordinates": true,
"property_type": true,
"property_country": true,
"property_district": true,
"property_region": "description",
"property_city": "description",
"property_profile": "description",
"checkin_time": "description",
"checkin_out_time": "description which is "afraid"",
"pets_allowed": false,
"age_restriction_for_checkin": true,
"property_images":{"property_s3_image_links":"1.jpg,2.jpg,3.jpg","date_updated":"95356373773"},
"property_amenities":{"together":true,"prospering":true,"touring_right_now":false},
"sorrounding_areas":['.$no.'],
"restaurants":[{
"album_id": "23883838dhhskk89968654s",
"album_name":"Rammstein",
"date_taken":"35356373773"
}],
"date_inserted":"hard metal",
"updated_on": true
}
}';
echo '<pre>';
echo $jl;
echo '</pre>';
?>
php抱怨我不能从json文本内部使用for循环,这是一个问题,因为我有几个数组要添加。如何将数组数据添加到json文本字符串中?使用匿名函数形成json文本,并根据数组长度添加或省略最后一个元素逗号,确保生成有效的json
不要手动构建JSON。解码,添加新元素,重新编码。这能回答你的问题吗?除此之外,您试图在赋值中使用echo
(非法),并试图使用+
连接(混合JS和PHP语法)。?不,没有。具体地说,我想为一个非常特定的用例添加数组数据。