Combine two php curl reponses

I’ve got two curl scripts that return the following responses in php which i’m then decoding and using as a jquery jobject to populate a form.

can anyone explain how i’d combine the two please as i’ve tried a couple of things and can’t get it to work.

response1= {
  "id": "idher",
  "registration": "registrationhere"
     }

and response2 = {
 "vech": {
    "set1": {
      "make": "bosh",
      "model": "123",
      "regDate": "2008-09-29"

}}}

Show us the code you’ve tried, in case anyone can spot why it doesn’t work.

and what does ‘combine’ mean? $combined = $response1 . $response2 or have a look at àrray_merge()

I’ve tried $combined = $response1 . $response2 but when i try to parse it in json i get a syntax error.

When i tried array merge i got the following error array_merge(): Argument #1 is not an array

the script im using to decode it is:

<script>
jQuery( document ).ready(function( $ ) {      
      $("#ajax").on("click", function(){
        var text = $("#myTextField").val();
        $.ajax({
          type: "POST",
          url: "lookup.php",
          cache: false,
          data: { "value" : text },
          success: function(response){
    
var obj = jQuery.parseJSON(response)
  
      console.log(obj);

 
          },
          error: function(response){
            alert ("Ajax Error");
            console.log(response);
          }
        });
      });
});  
    </script>

there’s no PHP code (so nothing to answer on your missing argument) and there is just one response and there is no code trying to combine anything. and yes, concatenating jsons will not produce a valid json, but you are still not specific at what your ‘combine’ should do with two json strings.

Apologies, the php code is purely performing a lookup and then echoing it on the lookup.php page. Which the jquer

$response1 = curl_exec($curl1);
$response2 = curl_exec($curl2);

$response3 =  $response1 . $response2

echo $response3;

What i want to do is combine these two responses into one object i can then query. It works if i just try to read either of the 2 responses but as soon as i combine them i get the errors.

as the results may be 2 JSON strings i assume you first want to convert them into asociative arrays (= with keys) via json_decode() (provide second parameter) and then merge existing keys of both? therefore you can use array_merge() on both arrays and type cast it back to an object.

// JSONs
$json1 = '{
            "id": "idher",
            "registration": "registrationhere"
          }';

$json2 = '{
            "vech": {
                "set1": {
                        "make": "bosh",
                        "model": "123",
                        "regDate": "2008-09-29"
                        }
                    }
          }';
// Convert JSONs to arrays
$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);

// Works as long as the arrays does not have equal keys in them
$combinedArray = array_merge($array1, $array2);

// Alternative way. Also works as long as the arrays does not have equal keys in them
$combinedArray = $array1+$array2;

// Convert back to valid json
$resultJSON = json_encode($combinedArray);

echo $resultJSON;

If the arrays do have equal string keys then you could write your own combine function to merge the arrays so you do not lose data in the process. This because array_merge will overwrite and replace the values if there is equal string keys in the arrays.

Cheers,
TeNDoLLA

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.