PHP - recursive function foreach - php

I would to create a recursive function in order to retrieve data from an array and to organize then.
However I have some difficulties to create the right logic. The principle must be apply to any sub level until it ends or nothing is found.
I want to prevent this kind of code by repeating a foreach inside a foreach...:
$cats = get_categories($args);
$categories = array();
foreach($cats as $cat){
$parent = $cat->category_parent;
if ($parent) {
$categories['child'][$parent][$cat->cat_ID] = $cat->name;
} else {
$categories['parent'][$cat->cat_ID] = $cat->name;
}
}
}
if (isset($categories['parent']) && !empty($categories['parent'])) {
foreach($categories['parent'] as $id => $cat){
$new_cats[$id]['title'] = $cat;
if (isset($categories['child'][$id])) {
foreach($categories['child'][$id] as $child_id => $child_cat){
$new_cats[$child_id]['title'] = $child_cat;
$new_cats[$child_id]['parent_id'] = $id;
if (isset($categories['child'][$child_id])) {
foreach($categories['child'][$child_id] as $sub_child_id => $sub_child_cat){
$new_cats[$sub_child_id]['title'] = $sub_child_cat;
$new_cats[$sub_child_id]['parent_id'] = $child_id;
}
}
}
}
}
}
}

May be this code help you to get idea for formatting your desired array format.
<?php
// Main function
function BuildArr($arr)
{
$formattedArr = array();
if(!empty($arr))
{
foreach($arr as $val)
{
if($val['has_children'])
{
$returnArr = SubBuildArr($val['children']); // call recursive function
if(!empty($rs))
{
$formattedArr[] = $returnArr;
}
}
else
{
$formattedArr[] = $val;
}
}
}
return $formattedArr;
}
// Recursive Function( Build child )
function SubBuildArr($arr)
{
$sub_fortmattedArr = array();
if(!empty($arr))
{
foreach($arr as $val)
{
if($val['has_children'])
{
$response = SubBuildArr($val['children']); // call recursive
if(!empty($response))
{
$sub_fortmattedArr[] = $response;
}
}
else
{
$sub_fortmattedArr[] = $arr;
}
}
return $sub_fortmattedArr;
}
}
?>
I use this code for my previous project for generating categories that upto n-th level

Related

Reduce amount of queries and increase performance for categories and subcategories queries

EDIT 3: Got it down to 300-500 ms by changing flatten method to only merge arrays if not empty.
EDIT 2: Got it down to 1.6 seconds by only calling array_replace for non empty array. Now all that is left to do is optimize the function sort_categories_and_sub_categories. That is NOW the bottleneck. If I remove that I am down to 300ms. Any ideas?
get_all_categories_and_sub_categories
foreach(array_keys($categories) as $id)
{
$subcategories = $this->get_all_categories_and_sub_categories($id, $depth + 1);
if (!empty($subcategories))
{
$categories = array_replace($categories, $subcategories);
}
}
EDIT
I improved performance by over 50% (6 seconds --> 2.5 seconds) by doing a cache in the get_all method. It reduces the amount of queries to 1 from 3000. I am still wondering why it is slow.
I have the following method for getting categories and nested sub categories. If a user has a couple hundred (or thousand) top level categories it does a bunch of queries for each category to find the children. In one case I have 3000 categories and it did 3000 queries. Is there a way to optimize this to do less queries? OR should I just check to see if they have a lot of categories NOT to try to show nested too.
function get_all_categories_and_sub_categories($parent_id = NULL, $depth = 0)
{
$categories = $this->get_all($parent_id);
if (!empty($categories))
{
foreach($categories as $id => $value)
{
$categories[$id]['depth'] = $depth;
}
foreach(array_keys($categories) as $id)
{
$categories = array_replace($categories, $this->get_all_categories_and_sub_categories($id, $depth + 1));
}
return $categories;
}
else
{
return $categories;
}
}
function get_all($parent_id = NULL, $limit=10000, $offset=0,$col='name',$order='asc')
{
static $cache = array();
if (!$cache)
{
$this->db->from('categories');
$this->db->where('deleted',0);
if (!$this->config->item('speed_up_search_queries'))
{
$this->db->order_by($col, $order);
}
$this->db->limit($limit);
$this->db->offset($offset);
foreach($this->db->get()->result_array() as $result)
{
$cache[$result['parent_id'] ? $result['parent_id'] : 0][] = array('name' => $result['name'], 'parent_id' => $result['parent_id'], 'id' => $result['id']);
}
}
$return = array();
$key = $parent_id == NULL ? 0 : $parent_id;
if (isset($cache[$key]))
{
foreach($cache[$key] as $row)
{
$return[$row['id']] = array('name' => $row['name'], 'parent_id' => $row['parent_id']);
}
return $return;
}
return $return;
}
function sort_categories_and_sub_categories($categories)
{
$objects = array();
// turn to array of objects to make sure our elements are passed by reference
foreach ($categories as $k => $v)
{
$node = new StdClass();
$node->id = $k;
$node->parent_id = $v['parent_id'];
$node->name = $v['name'];
$node->depth = $v['depth'];
$node->children = array();
$objects[$k] = $node;
}
// list dependencies parent -> children
foreach ($objects as $node)
{
$parent_id = $node->parent_id;
if ($parent_id !== null)
{
$objects[$parent_id]->children[] = $node;
}
}
// clean the object list to make kind of a tree (we keep only root elements)
$sorted = array_filter($objects, array('Category','_filter_to_root'));
// flatten recursively
$categories = self::_flatten($sorted);
$return = array();
foreach($categories as $category)
{
$return[$category->id] = array('depth' => $category->depth, 'name' => $category->name, 'parent_id' => $category->parent_id);
}
return $return;
}
static function _filter_to_root($node)
{
return $node->depth === 0;
}
static function _flatten($elements)
{
$result = array();
foreach ($elements as $element)
{
if (property_exists($element, 'children'))
{
$children = $element->children;
unset($element->children);
}
else
{
$children = null;
}
$result[] = $element;
if (isset($children))
{
$flatened = self::_flatten($children);
if (!empty($flatened))
{
$result = array_merge($result, $flatened);
}
}
}
return $result;
}

Recursive php menu

I have been searching around for some time but cannot seem to find an answer to my problem.
I have a deep nested array that I need to turn into a nested menu.
https://gist.github.com/anonymous/98e0dcf4f2aef40a1da6
I would like it to end up as something like the following.
https://gist.github.com/anonymous/a0dd4c7d047f11a5ce82
class foo {
function NavigationBuild($routes, $child = false) {
if ($child) {
foreach($routes as $route = > $row) {
if (is_array($row['children'])) {
$output. = self::NavigationBuild($row['children'], true);
} else {
$output. = "<li>".$val['route']."MEEEEE</li>";
}
}
} else {
$output. = '<ul>';
foreach($routes as $route = > $row) {
if (!strlen($row['parent'])) {
$output. = "<li>".$route."</li>";
}
foreach($row['children'] as $key = > $val) {
if (is_array($val['children'])) {
$output. = self::NavigationBuild($val['children'], true);
} else {
$output. = "<li>".$val['route']."MEEEEE</li>";
}
}
}
$output. = '</ul>';
}
return $output;
}
}
Figured it out - seems some sleep was needed.
Thanks to all USEFUL input

convert Object to array

I ceated a function to convert a list of parent child to an object of class "city" :
public static function createCity(array $config)
{
$city= new City();
$id=key($config);
$label= $config[$id]['label'];
$city->setId($id);
$city->setLabel($label);
$children = $config[$id]['childrens'];
if(!empty($children)){
foreach($children as $key_child => $child) {
$children = array($key_child => $child);
$city->addChild(self::createCity($children));
}
}
return $city;
}
Now I would to creat a function to do the opposite => convert an object of type Class City to an array,so I do like that :
public function getCityArray(City$rootCity)
{
$result = array();
$result['id'] = $rootCity->getId();
$result['label']= $rootCity->getLabel();
$children = $rootCity->getChildren();
if ( !empty($children)) {
foreach ($children as $key_child => $child) {
$result['childrens'] = array($key_child => $child );
$result[] = $this->getCityArray($child);
}
}
return $result;
}
But it doesn't work because when I do var_dump('$result') so I have a list with no end and the loop does not stop?
Try this. Since I don't know have the full code, not sure if it will work. The class variable $result will contain the results.
$result = array();
public function getCityArray(City $rootCity) {
$result['id'] = $rootCity->getId();
$result['label']= $rootCity->getLabel();
$children = $rootCity->getChildren();
if ( !empty($children)) {
$result['childrens'] = $children;
$this->result[] = $result;
foreach ($children as $key_child => $child) {
$this->getCityArray($child);
}
}
}

Split an array into sub arrays

I would like to split an array:
$o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');
based on the color attribute of each item, and fill corresponding sub arrays
$a = array("green", "yellow", "blue");
function isGreen($var){
return($var->color == "green");
}
$greens = array_filter($o, "isGreen");
$yellows = array_filter($o, "isYellow");
// and all possible categories in $a..
my $a has a length > 20, and could increase more, so I need a general way instead of writing functions by hand
There doesn't seem to exist a function array_split to generate all filtered arrays
or else I need a sort of lambda function maybe
You could do something like:
$o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');
$greens = array_filter($o, function($item) {
if ($item->color == 'green') {
return true;
}
return false;
});
Or if you want to create something really generic you could do something like the following:
function filterArray($array, $type, $value)
{
$result = array();
foreach($array as $item) {
if ($item->{$type} == $value) {
$result[] = $item;
}
}
return $result;
}
$o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');
$greens = filterArray($o, 'color', 'green');
$yellows = filterArray($o, 'color', 'yellow');
In my second example you could just pass the array and tell the function what to filter (e.g. color or some other future property) on based on what value.
Note that I have not done any error checking whether properties really exist
I would not go down the road of creating a ton of functions, manually or dynamically.
Here's my idea, and the design could be modified so filters are chainable:
<?php
class ItemsFilter
{
protected $items = array();
public function __construct($items) {
$this->items = $items;
}
public function byColor($color)
{
$items = array();
foreach ($this->items as $item) {
// I don't like this: I would prefer each item was an object and had getColor()
if (empty($item->color) || $item->color != $color)
continue;
$items[] = $item;
}
return $items;
}
}
$items = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');
$filter = new ItemsFilter($items);
$greens = $filter->byColor('green');
echo '<pre>';
print_r($greens);
echo '</pre>';
If you need more arguments you could use this function:
function splitArray($array, $params) {
$result = array();
foreach ($array as $item) {
$status = true;
foreach ($params as $key => $value) {
if ($item[$key] != $value) {
$status = false;
continue;
}
}
if ($status == true) {
$result[] = $item;
}
}
return $result;
}
$greensAndID1 = splitArray($o, array('color' => 'green', 'id' => 1));

drupal: getting nodeautoterm node ids from taxonomy ids

I'm using drupal's NAT module and need to get the nid from the term id.
Here's what I tried:
foreach ( (array)nat_get_nids($termid) as $natid ) {
$NatName = $natid->name;
}
print $natid;
This does not work.
Node auto term's get nid function is like this:
function nat_get_nids($tids, $get_nodes = FALSE) {
static $nid_cache = NULL;
static $node_cache = NULL;
$return = array();
// Keep processing to a minimum for empty tid arrays.
if (!empty($tids)) {
// Sort tid array to ensure that the cache_string never suffers from order
// issues.
sort($tids);
$cache_string = implode('+', $tids);
if ($get_nodes) {
if (isset($node_cache[$cache_string])) {
return $node_cache[$cache_string];
}
elseif (isset($nid_cache[$cache_string])) {
// If the nid cache stores the same string, node_load() each nid and
// return them.
$return = array();
foreach (array_keys($nid_cache[$cache_string]) as $nid) {
$return[$nid] = node_load($nid);
}
$node_cache[$cache_string] = $return;
return $return;
}
}
else {
if (isset($nid_cache[$cache_string])) {
return $nid_cache[$cache_string];
}
elseif (isset($node_cache[$cache_string])) {
// If the node cache stores the same string, retrieve only the nids and
// return them.
foreach ($node_cache[$cache_string] as $nid => $node) {
$return[$nid] = $node->name;
}
// Cache extracted results.
$nid_cache[$cache_string] = $return;
return $return;
}
}
// Results have not been cached.
$tids = implode(', ', $tids);
$result = db_query("SELECT n.nid, t.name FROM {nat} n INNER JOIN {term_data} t USING (tid) WHERE n.tid IN (". db_placeholders($tids) .")", $tids);
while ($node = db_fetch_object($result)) {
if ($get_nodes) {
$return[$node->nid] = node_load($node->nid);
}
else {
$return[$node->nid] = $node->name;
}
}
if ($get_nodes) {
$node_cache[$cache_string] = $return;
}
else {
$nid_cache[$cache_string] = $return;
}
}
return $return;
}
Thanks in advance!
Edit: Try based on the first answer:
foreach (nat_get_nids($termid) as $nid => $node_name) {
}
print $node_name;
It looks like nat_get_nids is returning an associative array, so your for loop should look like
foreach (nat_get_nids($termid) as $nid => $node_name) {
...
}
$nids = nat_get_nids(array($termid));

Categories