PHP code example of pecee / dot-notation
1. Go to this page and download the library: Download pecee/dot-notation 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/ */
pecee / dot-notation example snippets
$d = new \Pecee\DotNotation(['one' => ['two' => 2]]);
$d->set('one.two', 3);
echo $d->getValues();
array ('one' => ['two' => 3]);
class CustomerMapper {
protected $customer;
public function __construct(array $values) {
// Map fields (from -> to)
$this->customer = $this->map($values, [
'customerId' => 'id',
'relationshipStatus' => 'relationship_status',
'customerGender' => 'gender',
'meta.name' => 'name',
]);
}
protected function map(array $input, array $mapping) {
$output = array();
foreach($mapping as $before => $after) {
$map = new DotNotation($input);
$value = $map->get($before);
$map = new DotNotation($output);
$map->set($after, $value);
$output = $map->getValues();
}
return $output;
}
protected function getCustomer() {
return $this->customer;
}
}
// Example:
$mapper = new CustomerMapper([
'customerId' => 123456,
'relationshipStatus' => 'single',
'customerGender' => 'male',
'meta' => ['name' => 'Peter']
]);
$customer = $mapper->getCustomer();
array(
'id' => 123456,
'relationship_status' => 'single',
'gender' => 'male',
'name' => 'Peter',
);