PHP code example of jsnlib / ao

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

    

jsnlib / ao example snippets


$data = array('say' => 'hello');

echo $data->say; // 輸出 hello

$mix = array('my'=>"Jsnao");
$result = new Jsnao($mix);
$result->my; // 輸出: Jsnao

$mix = '{"my":"Jsnao"}';
$result = new Jsnao($mix);
$result->my; // 輸出: Jsnao

$mix = new stdClass;
$mix->my = "Jsnao";
$result = new Jsnao($mix);
$result['my']; // 輸出: Jsnao

$mix = 'Hello World';
$result = new Jsnao($mix);
$result->data; // 輸出: Hello World

$mix = 123456;
$result = new Jsnao($mix);
$result->data; // 輸出: 123456

$mix = null;
$result = new Jsnao($mix);



$cart = array
(
    'A001'   =>  array
    (
        'name'  =>  'apple',
    ),
    1000 => array
    (
        'name'  => 'water'
    )
);
$cart = new jsnao($cart);

// 取值
$cart->A001->name; //output: apple
// 或
$cart->get(1000)->name;

// 賦值
$cart->A002 = array('name' => 'banana');
// 或
$cart->A002 = array();
$cart->A002->name = 'banana';
// 或
$cart->put(2000, array('name' => 'lemon'));

// 修改
$cart->A001->name = 'cherry';
// 或
$cart->A001->put('name', 'cherry');

// 刪除
$cart->A003 = array('name' => 'bag');
unset($cart->A003);

echo $cart;

<?
/ 購物車
$cart = array
(
    '001'   =>  array
    (
        'name'  =>  'apple',
    )
);

$cart = new Jsnao($cart);

// 取值
$cart['001']['name']; // output: apple

// 賦值
$cart['002']['name'] = "banana"; 

// 修改
$cart['001']['name'] = "cherry"; 

// 刪除
$cart['003']['name'] = "bag";
unset($cart['003']);

echo $cart;

<?
/ 購物車
$cart = array
(
    '001'   =>  array
    (
        'name'  =>  'apple',
    )
);

$cart = new Jsnao($cart);


// 取值
$cart->offsetGet('001')->name; //output: apple

// 賦值
$cart->offsetSet('002', array('name' => 'banana')); //output: apple

// 修改
$cart->offsetGet('001')->offsetSet('name', 'cherry');

// 刪除
$cart->offsetSet('003', array('name' => 'bag')); 
$cart->offsetUnset('003'); 

echo $cart;

$result = new jsnao($array);
$result->get(0);
// 或
$result[0];

$result = new jsnao($array);
$result->put(1, "banana");
// 或
$result->put[1] = "banana";
// 或
$result->first = "banana";

$result = new jsnao($array);
$result->toArray();

$result = new jsnao($array);
$result->log();
// 或
$result->log("自訂顯示名稱");
// 或
$data = $result->log();

$data = array
(
    'profile' => array
    (
        'phone' => "0939-333-333",
        'tel' => "07-333-3333"
    ), 
);
$data = new jsnao($data);
echo $data->log()->profile->phone; 
// 輸出 0939-333-333
// console.log() 為 Object {profile: Object {phone: "0939-333-333", tel: "07-333-3333"}}

// 或是放在另外一個定位點

echo $data->profile->log()->phone; 
// 輸出 0939-333-333
// console.log() 為 Object {phone: "0939-333-333", tel: "07-333-3333"}

$result = new jsnao($array);
echo $result;