-1

I have this json literal string

{  
    "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      
    }  
}  

I am getting some data from the client that i want to add to the string then encode it and store it away.I am receiving several data sets as arrays and i wanted to add that as well by looping and encoding the resulting json literal string

  <?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 complains that i can't use the for loop from inside the json literal which is a problem because i have several arrays i want to add. How can i add the array data in the json literal string?.

8
  • Don't build JSON manually. Decode it, add new elements, re-encode. Commented May 1, 2021 at 20:31
  • Does this answer your question? Add new data into PHP JSON string Commented May 1, 2021 at 20:33
  • Aside from that, you're trying to use echo inside an assignment (illegal) and you're trying to use + to concatenate (mixing up JS and PHP syntax). Commented May 1, 2021 at 20:34
  • ? no it doesn't. Specifically, i wanted to add in array data for a very specific use case. Commented May 1, 2021 at 20:34
  • The + is an error i want to concatenta ewith dot, thanks for catching that. Commented May 1, 2021 at 20:34

1 Answer 1

0

Did form the json literal using an anonymous function and made sure it produces valid json by adding or omitting last element comma,based on the length of the array

 <?php
    $newarr = array(6,9,8,0,7);
    
      
    $of = function()
    {
    $h = rand(6,99999);
    $comma_resolution = '';
    $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":[';
            echo $jl;
            $possible_array_length = 4;
            for ($x = 0; $x <= $possible_array_length; $x++) {
            if($x === $possible_array_length){
            $comma_resolution = '';
            }
            else{
            $comma_resolution = ',';
            }
             echo '{
                        "album_id": "'.$x.'",
                        "album_name":"Rammstein",
                        "date_taken":"35356373773"
            }'.$comma_resolution.'';
            }
            
            $second = '],
            "restaurants":[{
                "album_id": "23883838dhhskk89968654s",
                "album_name":"Rammstein",
                "date_taken":"35356373773"
            }],
            "date_inserted":"hard metal", 
            "updated_on": true      
        }  
    }';
    echo $second;
    };
    
    echo '<pre>';
    $of();
    echo '</pre>';
    ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.