Php - 递归递归函数,用于获取父级的一个级别上的所有节点

时间:2015-04-04 15:06:56

标签: php arrays recursion

我对递归函数不是很好。我阅读了所有的论坛,并尝试围绕着需要做的事情,我只是无法得到它,而且我已经没时间了。所以我需要寻求帮助。

我有下面的数组。我想把所有公司都放在一个层面上。所以返回的结果是,第一个元素是元素0,然后all_child_companies下的第一个节点将是元素1,依次递归。

<?php

// array(
//   'id' => '1',
//   'name' => 'Company 1',
//   'address_id' => '15',
//   'emp_size' => '0',
//   'parent_id' => '0',
//   'is_beta' => '0',
//   'owner_id' => '1',
//   'created_at' => '2015-04-02 03:25:32',
//   'updated_at' => '2015-04-02 03:25:32',
//   'deleted_at' => NULL,
//   'industry' => '',
//   'avatar_file_name' => NULL,
//   'avatar_file_size' => NULL,
//   'avatar_content_type' => NULL,
//   'avatar_updated_at' => NULL,
//   'address' => array(
//     'id' => '15',
//     'street1' => '111 something street',
//     'street2' => '',
//     'street3' => '',
//     'city' => 'houston',
//     'state' => '0',
//     'zip' => '',
//     'created_at' => '2015-04-02 03:25:32',
//     'updated_at' => '2015-04-02 03:25:32',
//     'deleted_at' => NULL,
//     'address_id' => '0',
//     'address_type' => '',
//     'country' => ''
//   ),
//   'all_child_companies' => array(
//     0 => array(
//       'id' => '2',
//       'name' => 'Company 1',
//       'address_id' => '16',
//       'emp_size' => '55',
//       'parent_id' => '1',
//       'is_beta' => '1',
//       'owner_id' => '1',
//       'created_at' => '2015-04-02 03:25:32',
//       'updated_at' => '2015-04-02 03:25:32',
//       'deleted_at' => NULL,
//       'industry' => '',
//       'avatar_file_name' => NULL,
//       'avatar_file_size' => NULL,
//       'avatar_content_type' => NULL,
//       'avatar_updated_at' => NULL,
//       'address' => array(
//         'id' => '16',
//         'street1' => '222 something street',
//         'street2' => '',
//         'street3' => '',
//         'city' => 'tucson',
//         'state' => '0',
//         'zip' => '',
//         'created_at' => '2015-04-02 03:25:32',
//         'updated_at' => '2015-04-02 03:25:32',
//         'deleted_at' => NULL,
//         'address_id' => '0',
//         'address_type' => '',
//         'country' => ''
//       ),
//       'all_child_companies' => array(
//         0 => array(
//           'id' => '3',
//           'name' => 'Company 2',
//           'address_id' => '17',
//           'emp_size' => '15',
//           'parent_id' => '2',
//           'is_beta' => '0',
//           'owner_id' => '1',
//           'created_at' => '2015-04-02 03:25:32',
//           'updated_at' => '2015-04-02 03:25:32',
//           'deleted_at' => NULL,
//           'industry' => '',
//           'avatar_file_name' => NULL,
//           'avatar_file_size' => NULL,
//           'avatar_content_type' => NULL,
//           'avatar_updated_at' => NULL,
//           'address' => array(
//             'id' => '17',
//             'street1' => '333 something street',
//             'street2' => '',
//             'street3' => '',
//             'city' => 'tempe',
//             'state' => '0',
//             'zip' => '',
//             'created_at' => '2015-04-02 03:25:32',
//             'updated_at' => '2015-04-02 03:25:32',
//             'deleted_at' => NULL,
//             'address_id' => '0',
//             'address_type' => '',
//             'country' => ''
//           ),
//           'all_child_companies' => array(
//             0 => array(
//               'id' => '4',
//               'name' => 'Company 3',
//               'address_id' => '19',
//               'emp_size' => '100',
//               'parent_id' => '3',
//               'is_beta' => '0',
//               'owner_id' => '0',
//               'created_at' => '2015-04-02 05:24:34',
//               'updated_at' => '2015-04-02 05:24:34',
//               'deleted_at' => NULL,
//               'industry' => '',
//               'avatar_file_name' => NULL,
//               'avatar_file_size' => NULL,
//               'avatar_content_type' => NULL,
//               'avatar_updated_at' => NULL,
//               'address' => array(
//                 'id' => '19',
//                 'street1' => '333 something street',
//                 'street2' => '',
//                 'street3' => '',
//                 'city' => 'phoenix',
//                 'state' => '0',
//                 'zip' => '85042',
//                 'created_at' => '2015-04-02 05:24:34',
//                 'updated_at' => '2015-04-02 05:24:34',
//                 'deleted_at' => NULL,
//                 'address_id' => '0',
//                 'address_type' => '',
//                 'country' => 'United States Of America'
//               ),
//               'all_child_companies' => array(
// 
//               )
//             )
//           )
//         )
//       )
//     )
//   )
// )

提前感谢您的帮助。

我知道这不起作用,但是我一直在努力......

/**
 * @param $needle_key
 * @param $array
 * @return bool
 */
public static function array_search_key($needle_key = 'all_child_companies', $array)
{
    if(isset($array['all_child_companies']) && !empty($array['all_child_companies'])){
        return self::array_search_key($needle_key, $array['all_child_companies']);
    }

    foreach ($array AS $key => $value) {
        if ($key == $needle_key) return $value;
        if (is_array($value)) {
            if (($result = self::array_search_key($needle_key, $value)) !== false)
                return $result;
        }
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

这是一个简单,有效的解决方案:

function resolveRecursion(array $result, array $array) {
    $result[] = $array; //add the current company to the result

    foreach($array['all_child_companies'] as $childCompany) {
        $result = resolveRecursion($result, $childCompany); //add all the children companies to the result
    }

    return $result;
}

$result = resolveRecursion([], $yourCompaniesArray);

答案 1 :(得分:1)

您可以像在此功能中一样尝试:

function flatten_array($needle_key, $array) {
  $result = array($array);
  foreach ($array[$needle_key] as $key => $value) {
    $result = array_merge($result, flatten_array($needle_key, $value));
    unset($result[0][$needle_key][$key]);
  }
  return $result;
}