I have a below PHP array in which you can see category/sub-category structure based on "parent_id" & "parent" element.
Now the requirement is, on the basis of "menu_display_special = 1" condition, I want to delete that key as well as the keys which have this key's id as parent_id.
Note: If the "menu_display_special = 1" condition matches, it should remove the keys from category & sub-categories(if any) from the array.
Result should return only one array element. i.e. id = 2378
Array
(
[35] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2375
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2376
[parent] =>
)
[36] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2377
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
)
)
[parent_id] => 2376
[parent] => 1
)
[37] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2379
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2377
[parent] =>
)
[38] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2380
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2377
[parent] =>
)
[39] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2381
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2377
[parent] =>
)
[40] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2378
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
)
)
[parent_id] => 2376
[parent] =>
)
)
Code After var_export :
array (
35 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2375',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2376',
'parent' => false,
)),
36 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2377',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2376',
'parent' => true,
)),
37 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2379',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2377',
'parent' => false,
)),
38 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2380',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2377',
'parent' => false,
)),
39 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2381',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
)),
)),
'parent_id' => '2377',
'parent' => false,
)),
40 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2378',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
)),
)),
'parent_id' => '2376',
'parent' => false,
)),
)
Coding attempt : but it returns 2 array element i.e. 2381 & 2378. It should give me only 2378 as 2381 is child of 2377( which has menu_display_special value 1)
$listArray = array_filter($list,
function($v) use($list) {
return $v->params->get('menu_display_special') != '1' && ($v->parent == TRUE || $list[$v->parent_id]['params']['menu_display_special'] != '1');
}
);
I'm not sure if your design requirements allow me to merge the conditions in my recursive function, so I've kept them separate to try to be ultimately accurate.
There is no return value and $array is preceded by a & to make it modify the originally passed input array.
Code (Demo)
function recurse(&$array, $pid = null) {
foreach ($array as $id => $item) {
if (isset($item->params->data->menu_display_special) && $item->params->data->menu_display_special == 1) {
unset($array[$id]);
recurse($array, $item->id);
} elseif ($item->parent_id === $pid) {
unset($array[$id]);
}
}
}
$list = [
35 => (object)[
'id' => '2375',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2376',
'parent' => false
],
36 => (object)[
'id' => '2377',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2376',
'parent' => false
],
37 => (object)[
'id' => '2379',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2377',
'parent' => false
],
38 => (object)[
'id' => '2380',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2377',
'parent' => false
],
39 => (object)[
'id' => '2381',
'params' => (object)[
'data' => (object)[]
],
'parent_id' => '2377',
'parent' => false
],
40 => (object)[
'id' => '2378',
'params' => (object)[
'data' => (object)[]
],
'parent_id' => '2376',
'parent' => false
],
];
recurse($list);
var_export($list);
Output:
array (
40 =>
(object) array(
'id' => '2378',
'params' =>
(object) array(
'data' =>
(object) array(
),
),
'parent_id' => '2376',
'parent' => false,
),
)
Related
My WordPress site is unexpectedly redirecting (301) the visitors with X-Redirect-By: WordPress header. So I assume some code or plugin is calling wp_redirect to do the redirection. To trace the origin of the redirection I added the following in functions.php
add_filter( 'wp_redirect', function($url) { echo '<pre>'; var_export(debug_backtrace()); } );
Following is the output
array (
0 =>
array (
'file' => '/var/www/wp-includes/class-wp-hook.php',
'line' => 309,
'function' => '{closure}',
'args' =>
array (
0 => 'https://example.com/?utm_term&utm_adgroup=domain-name',
),
),
1 =>
array (
'file' => '/var/www/wp-includes/plugin.php',
'line' => 191,
'function' => 'apply_filters',
'class' => 'WP_Hook',
'object' =>
WP_Hook::__set_state(array(
'callbacks' =>
array (
1 =>
array (
'000000001c29730b0000000015f85ab7wp_redirect' =>
array (
'function' =>
array (
0 =>
WordPress_Module::__set_state(array(
'can_log' => true,
'redirect_url' => 'https://example.com/?utm_term&utm_adgroup=domain-name',
'redirect_code' => 301,
'redirects' =>
array (
),
'matched' => false,
)),
1 => 'wp_redirect',
),
'accepted_args' => 2,
),
),
10 =>
array (
'000000001c2970950000000015f85ab7wp_redirect' =>
array (
'function' =>
array (
0 =>
Change_WP_Admin_Login::__set_state(array(
'wp_login_php' => NULL,
)),
1 => 'wp_redirect',
),
'accepted_args' => 2,
),
'000000001c2972ef0000000015f85ab7' =>
array (
'function' =>
Closure::__set_state(array(
)),
'accepted_args' => 1,
),
),
),
'iterations' =>
array (
0 =>
array (
0 => 1,
1 => 10,
),
),
'current_priority' =>
array (
0 => 10,
),
'nesting_level' => 1,
'doing_action' => false,
)),
'type' => '->',
'args' =>
array (
0 => 'https://example.com/?utm_term&utm_adgroup=domain-name',
1 =>
array (
0 => 'https://example.com/?utm_term&utm_adgroup=domain-name',
1 => 301,
),
),
),
2 =>
array (
'file' => '/var/www/wp-includes/pluggable.php',
'line' => 1382,
'function' => 'apply_filters',
'args' =>
array (
0 => 'wp_redirect',
1 => 'https://example.com/?utm_term&utm_adgroup=domain-name',
2 => 301,
),
),
3 =>
array (
'file' => '/var/www/wp-includes/canonical.php',
'line' => 799,
'function' => 'wp_redirect',
'args' =>
array (
0 => 'https://example.com/?utm_term&utm_adgroup=domain-name',
1 => 301,
),
),
4 =>
array (
'file' => '/var/www/wp-includes/class-wp-hook.php',
'line' => 307,
'function' => 'redirect_canonical',
'args' =>
array (
0 => 'https://example.com/?utm_term=&utm_adgroup=domain-name',
),
),
5 =>
array (
'file' => '/var/www/wp-includes/class-wp-hook.php',
'line' => 331,
'function' => 'apply_filters',
'class' => 'WP_Hook',
'object' =>
WP_Hook::__set_state(array(
'callbacks' =>
array (
0 =>
array (
'_wp_admin_bar_init' =>
array (
'function' => '_wp_admin_bar_init',
'accepted_args' => 1,
),
),
10 =>
array (
'wp_old_slug_redirect' =>
array (
'function' => 'wp_old_slug_redirect',
'accepted_args' => 1,
),
'redirect_canonical' =>
array (
'function' => 'redirect_canonical',
'accepted_args' => 1,
),
'far_template_redirect' =>
array (
'function' => 'far_template_redirect',
'accepted_args' => 1,
),
'000000001c29730b0000000015f85ab7template_redirect' =>
array (
'function' =>
array (
0 =>
WordPress_Module::__set_state(array(
'can_log' => true,
'redirect_url' => 'https://example.com/?utm_term&utm_adgroup=domain-name',
'redirect_code' => 301,
'redirects' =>
array (
),
'matched' => false,
)),
1 => 'template_redirect',
),
'accepted_args' => 1,
),
'et_fb_auto_activate_builder' =>
array (
'function' => 'et_fb_auto_activate_builder',
'accepted_args' => 1,
),
'000000001c2975080000000015f85ab7render_sitemaps' =>
array (
'function' =>
array (
0 =>
WP_Sitemaps::__set_state(array(
'index' =>
WP_Sitemaps_Index::__set_state(array(
'registry' =>
WP_Sitemaps_Registry::__set_state(array(
'providers' =>
array (
),
)),
'max_sitemaps' => 50000,
)),
'registry' =>
WP_Sitemaps_Registry::__set_state(array(
'providers' =>
array (
),
)),
'renderer' =>
WP_Sitemaps_Renderer::__set_state(array(
'stylesheet' => '',
'stylesheet_index' => '',
)),
)),
1 => 'render_sitemaps',
),
'accepted_args' => 1,
),
),
11 =>
array (
'rest_output_link_header' =>
array (
'function' => 'rest_output_link_header',
'accepted_args' => 0,
),
'wp_shortlink_header' =>
array (
'function' => 'wp_shortlink_header',
'accepted_args' => 0,
),
),
),
'iterations' =>
array (
0 =>
array (
0 => 0,
1 => 10,
2 => 11,
),
),
'current_priority' =>
array (
0 => 10,
),
'nesting_level' => 1,
'doing_action' => true,
)),
'type' => '->',
'args' =>
array (
0 => NULL,
1 =>
array (
0 => '',
),
),
),
6 =>
array (
'file' => '/var/www/wp-includes/plugin.php',
'line' => 476,
'function' => 'do_action',
'class' => 'WP_Hook',
'object' =>
WP_Hook::__set_state(array(
'callbacks' =>
array (
0 =>
array (
'_wp_admin_bar_init' =>
array (
'function' => '_wp_admin_bar_init',
'accepted_args' => 1,
),
),
10 =>
array (
'wp_old_slug_redirect' =>
array (
'function' => 'wp_old_slug_redirect',
'accepted_args' => 1,
),
'redirect_canonical' =>
array (
'function' => 'redirect_canonical',
'accepted_args' => 1,
),
'far_template_redirect' =>
array (
'function' => 'far_template_redirect',
'accepted_args' => 1,
),
'000000001c29730b0000000015f85ab7template_redirect' =>
array (
'function' =>
array (
0 =>
WordPress_Module::__set_state(array(
'can_log' => true,
'redirect_url' => 'https://example.com/?utm_term&utm_adgroup=domain-name',
'redirect_code' => 301,
'redirects' =>
array (
),
'matched' => false,
)),
1 => 'template_redirect',
),
'accepted_args' => 1,
),
'et_fb_auto_activate_builder' =>
array (
'function' => 'et_fb_auto_activate_builder',
'accepted_args' => 1,
),
'000000001c2975080000000015f85ab7render_sitemaps' =>
array (
'function' =>
array (
0 =>
WP_Sitemaps::__set_state(array(
'index' =>
WP_Sitemaps_Index::__set_state(array(
'registry' =>
WP_Sitemaps_Registry::__set_state(array(
'providers' =>
array (
),
)),
'max_sitemaps' => 50000,
)),
'registry' =>
WP_Sitemaps_Registry::__set_state(array(
'providers' =>
array (
),
)),
'renderer' =>
WP_Sitemaps_Renderer::__set_state(array(
'stylesheet' => '',
'stylesheet_index' => '',
)),
)),
1 => 'render_sitemaps',
),
'accepted_args' => 1,
),
),
11 =>
array (
'rest_output_link_header' =>
array (
'function' => 'rest_output_link_header',
'accepted_args' => 0,
),
'wp_shortlink_header' =>
array (
'function' => 'wp_shortlink_header',
'accepted_args' => 0,
),
),
),
'iterations' =>
array (
0 =>
array (
0 => 0,
1 => 10,
2 => 11,
),
),
'current_priority' =>
array (
0 => 10,
),
'nesting_level' => 1,
'doing_action' => true,
)),
'type' => '->',
'args' =>
array (
0 =>
array (
0 => '',
),
),
),
7 =>
array (
'file' => '/var/www/wp-includes/template-loader.php',
'line' => 13,
'function' => 'do_action',
'args' =>
array (
0 => 'template_redirect',
),
),
8 =>
array (
'file' => '/var/www/wp-blog-header.php',
'line' => 19,
'args' =>
array (
0 => '/var/www/wp-includes/template-loader.php',
),
'function' => 'require_once',
),
9 =>
array (
'file' => '/var/www/index.php',
'line' => 17,
'args' =>
array (
0 => '/var/www/wp-blog-header.php',
),
'function' => 'require',
),
)
Can anyone point me to the origin or the redirect request please
There is an array of data obtained by Rest API. I am trying to make a menu from this data, but for this I need to convert it with regard to nesting (I think this is done in a loop)
I rummaged through the Internet, everywhere recursion with usual numeric id, and my id is a string, I tried to do it myself through foreach, it turned out
foreach ($elements as $item){
$thisRef = &$ref[$item['objectId']];
$thisRef['parent'] = &$item['parent'];
$thisRef['name'] = $item['property']['ru']['value'];
$thisRef['objectId'] = $item['objectId'];
$thisRef['number'] = $item['number'];
$thisRef['duration'] = $item['duration'];
$thisRef['object'] = $item['object'];
$thisRef['listorder'] = $item['listorder'];
if($item['parent'] == 0) {
$items[$item['objectId']] = &$thisRef;
} else {
$ref[$item['parent']]['child'][$item['objectId']] = &$thisRef;
}
}
print_r($items);
The cycle written by me works poorly, it displays a branch only up to level 2
array of $elements spread below in print_r and var_export
[objectId] is the id of the item
[parent] is the parent of the element, it refers to the parent's objectId, if it is empty or equal to 0, then it has no parent
A big request to offer a solution with unlimited nesting.
I hope you are will help me.
Array through print_r
Array
(
[0] => Array
(
[parent] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
[created] => 1540568545112
[element_id] =>
[ownerId] =>
[type] =>
[duration] =>
[number] => 1
[start_time] =>
[enable] =>
[tiam] =>
[listorder] => 1
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[updated] => 1540569546246
[value] =>
[objectId] => 0DE56A96-9FA9-64E0-FFAB-97A246517200
[object] => round
[___class] => Elements
[property] => Array
(
[ru] => Array
(
[language_code] => ru
[created] => 1540568545865
[lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
[element_id] => 0DE56A96-9FA9-64E0-FFAB-97A246517200
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[type] =>
[ownerId] =>
[value] => Раунд
[updated] =>
[objectId] => C28EE86F-71BC-A6D3-FFF6-707638303500
[object] => round
[___class] => Elements_values
)
)
)
[1] => Array
(
[parent] =>
[created] => 1540540322673
[element_id] =>
[ownerId] =>
[type] =>
[duration] =>
[number] => 1
[start_time] =>
[enable] =>
[tiam] =>
[listorder] => 1
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[updated] => 1540564573693
[value] =>
[objectId] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
[object] => team
[___class] => Elements
[property] => Array
(
[ru] => Array
(
[language_code] => ru
[created] => 1540540323417
[lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
[element_id] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[type] =>
[ownerId] =>
[value] => Команда
[updated] =>
[objectId] => 2B2C86CF-E1F8-5D2C-FF0E-1DB592CC7500
[object] => team
[___class] => Elements_values
)
)
)
[2] => Array
(
[parent] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
[created] => 1540567572958
[element_id] =>
[ownerId] =>
[type] =>
[duration] =>
[number] => 1
[start_time] =>
[enable] =>
[tiam] =>
[listorder] => 1
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[updated] => 1540567674259
[value] =>
[objectId] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
[object] => stage
[___class] => Elements
[property] => Array
(
[ru] => Array
(
[language_code] => ru
[created] => 1540567573722
[lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
[element_id] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[type] =>
[ownerId] =>
[value] => Этап
[updated] =>
[objectId] => 4E694471-70B3-C12B-FF79-97895532DD00
[object] => stage
[___class] => Elements_values
)
)
)
[3] => Array
(
[parent] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
[created] => 1540565297592
[element_id] =>
[ownerId] =>
[type] =>
[duration] =>
[number] => 1
[start_time] =>
[enable] =>
[tiam] =>
[listorder] => 1
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[updated] => 1540565310503
[value] =>
[objectId] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
[object] => route
[___class] => Elements
[property] => Array
(
[ru] => Array
(
[language_code] => ru
[created] => 1540565298342
[lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
[element_id] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
[quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
[type] =>
[ownerId] =>
[value] => Маршрут
[updated] =>
[objectId] => 9CD62B6E-EACF-2911-FF57-3FF369F2BE00
[object] => route
[___class] => Elements_values
)
)
)
)
Array through var_export
array (
0 =>
array (
'parent' => 'D0384DBB-F67C-FE92-FF46-F192B3F83B00',
'created' => 1540568545112,
'element_id' => NULL,
'ownerId' => NULL,
'type' => NULL,
'duration' => NULL,
'number' => '1',
'start_time' => NULL,
'enable' => NULL,
'tiam' => NULL,
'listorder' => '1',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'updated' => 1540569546246,
'value' => NULL,
'objectId' => '0DE56A96-9FA9-64E0-FFAB-97A246517200',
'object' => 'round',
'___class' => 'Elements',
'property' =>
array (
'ru' =>
array (
'language_code' => 'ru',
'created' => 1540568545865,
'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
'element_id' => '0DE56A96-9FA9-64E0-FFAB-97A246517200',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'type' => NULL,
'ownerId' => NULL,
'value' => 'Раунд',
'updated' => NULL,
'objectId' => 'C28EE86F-71BC-A6D3-FFF6-707638303500',
'object' => 'round',
'___class' => 'Elements_values',
),
),
),
1 =>
array (
'parent' => NULL,
'created' => 1540540322673,
'element_id' => NULL,
'ownerId' => NULL,
'type' => NULL,
'duration' => NULL,
'number' => '1',
'start_time' => NULL,
'enable' => NULL,
'tiam' => NULL,
'listorder' => '1',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'updated' => 1540564573693,
'value' => NULL,
'objectId' => '4449513D-1CDD-00EA-FFDF-1947E4FDEA00',
'object' => 'team',
'___class' => 'Elements',
'property' =>
array (
'ru' =>
array (
'language_code' => 'ru',
'created' => 1540540323417,
'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
'element_id' => '4449513D-1CDD-00EA-FFDF-1947E4FDEA00',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'type' => NULL,
'ownerId' => NULL,
'value' => 'Команда',
'updated' => NULL,
'objectId' => '2B2C86CF-E1F8-5D2C-FF0E-1DB592CC7500',
'object' => 'team',
'___class' => 'Elements_values',
),
),
),
2 =>
array (
'parent' => 'F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00',
'created' => 1540567572958,
'element_id' => NULL,
'ownerId' => NULL,
'type' => NULL,
'duration' => NULL,
'number' => '1',
'start_time' => NULL,
'enable' => NULL,
'tiam' => NULL,
'listorder' => '1',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'updated' => 1540567674259,
'value' => NULL,
'objectId' => 'D0384DBB-F67C-FE92-FF46-F192B3F83B00',
'object' => 'stage',
'___class' => 'Elements',
'property' =>
array (
'ru' =>
array (
'language_code' => 'ru',
'created' => 1540567573722,
'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
'element_id' => 'D0384DBB-F67C-FE92-FF46-F192B3F83B00',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'type' => NULL,
'ownerId' => NULL,
'value' => 'Этап',
'updated' => NULL,
'objectId' => '4E694471-70B3-C12B-FF79-97895532DD00',
'object' => 'stage',
'___class' => 'Elements_values',
),
),
),
3 =>
array (
'parent' => '4449513D-1CDD-00EA-FFDF-1947E4FDEA00',
'created' => 1540565297592,
'element_id' => NULL,
'ownerId' => NULL,
'type' => NULL,
'duration' => NULL,
'number' => '1',
'start_time' => NULL,
'enable' => NULL,
'tiam' => NULL,
'listorder' => '1',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'updated' => 1540565310503,
'value' => NULL,
'objectId' => 'F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00',
'object' => 'route',
'___class' => 'Elements',
'property' =>
array (
'ru' =>
array (
'language_code' => 'ru',
'created' => 1540565298342,
'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
'element_id' => 'F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00',
'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
'type' => NULL,
'ownerId' => NULL,
'value' => 'Маршрут',
'updated' => NULL,
'objectId' => '9CD62B6E-EACF-2911-FF57-3FF369F2BE00',
'object' => 'route',
'___class' => 'Elements_values',
),
),
),
)
I need format array
Format example
Array
(
......
[4449513D-1CDD-00EA-FFDF-1947E4FDEA00] => Array
(
[parent] =>
[name] => Команда
[objectId] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
[number] => 1
[duration] =>
[object] => team
[listorder] => 1
[child] => Array
(
[F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00] => Array
(
[parent] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
[name] => Маршрут
[objectId] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
[number] => 1
[duration] =>
[object] => route
[listorder] => 1
[child]=> Array
(
[D0384DBB-F67C-FE92-FF46-F192B3F83B00] => Array
(
[parent] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
[name] => Этап
[objectId] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
[number] => 1
[duration] =>
[object] => stage
[listorder] => 1
)
)
)
)
....
)
)
Try this:
function cascadeElements($elements, &$result, $index = 0, $parent = null) {
$next = function () use ($elements, &$result, $index, $parent) {
cascadeElements($elements, $result, ++$index, $parent);
};
if ($index >= count($elements)) {
return;
}
$element = $elements[$index];
if (!$parent && $element['parent'] !== null) {
return $next();
}
if ($parent && $element['parent'] !== $parent['objectId']) {
return $next();
}
$element['children'] = [];
cascadeElements($elements, $element['children'], 0, $element);
$result[$element['objectId']] = $element;
return $next();
}
You use it like this:
$result = [];
cascadeElements($data, $result);
$result will get filled with objectId keyed hierarchical list of elements where each element has it's children under children key.
This function should work for infinite amount of nesting.
I believe you want to loop through all of the items, current item as $current. If it has a parent, find the parent object $parent and then add the item to the parent's children array($parent.children[] = $current).
// Loop through all
foreach ($elements as $current) {
if (!empty($current['parent'])) {
// Loop through all if they have a parent
foreach ($elements as $parent) {
// Add $current as a child if this is the parent
if ($current['parent'] === $parent['objectId']) {
$parent['children'][] = $current;
}
}
}
}
If you only want parents in the array, you might be able to unset() or just make a new array by checking if the element doesn't have a parent.
Here is input array with an n-level depth
array
(
[0] => array
(
[id] => 1
[parent_id] => 0
[variable_name] => menus
[variable_value] =>
[children] => array
(
[0] => array
(
[id] => 2
[parent_id] => 1
[variable_name] => products
[variable_value] => {"name":"Products", "link":"cctv.html", "show":"1"},
)
[1] => array
(
[id] => 3
[parent_id] => 1
[variable_name] => companies
[variable_value] => {"name":"Companies", "link":"companies.html", "show":"1"},
),
),
)
[1] => array
(
[id] => 4
[parent_id] => 0
[variable_name] => breadcrumbs
[variable_value] =>
[children] => array
(
[0] => array
(
[id] => 5
[parent_id] => 4,
)
[1] => array
(
[id] => 6
[parent_id] => 4
[variable_name] => companies
[variable_value] => {"name":"Companies", "link":"companies.html", "show":"1"},
),
),
)
[2] => array
(
[id] => 7
[parent_id] => 0
[variable_name] => products
[variable_value] =>
[children] => array
(
[0] => array
(
[id] => 8
[parent_id] => 7
[variable_name] => pages
[variable_value] =>
[children] => array
(
[0] => array
(
[id] => 9
[parent_id] => 8
[variable_name] => child_category_page
[variable_value] =>
[children] => array
(
[0] => array
(
[id] => 10
[parent_id] => 9
[variable_name] => middle
[variable_value] =>
[children] => array
(
[0] => array
(
[id] => 11
[parent_id] => 10
[variable_name] => left
[variable_value] => {"name":"Companies", "link":"companies.html", "show":"1", "test":1},
),
),
),
),
),
),
),
),
),
)
I want to convert it into,
[
'menus' => [
'products' => [
'name' => 'Products',
'link' => 'cctv.html',
'show' => true,
],
'companies' => [
'name' => 'Companies',
'link' => 'companies.html',
'show' => true,
],
],
'breadcrumbs' => [
'news' => [
'text' => 'News',
'show' => true,
'link' => 'news.html',
],
'companies' => [
'text' => 'Companies',
'show' => true,
'link' => 'companies.html',
],
],
'products' => [
'pages' => [
'child_category_page' => [
'middle' => [
'left' => [
'text' => 'Companies',
'show' => true,
'link' => 'companies.html',
],
],
],
],
],
];
What I have tried is,
$data = DB::table("SITE_CONFIGS")->where("parent_id", 0)->get();
$data = get_tree_site_configs($data);
function get_tree_site_configs($data, $parent=0, &$result=[]){
$data = json_decode(json_encode($data),true);
$branch = [];
foreach ($data as $key => &$value) {
if($parent == $value['parent_id']){
$has_sub = DB::table("SITE_CONFIGS")->where("parent_id", $value['id'])->get();
$children = get_tree_site_configs($has_sub, $value['id'],$result);
if($children){
$value['children'] = $children;
}
// pr($value);
$branch[] = $value;
}
}
return $branch;
}
Note: There is parent-child relation to n-level, parent id with variable_value are leaf notes, means they don't have any children, rest all are parents of some records.
function get_tree_site_configs(array $config) {
$result = [];
foreach ($config as $item) {
if (isset($item['children'])) {
$value = get_tree_site_configs($item['children']);
} else {
$value = $item['variable_value'];
}
$result[$item['variable_name']] = $value;
}
return $result;
}
I have been trying to get this to work for 7.5 hours now, but my brain has melted.
I have a multi-dimensional array similar to the following:
array (
'expanded' => true,
'key' => 'root_1',
'title' => 'root',
'children' =>
array (
0 =>
array (
'folder' => false,
'key' => '_1',
'title' => 'News',
'data' =>
array (
'id' => '3',
'parent_id' => 0,
),
),
1 =>
array (
'folder' => true,
'key' => '_2',
'title' => 'Animations',
'data' =>
array (
'id' => '5',
'parent_id' => '0',
),
'children' =>
array (
0 =>
array (
'folder' => false,
'key' => '_3',
'title' => 'The Simpsons',
'data' =>
array (
'id' => '1',
'parent_id' => '5',
),
),
1 =>
array (
'folder' => false,
'key' => '_4',
'title' => 'Futurama',
'data' =>
array (
'id' => '4',
'parent_id' => '5',
),
),
),
),
2 =>
array (
'folder' => true,
'key' => '_5',
'title' => 'Episodes',
'data' =>
array (
'id' => '6',
'parent_id' => '0',
),
'children' =>
array (
0 =>
array (
'folder' => true,
'key' => '_6',
'title' => 'UK Episodes',
'data' =>
array (
'id' => '7',
'parent_id' => '6',
),
'children' =>
array (
0 =>
array (
'folder' => false,
'key' => '_7',
'title' => 'Coupling',
'data' =>
array (
'id' => '2',
'parent_id' => '7',
),
),
),
),
1 =>
array (
'folder' => true,
'key' => '_8',
'title' => 'AU Episodes',
'data' =>
array (
'id' => '8',
'parent_id' => '6',
),
),
),
),
),
)
I need to search through all sub arrays and build a new array returning the id and parent_id of each of the children AND also interpreting the order of the array children from the order they appear in the original array.
The output needs to end up something like this:
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 5
[order] => 1
)
[1] => Array
(
[id] => 2
[parent_id] => 7
[order] => 1
)
[2] => Array
(
[id] => 4
[parent_id] => 5
[order] => 2
)
)
I have tried the following recursion approach, however my code turns into a mess, and thats before I've even attempted to set the order of the items.
I've also searched stackoverflow for another example that I could learn from however I haven't found anything yet... If there is another example please feel free to point me in the right direction.
I appreciate any assistance!
A super easy way to run over all these is to use a RecursiveIteratorIterator over a RecursiveArrayIterator, pluck out the 'data' arrays and add in the key as the order.
Example:
$array = []; // Your starting array with stuff in it.
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
$result = [];
foreach ($iterator as $key => $value) {
if (isset($value['data'])) {
$result[] = array_merge($value['data'], ['order' => $key]);
}
}
print_r($result);
Output:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[order] => 0
)
[1] => Array
(
[id] => 5
[parent_id] => 0
[order] => 1
)
[2] => Array
(
[id] => 1
[parent_id] => 5
[order] => 0
)
[3] => Array
(
[id] => 4
[parent_id] => 5
[order] => 1
)
[4] => Array
(
[id] => 6
[parent_id] => 0
[order] => 2
)
[5] => Array
(
[id] => 7
[parent_id] => 6
[order] => 0
)
[6] => Array
(
[id] => 2
[parent_id] => 7
[order] => 0
)
[7] => Array
(
[id] => 8
[parent_id] => 6
[order] => 1
)
)
If i'm understanding what you're trying to do properly the below snippet should work. No need for the order key in each sub array because that's recorded automatically when you insert a new array index into $new_array.
Let me know if you have any other problems.
$new_array = [];
$current_array = []; //with all your data
foreach($current_array['children'] as $array) {
$new_array[] = [
'id' => $array['data']['id'],
'parent_id' => $array['data']['parent_id'];
];
}
I have an "tree" array like the following. This is a navigation. Now i want to remove all levels >= 3. So i only want to get an array with the first two levels. Is there a way to trim/shorten the array like that.
Do you have a hint for me what i can look for?
Array
(
[0] => Array
(
[name] => Home
[level] => 1
[sub] =>
)
[1] => Array
(
[name] => Products
[level] => 1
[sub] => Array
(
[56] => Array
(
[name] => Product 1
[level] => 2
[sub] => Array
(
[61] => Array
(
[name] => Product 1b
[target] =>
[level] => 3
[sub] =>
)
)
)
[57] => Array
(
[name] => Product 2
[level] => 2
[sub] =>
)
)
)
[2] => Array
(
[name] => Contact
[level] => 1
[sub] =>
)
[3] => Array
(
[name] => Something Else
[level] => 1
[sub] =>
)
)
$data = [
0 => ['name' => 'Home', 'level' => 1, 'sub' => []],
1 => [
'name' => 'Products',
'level' => 2,
'sub' => [
'56' => [
'name' => 'Product 1',
'level' => 2,
'sub' => [
'61' => [ 'name' => 'Product 1b', 'target' => '', 'level' => 3, 'sub' => ''],
],
],
'57' => ['name' => 'Product 2', 'level' => 2, 'sub' => '']
]
],
2 => ['name' => 'Contact', 'level' => 1, 'sub' => []],
3 => ['name' => 'Something Else', 'level' => 1, 'sub' => []]
];
function processArray(&$arr) {
foreach ($arr as $key => $array) {
if ($array['level'] >= 3) {
unset($arr[$key]);
}
if (!empty($arr[$key]['sub'])) {
processArray($arr[$key]['sub']);
}
}
}
processArray($data);