PHP code example of gamringer / php-json-pointer
1. Go to this page and download the library: Download gamringer/php-json-pointer library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gamringer / php-json-pointer example snippets
$target = [
"foo" => ["bar", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
var_dump($pointer->has("/foo"));
/* Results:
bool(true)
*/
$target = [
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
var_dump($pointer->has("/foo"));
/* Results:
bool(false)
*/
$target = [
"foo" => ["bar", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
var_dump($pointer->get("/foo"));
/* Results:
array(2) {
[0] =>
string(3) "bar"
[1] =>
string(3) "baz"
}
*/
$target = [
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
var_dump($pointer->get("/foo"));
/* Results:
Throws gamringer\JSONPointer\Exception
*/
$target = [
"foo" => ["bar", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
$value = "waldo";
var_dump($pointer->insert("/foo/1", $value));
var_dump($pointer->get("/foo"));
/* Results:
class gamringer\JSONPointer\VoidValue#6 (2) {
protected $owner =>
array(3) {
...
}
protected $target =>
string(1) "1"
}
array(3) {
[0] =>
string(3) "bar"
[1] =>
string(5) "waldo"
[2] =>
string(3) "baz"
}
*/
$target = [
"foo" => ["bar", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
$value = "waldo";
var_dump($pointer->insert("/foo", $value));
var_dump($pointer->get("/foo"));
/* Results:
array(2) {
[0] =>
string(3) "bar"
[1] =>
string(3) "baz"
}
string(5) "waldo"
*/
$target = [
"foo" => ["bar", "waldo", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
$value = "corge";
var_dump($pointer->set("/foo", $value));
/* Results:
array(3) {
[0] =>
string(3) "bar"
[1] =>
string(5) "waldo"
[2] =>
string(3) "baz"
}
*/
$target = [
"foo" => ["bar", "waldo", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
$value = "garply";
var_dump($pointer->set("/grault", $value));
/* Results:
class gamringer\JSONPointer\VoidValue#6 (2) {
protected $owner =>
array(3) {
...
}
protected $target =>
string(6) "grault"
}
*/
$target = [
"foo" => ["bar", "waldo", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
var_dump($pointer->remove("/qux"));
/* Results:
string(4) "quux"
*/
$target = [
"foo" => ["bar", "waldo", "baz"],
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
var_dump($pointer->remove("/qux"));
/* Results:
Throws gamringer\JSONPointer\Exception
*/
$target = [
"foo" => ["bar", "waldo", "baz"],
"qux" => "quux"
];
$pointer = new \gamringer\JSONPointer\Pointer($target);
$pointer->remove("/qux");
var_dump($target);
/* Results:
array(1) {
'foo' =>
array(3) {
[0] =>
string(3) "bar"
[1] =>
string(5) "waldo"
[2] =>
string(3) "baz"
}
}
*/
$target = [
"foo" => ["bar", "waldo", "baz"],
"qux" => "quux"
];
$value = "bar";
$pointer = new \gamringer\JSONPointer\Pointer($target);
$pointer->set("/foo", $value);
var_dump($target);
/* Results:
array(2) {
'foo' =>
string(3) "bar"
'qux' =>
string(4) "quux"
}
*/