Jsonfy class is used to handle a two
level dimensionsional array or json string. It may convert a json string to
an array, convert an array to json, modify an existing json or array data
, fetch or remove data from a json string. The methods available are as follows:
$data : refers to a json string or array data variable
$array : refers to sample array used - ['name' => 'foo', 'class' => 'bar'];
$json : refers to sample json used - {"name": "foo", "class": "bar"};
data : specifies that an argument accepts a json string or array data
$jsonfy = new Jsonfy;
newData method is used to set a new json string or array data
$jsonfy->newData(data); // check keywords for data
add() method is used to add a key and its value into
a json or array data. It involves a series of formats. We shall be looking at
few examples:
$jsonfy->add(null);
$jsonfy->add(name);
$jsonfy->add(null, null);
$jsonfy->add(null, value);
$jsonfy->add(name, value);
$jsonfy->add(null, null, null);
$jsonfy->add(name, key, value);
$jsonfy->add(null, key, value);
$jsonfy->add(name, null, value);
where:
name : name of a given index of an associative array
value : value of a given index of an associative or 2-level multidimentional array
key : subkey of a 2-level multidimentional array
null : numbered index (e.g 0, 1, 2 ...) or empty string
Note: This may look comprehensive but a series of examples will provide guidance
Note:: all case lines below are assumed to be the first line
//case 1 - one argument
sample: $jsonfy->add(''); //['0'=>'']
sample: $jsonfy->add('foo'); //['foo'=>'']
//case 2 - two arguments
sample: $jsonfy->add('', ''); //['0'=>'']
sample: $jsonfy->add('', 'bar'); //['0'=>'bar']
sample: $jsonfy->add('foo', 'bar'); //['foo'=>'bar']
//case 3 - three arguments
sample: $jsonfy->add('', '', ''); //['0'=>['0'=>'']]
sample: $jsonfy->add('foo', 'bar', 'me'); //['foo'=>['bar'=>'me']]
sample: $jsonfy->add('', 'bar', 'me'); //['0'=>['bar'=>'me']]
sample: $jsonfy->add('', 'me', ''); //['0'=>['me'=>'']]
sample: $jsonfy->add('', '', 'me'); //['0'=>['0'=>'me']]
numbers except in few
occassions. Kindly note the followingnumbers are used starting from 0.
jsonfy class increases index numbers. This
means that if add('') is used twice, the first will have the index of zero
0 while the the second will assume an index of one 1
datakey() method returns the first key of a specified value for
an associative array.
$jsonfy->datakey(key);
where:
key : name of a given index of an associative array
Note:: very line below is assumed to be the first list
$jsonfy->newData(['user'=> 'foo', 'class' => 'bar']); or {"name": "foo", "class": "bar"}
var_dump( $jsonfy->datakey('foo') ); // user
var_dump( $jsonfy->datakey('bar') ); // class
update() method works similarly as the add() method, taking the
same count of parameters needed for a corresponding update
//example 1
$jsonfy->add('name', 'foo'); //['name'=>'foo']
$jsonfy->update('name', 'voo'); //['name'=>'voo']
//example 2
$jsonfy->add('', 'foo'); //['0'=>'foo']
$jsonfy->update($jsonfy->datakey('foo'), 'bar'); //['0'=>'bar']
//example 3
$jsonfy->add('user','foo','bar'); //['user'=>['foo'=>'bar']]
$jsonfy->update('user', 'foo', 'me'); //['user'=>['foo'=>'me']]
delete() method deletes a value from existing jsonfy data
It removes either the main key of a 2-level multidimentional array or the subkey
of a multidimentional array.
//test data
$jsonfy->add('user','foo','bar'); //['user'=>['foo'=>'bar']]
//example 1
$jsonfy->delete('user', 'foo'); //['user'=>'']
//example 2
$jsonfy->delete('user'); //[]
read method reads data from a supplied jsonfy data.
$jsonfy->read(key);
where:
key: main array index key.
//example 1
$jsonfy->add('user','foo','bar'); //['user'=>['foo'=>'bar']]
var_dump( $jsonfy->read('user') ); //['foo'=>'bar']
//example 2
$jsonfy->add('user','foo'); //['user'=>'foo']
var_dump( $jsonfy->read('user') ); //foo
Note: when an index key does not exist, it returns a boolean of false
data method returns the entire stored data. This can be in
form of array or json string.
$jsonfy->data(type);
where:
type - options [null|json|source|count]
null - returns array of data stored
json - returns json string of data stored
count - returns the total count of data
source - returns the source data supplied from newData method
$jsonfy->newData(['foo' => 'bar']);
var_dump($jsonfy->data('source')); // ['foo' => 'bar']
var_dump($jsonfy->data()); // ['foo' => 'bar']
var_dump($jsonfy->data('json')); // {"foo": "bar"}
var_dump($jsonfy->data('count')); // 1