PHP code example of jjware / phpcollectionjson

1. Go to this page and download the library: Download jjware/phpcollectionjson 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/ */

    

jjware / phpcollectionjson example snippets


use PhpCollectionJson\Document;
use PhpCollectionJson\Collection;
use PhpCollectionJson\Item;
use PhpCollectionJson\Data;

$document = new Document();
$collection = new Collection('http://www.somesite.com/users');
$document->setCollection($collection);

$item = new Item('http://www.somesite.com/users/123');
$item->getData()
    ->add(new Data('firstName', 'John'))
    ->add(new Data('lastName', 'Smith'))
    ->add(new Data('username', 'jsmith'));

$collection->getItems()->add($item);

echo json_encode($document);

use PhpCollectionJson\Document;

$json = file_get_contents('http://www.somesite.com/users'); // don't do this at home kids

$document = Document::fromJSON($json);

$firstName = $document->getCollection()->getItems()->elementAt(0)->getData()->elementAt(0)->getValue();

// $firstName === 'John'