1
\$\begingroup\$

Input:

$input = [
    'category' => [
        '1' => [
            'name' => 'c1',
            'attribute' => [
                '1' => [
                    'name' => 'a1',
                    'option' => [
                        '1' => [
                            'name' => 'o1'
                        ],
                        '2' => [
                            'name' => 'o2'
                        ]
                    ]
                ],
                '2' => [
                    'name' => 'a2',
                    'option' => [
                        '3' => [
                            'name' => 'o3'
                        ],
                        '4' => [
                            'name' => 'o4'
                        ]
                    ]
                ]
            ]
        ],
        '2' => [
            'name' => 'c2',
            'attribute' => [
                '3' => [
                    'name' => 'a3',
                    'option' => [
                        '5' => [
                            'name' => 'o5'
                        ],
                        '6' => [
                            'name' => 'o6'
                        ]
                    ]
                ],
                '4' => [
                    'name' => 'a4',
                    'option' => [
                        '7' => [
                            'name' => 'o7'
                        ],
                        '8' => [
                            'name' => 'o8'
                        ]
                    ]
                ]
            ]
        ]
    ]
];

Output:

$data = [
    ['category' => 1, 'categoryname' => 'c1', 'attribute' => 1, 'attributename' => 'a1', 'option' => 1, 'optionname' => 'o1'],
    ['category' => 1, 'categoryname' => 'c1', 'attribute' => 1, 'attributename' => 'a1', 'option' => 2, 'optionname' => 'o2'],
    ['category' => 1, 'categoryname' => 'c1', 'attribute' => 2, 'attributename' => 'a2', 'option' => 3, 'optionname' => 'o3'],
    ['category' => 1, 'categoryname' => 'c1', 'attribute' => 2, 'attributename' => 'a2', 'option' => 4, 'optionname' => 'o4'],
    ['category' => 2, 'categoryname' => 'c2', 'attribute' => 3, 'attributename' => 'a3', 'option' => 5, 'optionname' => 'o5'],
    ['category' => 2, 'categoryname' => 'c2', 'attribute' => 3, 'attributename' => 'a3', 'option' => 6, 'optionname' => 'o6'],
    ['category' => 2, 'categoryname' => 'c2', 'attribute' => 4, 'attributename' => 'a4', 'option' => 7, 'optionname' => 'o7'],
    ['category' => 2, 'categoryname' => 'c2', 'attribute' => 4, 'attributename' => 'a4', 'option' => 8, 'optionname' => 'o8'],
];

Method I used:

$final = [];

    foreach ($input as $k => $d) {
        foreach ($d as $ki => $i) {
            foreach ($i['attribute'] as $ka => $a) {
                foreach ($a['option'] as $ko => $o) {
                    array_push($final, ['category' => $ki, 'categoryname' => $i['name'], 'attribute' => $ka, 'attributename' => $a['name'], 'option' => $ko, 'optionname' => $o['name']]);
                };
            }
        }
    };

I want to convert the $input(INPUT) data into $data(OUTPUT). I mentioned my method which I used. Is it the best way we can use or is there any better option in terms of faster execution? What would be a better approach, or is it possible to just convert it into a one-liner?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Review! Where does this data come from? Does it come from a database, file, API, etc.? Is it a data source that you are able to control the format and/or the queries of? \$\endgroup\$ Feb 11, 2021 at 16:19
  • \$\begingroup\$ No, for now its just an array of data. Maybe in future i might want to do this with database. But for now its just an array. \$\endgroup\$
    – Deep Patel
    Feb 11, 2021 at 16:38

1 Answer 1

3
\$\begingroup\$

As long as this input array is static in its depth and structure, the best and fastest technique (due to lowest overhead) is to use old-school loops.

Beyond that, I would:

  1. Not declare variable that will not be used
  2. Use square brace pushing syntax
  3. Avoid excessively wide lines of code which demand the developer to horizontally scroll
  4. Use words as variable names to make your code more readable

Code: (Demo)

$final = [];
foreach ($categorizedOptions as $categories) {
    foreach ($categories as $categoryId => $category) {
        foreach ($category['attribute'] as $attributeId => $attribute) {
            foreach ($attribute['option'] as $optionId => $option) {
                $final[] = [
                    'category' => $categoryId,
                    'categoryname' => $category['name'],
                    'attribute' => $attributeId,
                    'attributename' => $attribute['name'],
                    'option' => $optionId,
                    'optionname' => $option['name']
                ];
            }
        }
    }
}

Don't worry with trying to convert this code into some sexy one-liner. Trying to do so with recursion or nested functions will make your code ugly and negatively impact performance.

Even trying to get fancy with "array destructuring" and a compact() call don't look worth the effort. (Demo)

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.