Reputation: 325
Im not very good with recursive functions. I read all the forums and try to wrap my head around what needs to be done, i just can't get it and i'm running out of time. So i need to ask for help.
I have the array below. I am trying to have all the companies on one level. So the returned result would be, first element would be element 0 then the first node under all_child_companies would be element 1 and so on recursively.
<?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(
//
// )
// )
// )
// )
// )
// )
// )
// )
Thank you in advance for any help.
I know this does not work but its what i keep comming up with..
/**
* @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;
}
Upvotes: 1
Views: 1079
Reputation: 984
You could try it like in this function:
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;
}
Upvotes: 1
Reputation: 1367
Here is a simple, working solution:
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);
Upvotes: 1