1. Go to this page and download the library: Download joomla/utilities 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/ */
use Joomla\Utilities\ArrayHelper;
class Book {
public $name;
public $author;
public $genre;
public $rating;
}
class Author {
public $name;
public $born;
}
$input = array(
"name" => "The Hitchhiker's Guide to the Galaxy",
"author" => array(
"name" => "Douglas Adams",
"born" => 1952,
"died" => 2001),
"genre" => "comic science fiction",
"rating" => 10
);
$book = ArrayHelper::toObject($input, 'Book');
var_dump($book);
use Joomla\Utilities\ArrayHelper;
class Book {
public $name;
public $author;
public $genre;
public $rating;
}
class Author {
public $name;
public $born;
}
$book = new Book();
$book->name = "Harry Potter and the Philosopher's Stone";
$book->author = new Author();
$book->author->name = "J.K. Rowling";
$book->author->born = 1965;
$book->genre = "fantasy";
$book->rating = 10;
$array = ArrayHelper::fromObject($book);
var_dump($array);
use Joomla\Utilities\ArrayHelper;
$city = array(
"name" => "Oslo",
"country" => "Norway"
);
// Prints 'Oslo'
echo ArrayHelper::getValue($city, 'name');
// Prints 'unknown mayor' (no 'mayor' key is found in the array)
echo ArrayHelper::getValue($city, 'mayor', 'unknown mayor');