Search code examples
arraysjsonjsonencoder

php expects parameter 2 json_encode error


I have this array that returns a parameter error. Why?

        echo json_encode(
        array("replies" => array(
        array("message" => "Success ✅")
        )),
        array("variables" => array(
        array("data1" => "data2")
        ))          
        );

Solution

  • You need to wrap the array in a single variable because json_encode second argument is the flag and you have passed the array so it's a return error. For more info see this link https://www.php.net/manual/en/function.json-encode.php or try with below example

    echo json_encode(
        [["replies" => ["message" => "Success"]],
        ["variables" => ["data1" => "data2"]]]);
    

    #json #arraytojson #php