helper-json, Packagist
To install package, you need run command:
composer require akbsit/helper-json
- Check string:
$sData = '{"key1":"value1","key2":"value2"}';
$bFlag = JsonHelper::isJson($sData);
- JSON string to array:
$sData = '{"key1":"value1","key2":"value2"}';
$arData = JsonHelper::make()->data($sData)->decode();
array(2) {
["key1"]=> string(6) "value1"
["key2"]=> string(6) "value2"
}
- JSON file to array
$sPath = 'data.json'; // in file string {"key1":"value1","key2":"value2"}
$arData = JsonHelper::make()->data($sPath)->decode();
array(2) {
["key1"]=> string(6) "value1"
["key2"]=> string(6) "value2"
}
- Array to JSON string:
$arData = [
'key1' => 'value1',
'key2' => 'value2',
];
$sData = JsonHelper::make()->data($arData)->encode();
string(33) "{"key1":"value1","key2":"value2"}"