PHP code example of rkwp / object-iterable-laravel

1. Go to this page and download the library: Download rkwp/object-iterable-laravel 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/ */

    

rkwp / object-iterable-laravel example snippets


$array = [
    'NAME' => 'John Doe',
    'email' => '[email protected]',
    'ADDRESS' => [
        'CITY' => 'New York',
        'ZIP' => '10001',
    ],
];

$objectIterable = objectIterable($array);

echo $objectIterable->name; // Output: John Doe
echo $objectIterable->EMAIL; // Output: [email protected]

echo $objectIterable->address->city; // Output: New York
echo $objectIterable->ADDRESS->ZIP;  // Output: 10001

foreach ($objectIterable as $item) {
    echo $item->name;
    echo $item->email;
    echo $item['email'];
    echo $item['EMAIL'];
}

foreach ($objectIterable->address as $key => $value) {
    echo "$key: $value";
}

$array = $objectIterable->toArray();

$array = $objectIterable->forceArray();

$collection = $objectIterable->toCollect();

$json = json_encode($objectIterable); // Menghasilkan JSON dari objek

echo $objectIterable['name']; // Output: John Doe

echo $objectIterable['ADDRESS']['CITY']; // Output: New York

echo count($objectIterable); // Output: jumlah elemen dalam array

$objectIterable = objectIterable($array, false);

$data = [
    'Name' => 'Alice',
    'age' => 30,
    'Address' => [
        'Street' => '123 Main St',
        'City' => 'Wonderland',
    ]
];

$object = objectIterable($data);

echo $object->name; // Alice
echo $object->address->city; // Wonderland

$arrayBack = $object->toArray();

protected $commands = [
    \RKWP\Commands\GenerateDataTypeAndDataStructure::class
];



namespace App\DataType\Devel\Users;

use App\DataStructure\Devel\Users\UserStruct;
use App\Helper\ObjectIterable;

class TUser extends ObjectIterable {
    use UserStruct;

    function __construct()
    {
        foreach (get_object_vars($this) as $name => $value) {
            unset($this->{$name});
        }
    }

    /**
     * @return self|ObjectIterable
     */
    public function set($items) {
        $this->assign($items, static::class);
        return $this;
    }
}



namespace App\DataStructure\Devel\Users;

use App\DataType\Devel\Users\TProfile;

trait UserStruct {
	public $id;
	public $name;
	public $email;
	public $created_at;
	public $status;
	public TProfile $profile; // ini saya tambahkan sendiri secara manual, tapi class TProfile tetap saya buat menggunakan perintah artisan yang tadi
}

    $users = [
        [
            'id' => 1,
            'name' => 'John Doe',
            'email' => '[email protected]',
            'created_at' => '2024-10-01T10:30:00Z',
            'status' => 'active',
            'profile' => [
                'age' => 30,
                'gender' => 'male',
                'location' => 'New York'
            ],
        ],
        [
            'id' => 2,
            'name' => 'Jane Smith',
            'email' => '[email protected]',
            'created_at' => '2024-09-15T08:45:00Z',
            'status' => 'inactive',
            'profile' => [
                'age' => 27,
                'gender' => 'female',
                'location' => 'Los Angeles'
            ],
        ],
        [
            'id' => 3,
            'name' => 'Alice Johnson',
            'email' => '[email protected]',
            'created_at' => '2024-11-01T13:20:00Z',
            'status' => 'active',
            'profile' => [
                'age' => 35,
                'gender' => 'female',
                'location' => 'Chicago'
            ],
        ],
        [
            'id' => 4,
            'name' => 'Bob Brown',
            'email' => '[email protected]',
            'created_at' => '2024-08-22T11:05:00Z',
            'status' => 'suspended',
            'profile' => [
                'age' => 40,
                'gender' => 'male',
                'location' => 'San Francisco'
            ],
        ],
        [
            'id' => 5,
            'name' => 'Charlie Green',
            'email' => '[email protected]',
            'created_at' => '2024-07-30T16:00:00Z',
            'status' => 'active',
            'profile' => [
                'age' => 22,
                'gender' => 'non-binary',
                'location' => 'Seattle'
            ],
        ]
    ];

    $users = (new TUser())->set($users);

    dd($users->toArray()); // Untuk debug data

    $ageUsers = [];
    foreach ($users as $user) {
        array_push($ageUsers, $user->profile->age);
    }

    return implode(',', $ageUsers);