I have this data coming from a database which pulls all subcategories and relevant info:
$data = array(
array(
'destination_name' => 'Ohio Resort',
'destination_slug' => 'ohio',
'subcategory_name' => 'Bird Hunting',
'subcategory_slug' => 'bird-hunting',
'category_name' => 'Hunting',
'category_slug' => 'hunting',
),
array(
'destination_name' => 'Ohio Resort',
'destination_slug' => 'ohio',
'subcategory_name' => 'Bear Hunting',
'subcategory_slug' => 'bear-hunting',
'category_name' => 'Hunting',
'category_slug' => 'hunting',
),
array(
'destination_name' => 'Maryland Resort',
'destination_slug' => 'maryland',
'subcategory_name' => 'Bird Hunting',
'subcategory_slug' => 'bird-hunting',
'category_name' => 'Hunting',
'category_slug' => 'hunting',
),
array(
'destination_name' => 'Cali Resort',
'destination_slug' => 'california',
'subcategory_name' => 'Sledding',
'subcategory_slug' => 'sledding',
'category_name' => 'Winter Sports',
'category_slug' => 'winter-sports',
),
);
I am generating xml with this, but I need to reorder it based on my specific needs. Let's say I specify functionCall( $data, array('destination', 'category', 'subcategory') );
I want it to return it in this form:
Array(
[california] => array(
[_name] => 'California',
[winter-sports] => array(
'_name' => 'Winter Sports',
[0] => array(
subcategory_name => Sledding,
subcategory_slug => sledding
)
)
),
[maryland] => array(
[_name] => 'Maryland',
[hunting] => array(
'_name' => 'Hunting',
[0] => array(
subcategory_name => Bird Hunting,
subcategory_slug => bird hunting
)
)
),
[ohio] => array(
[_name] => 'Ohio',
[hunting] => array(
'_name' => 'Hunting',
[0] => array(
subcategory_name => Bird Hunting,
subcategory_slug => bird-hunting
),
[1] => array(
subcategory_name => Bear Hunting,
subcategory_slug => bear-hunting
)
)
),
);
If I specify functionCall( $data, array('category', 'subcategory') );
I want it to return:
Array(
[winter-sports] => array(
'_name' => 'Winter Sports',
[0] => array(
subcategory_name => Sledding,
subcategory_slug => sledding
)
),
[hunting] => array(
'_name' => 'Hunting',
[0] => array(
subcategory_name => Bird Hunting,
subcategory_slug => bird hunting
),
[1] => array(
subcategory_name => Bear Hunting,
subcategory_slug => bear-hunting
)
)
);
Could someone offer some pointers on creating the function that takes the data as the first argument and the level-nesting as the second argument and returns it in the proper format?
So far I'm doing the reorganization manually:
$data = array();
foreach( $categories as $res ) {
$destinationSlug = $res['destination_slug'];
$destinationName = $res['destination_name'];
$categorySlug = $res['category_slug'];
$categoryName = $res['category_name'];
if ( !in_array( $destinationSlug, array_keys( $data ) ) ) {
$data[ $destinationSlug ] = array();
$data[ $destinationSlug ]['_name'] = $destinationName;
}
if ( !in_array( $categorySlug, array_keys( $data[ $destinationSlug ] ) ) ) {
$data[ $destinationSlug ][ $categorySlug ] = array();
$data[ $destinationSlug ][ $categorySlug ]['_name'] = $categoryName;
}
$data[ $destinationSlug ][ $categorySlug ][] = array(
'subcategory_slug' => $res['subcategory_slug'],
'subcategory_name' => $res['subcategory_name'],
);
}
return $data;
Random notes:
- The data will always be in this format. There will always be a
_slug
and_name
pair. - I will need to have the ability to alphabetically order the results