0

I need to "reformat" some data coming from an external API so it works with the nested list module of Sencha touch. I cannot change the data output of that external API. Here's an example of the data I get from the API:

$quest = array(
    'gastronomy' => [
        'restaurants' => [
            'italians' => [
                [
                    'title' => 'Al Castello',
                    'leaf' => true
                ],

                [
                    'title' => 'Italia',
                    'leaf' => true
                ]
            ],

            'asians' => [
                [
                    'title' => 'Gautam',
                    'leaf' => true
                ],

                [
                    'title' => 'Wok',
                    'leaf' => true
                ]
            ]
        ]
    ]
);

In order to make it work with sencha touch the data must look like this after "reformatting" it with a PHP Service:

$result = array(
    'items' => [
        [
            'title' => 'gastronomy',
            'items' => [
                [
                    'title' => 'restaurants',
                    'items' => [
                        [
                            'title' => 'italians',
                            'items' => [
                                [
                                    'title' => 'Al Castello',
                                    'leaf' => true
                                ],

                                [
                                    'title' => 'Italia',
                                    'leaf' => true
                                ]
                            ]
                        ],

                        [
                            'title' => 'asians',
                            'items' => [
                                [
                                    'title' => 'Gautam',
                                    'leaf' => true
                                ],

                                [
                                    'title' => 'Wok',
                                    'leaf' => true
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
);

I have tried every way I could think of but with no success. What really bugs me is that all keys must be renamed to items. (It's hard for me to access the deeper nested items because of that when I'm using a recursive function)

Martin
  • 691
  • 1
  • 9
  • 18

2 Answers2

0

Haven't tested it, but it seems like a fairly simple recursive function should handle it.

For example:

function parseApi($arr) {
    $result = array();
    foreach ($arr as $key => $value) {
        if (isset($value['leaf'])) {
            $result[] = $value;
        } else {
            $result[] = array(
                'title' => $key,
                'items' => parseApi($value)
            );
        }
    }

    return $result;
}

$result = array( 'items' => $parseApi($quest);
0

You need a recursive function, and it needs to be able to tell the difference between associative and numerically-indexed arrays.

// from: http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
function isAssoc($arr) { return array_keys($arr) !== range(0, count($arr) - 1); }

function itemize($foo) {
    $output = [];
    if( ! isAssoc($foo) ) {
        foreach( $foo as $value ) {
            if( is_array($value) ) {
                $output[] = itemize($value);
            } else {
                $output[] = $value;
            }
        }
    } else {
        foreach( $foo as $key => $value ) {
            if( is_array($value) ) {
                $output[] = [
                    'title' => $key,
                    'items' => itemize($value)
                ];
            } else {
                $output[$key] = $value;
            }
        }
    }
    return $output;
}

echo json_encode(itemize($quest), JSON_PRETTY_PRINT);

Output:

[
    {
        "title": "gastronomy",
        "items": [
            {
                "title": "restaurants",
                "items": [
                    {
                        "title": "italians",
                        "items": [
                            {
                                "title": "Al Castello",
                                "leaf": true
                            },
                            {
                                "title": "Italia",
                                "leaf": true
                            }
                        ]
                    },
                    {
                        "title": "asians",
                        "items": [
                            {
                                "title": "Gautam",
                                "leaf": true
                            },
                            {
                                "title": "Wok",
                                "leaf": true
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
Sammitch
  • 30,782
  • 7
  • 50
  • 77