PHP code example of val / one-to-many-iterator

1. Go to this page and download the library: Download val/one-to-many-iterator 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/ */

    

val / one-to-many-iterator example snippets




[
    ['id' => 1, 'parent_column' => 'hello', 'child_column' => 'foo'],
    ['id' => 1, 'parent_column' => 'hello', 'child_column' => 'bar'],
    ['id' => 2, 'parent_column' => 'world', 'child_column' => 'baz'],
];



[
    [
        'id' => 1,
        'parent_column' => 'hello',
        'children' => [
            ['child_column' => 'foo'],
            ['child_column' => 'bar'],
        ],
    ],
    [
        'id' => 2,
        'parent_column' => 'world',
        'children' => [
            ['child_column' => 'baz'],
        ],
    ],
];



// With an iterator
$aggregated = new OneToManyIterator('id', 'children', $result);

// With a generator
$aggregated = new OneToManyGenerator('id', 'children', $result);

foreach ($aggregated as $i => $parent) {
    $parent['id'];
    $parent['parent_column'];

    foreach ($parent['children'] as $child) {
        $child['child_column'];
    }
}