PHP code example of squidit / data-array-mapper

1. Go to this page and download the library: Download squidit/data-array-mapper 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/ */

    

squidit / data-array-mapper example snippets




$resultSet = [
    [
        'userId'    => 3,
        'userName'  => 'MoròSwitie',
        'age'       => 37,
        'toyId'     => 7,
        'toyType'   => 'car',
        'toyName'   => 'Rover',
        'placeId'   => 33,
        'placeName' => 'Australia',
    ],
    [
        'userId'    => 3,
        'userName'  => 'MoròSwitie',
        'age'       => 37,
        'toyId'     => 7,
        'toyType'   => 'car',
        'toyName'   => 'Rover',
        'placeId'   => 34,
        'placeName' => 'New Zealand',
    ]
];

$finalResult = [
    3 => [
        'userId'    => 3,
        'userName'  =>'MoròSwitie',
        'age'       => 37,
        'toys'      => [
            7 => [
                'toyId'     => 7,
                'toyType'   => 'car',
                'toyName'   => 'Rover',
                'placesToyVisited' => [
                    33 => [
                        'placeId'   => 33,
                        'placeName' => 'Australia',
                    ],
                    34 => [
                        'placeId'   => 34,
                        'placeName' => 'New Zealand',
                    ],
                ]
            ]
        ]
    ]
];



$resultStructure = [
    'userId',
    'userName',
    'age',
    'toys' => [
        'toyId',
        'toyType',
        'toyName',
        'placesToyVisited' => [
            'placeId',
            'placeName',
        ]
    ]
];

$pivotPoints = [
    '[root]' => 'userId',
    'toys' => 'toyId',
    'toys.placesToyVisited' => 'placeId',
];



// Make sure composer autoload has loaded
use SquidIT\Data\ResultSetToArray\Mapper;

$parsedStructure = Mapper::parseStructure($resultStructure, $pivotPoints);
$mappedData      = Mapper::mapData($resultSet, $parsedStructure);



// Make sure composer autoload has loaded
use SquidIT\Data\ResultSetToArray\Mapper;

$parsedStructure = Mapper::parseStructure($resultStructure, $pivotPoints);
$mappedData      = Mapper::mapData($resultSet, $parsedStructure, false);



$resultStructure = [
    'userId' => 'accountId', // dataset column = 'userId', output would be 'accountId'
    'userName' => 'accountName',
    'age' => 'accountCreationDate',
    'toys' => [
        'toyId',
        'toyType' => 'specimen',
        'toyName',
        'placesToyVisited' => [
            'placeId',
            'placeName',
        ]
    ]
];

$pivotPoints = [
    '[root]' => 'userId',
    'toys' => 'toyId',
    'toys.placesToyVisited' => 'placeId',
];