如何实现返回嵌套数组的自定义重组函数?

时间:2010-08-18 17:32:49

标签: php arrays multidimensional-array

我的数据来自数据库,该数据库提取所有子类别和相关信息:

$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',
    ),
);

我用这个生成xml,但是我需要根据我的具体需要对它进行重新排序。假设我指定functionCall( $data, array('destination', 'category', 'subcategory') );

我希望它以这种形式返回:

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
         )
      )
   ),

);

如果我指定functionCall( $data, array('category', 'subcategory') );我希望它返回:

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
             )
          )
);

有人可以提供一些关于创建函数的指针,该函数将数据作为第一个参数,将level-nesting作为第二个参数并以正确的格式返回吗?

到目前为止,我正在手动进行重组:

$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;

随机笔记:

  • 数据始终采用此格式。始终会有_slug_name对。
  • 我需要能够按字母顺序排列结果

1 个答案:

答案 0 :(得分:0)

你可以使用uasort,它允许用户定义的函数用于对数组进行排序。但我建议你有自己的功能。