1
$data = array(
    'uid' => 'key',
    'zip' => 'STRING_VARIABLE',

    "school" => array( 
        array("sid" => "STRING_VARIABLE", "qty" => NUMERIC_VARIABLE),
            array("strSrchSchool" => array(
            "name" => "STRING_VARIABLE", 
            "address" => "STRING_VARIABLE", 
        ), 
        "students" => NUMERIC_VARIABLE) 
    ),
    "sort" => "default"
);

Above array is a part of API call. I want dynamic values at places where I have mentioned 'STRING_VARIABLE' & 'NUMERIC_VARIABLE'. So I want to write PHP variables there and tried few different things of concatenation but none is working.

When I call API With different tries for above PHP variables, it gives below error.

array(2) { ["status"]=> string(5) "error" ["error"]=> string(62) "Error parsing JSON object, check format and object definition." }

So please let me know what is correct method to write PHP variable directly at above mentioned places when creating PHP array.

1
  • Can you clarify what "Part of the API call" this is, and describe a bit about the workflow surrounding it? It appears that your error could be resolved by adding a json_decode() (if you are receiving data) or a json_encode if you are passing it. You can try adding a $data = json_encode() data after your array definition and seeing if that helps, but I would need more context to provide a proper answer otherwise. Commented Jul 9, 2020 at 6:18

1 Answer 1

1

You don't have to do anything special.

Just write the name of the variable.

$zip = '252354';
$data = array(
    'uid' => 'key',
    'zip' => $zip,

    "school" => array( 
        array("sid" => "STRING_VARIABLE", "qty" => NUMERIC_VARIABLE),
            array("strSrchSchool" => array(
            "name" => "STRING_VARIABLE", 
            "address" => "STRING_VARIABLE", 
        ), 
        "students" => NUMERIC_VARIABLE) 
    ),
    "sort" => "default"
);

And so on you can write as many variables inside the array as you wish.

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.