Input::json (method)

Last updated: Tue, Oct 31, 2023 12:53 pm
Return to Knowledgebase

Purpose

Location

users/classes/Input.php

Parameters

# Parameter Data Type Required Description
1 $json string or array Yes String or array to be processed
2 $associative = false bool No Whether to return associative arrays or objects
3 $encode = false bool No Whether to encode the cleaned data back into a JSON string

Returns

Data Type Description of Returned Data
other A PHP variable or a JSON string depending on the parameters passed.

Example

Input::json($jsonString, true)

Further Documentation:

1. If the input is a string, it first decodes the JSON string into a PHP array using json_decode.
2. Performs cleaning operations on the decoded JSON data using the recursive method.
3. Encodes the cleaned data back into a JSON string using json_encode if the $encode parameter is set to true.
4. Decodes the encoded JSON string back into either an associative array or an object, based on the value of the $associative parameter, if the $encode parameter is set to false.


// Decoding a JSON string
$jsonString = '{"name":"John","age":30,"city":"New York"}';
$result = Input::json($jsonString, true);
// $result = ["name" => "John", "age"=> "30", "city" => "New York"];

// Encoding a cleaned array into JSON
$arrayData = ["name" => "John", "age" => 30, "city" => "New York"];
$encodedResult = Input::json($arrayData, false, true); //
echo $encodedResult; // {"name":"John","age":"30","city":"New York"}