Category : recursion

I’m modifying an open source source script to show genealogy via organization chart style. having users table with id and parentid field. when I open /viewOrgChart?user_id=1 or numbers not in 2 3 4 5, it works well! BUT when I open with user_id 2 3 4 5, it throw ERROR: Trying to get property ‘parentid’ ..

Read more

I wrote some code like below, not work,don’t know how to make it work.I want to use it like treeRecursively($tree, function () {// some logic to change the tree}); can somebody help me? protected function treeRecursively(array $tree, callable $callable) { foreach ($tree as &$node) { if (isset($node[‘hasChildren’]) && $node[‘hasChildren’]) { $node[‘children’] = $this->treeRecursively($node[‘children’], $callable); } ..

Read more

I’m using this recursive function to get the grand parent of an option. As far as the needle exists in the array keys, it mean that needle has a parent. When not found, then the needle do not have any parent, so it must return the needle itself. get_parent_need(165,$hay); function get_parent_need($needle,$hay){ if(in_array($needle,array_keys($hay))){ get_parent_need($hay[$needle],array_keys($hay)); }else{ return ..

Read more

From the following php script: while($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) { $category = $row["category"]; $thing = $row["thing"]; echo $category." ".$thing; echo "<br>"; } I echo the following lines: Book book1 Book book2 Chair chair1 Chair chair2 Table table1 but I would like to display as the following result : Book book1 book2 Chair chair1 chair2 Table ..

Read more

show() function returns wrong data,why there is only one children how to modify recursiveData function Expectation result: [ { "id": 1, "title": "p1", "parentId": 0, "children": [ { "id": 5, "title": "p1-child1", "parentId": 1, "children": [ { "id": 13, "title": "p5-child", "parentId": 5, "children": [ { "id": 14, "title": "p13-child", "parentId": 13 } ] } ..

Read more

can you please tell me what could possible make this function: $subcategories = array(); function get_subcategories($id){ global $subcategories; echo $query = "SELECT id FROM shop_products_categories where parent=’".$id."’ order by bg_category ASC"; $res = mysql_query($query) or die($query); $num_rows = mysql_num_rows($res); if($num_rows > 0){ while($row = mysql_fetch_array($res)){ $subcategories[]= $row[‘id’]; get_subcategories($row[‘id’]); } } //var_dump($subcategories); return $subcategories; } to ..

Read more

I need to recursively get all permutations in order of an array of groups. So the Array would look something like: ([8,2], [5,7,9], [3,2]) The program would put together a string that would look something like this (pipes are simply added for aesthetics): 8, 5, 3 | 8, 5, 2 | 8, 7, 3 | ..

Read more