I need to modify my array to stop building array as multidimensional

Hi there everyone!

I’ve written a function to build an array of categories with unlimited recursive child categories. The issue I’m running into is that it’s adding the children categories as nested sub-arrays, so it’s a multidimensional array:

[0][0], [0][1], [0][1][0], [0][2]

But I would like it to look like it to be one-dimensional:

[0], [1], [2], [3], [4], [5]

My function:

function cat_array($parent, $level){
	global $link;
	$q_cats = "SELECT * FROM categories WHERE parent = '$parent' ORDER BY name ASC";
	$r_cats = mysqli_query ($link, $q_cats) or die('Catastrophic failure [Super secret code 95162]');
	$num_cats=mysqli_num_rows($r_cats);
	if($num_cats != 0){
		while ($row_cats = mysqli_fetch_assoc ($r_cats)) {
			$cat_id = $row_cats['id'];
			$name = $row_cats['name'];
			$safename = $row_cats['safename'];
			$parent = $row_cats['parent'];
			$catcounter = mysqli_query($link, "SELECT COUNT(*) AS id FROM links WHERE category = '$cat_id'");
			$catnum = mysqli_fetch_array($catcounter);
			$catcount = number_format($catnum["id"]);
			
			$cat_array = array("id"=>$cat_id, "name"=>$name, "safename"=>$safename, "parent"=>$parent, "linkcount"=>$catcount, "level"=>$level);
			
			/* This is supposed to use the function again inside this loop to find the children of the current row */
			$cat_array3[] = cat_array($cat_id, $level+1);
			
			/* If the child loop isn't empty, merge them. */
			if(!empty($cat_array3)){
				$cat_array4[] = array_merge($cat_array, $cat_array3);
			}
			
			/* Wiping the temp arrays */
			UNSET($cat_array);
			UNSET($cat_array2);
			UNSET($cat_array3);
			
			
		}
		/* Placed inside the count check so it doesn't return anything if nothing found */
		$cat_array = $cat_array4;
		return $cat_array;
	}
	
}

Here’s the array it’s building:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => ALTERNATOR, BATTERY & CHARGING
            [safename] => alternator-battery-and-charging
            [parent] => 0
            [linkcount] => 0
            [level] => 0
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => ALTERNATOR
                            [safename] => alternator
                            [parent] => 1
                            [linkcount] => 0
                            [level] => 1
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 35
                                            [name] => Loose or Weak Contact at Generator Harness Connector TSB 96-21-4 for 86-93 Bronco
                                            [safename] => loose-or-weak-contact-at-generator-harness-connector-tsb-96-21-4-for-86-93-bronco
                                            [parent] => 3
                                            [linkcount] => 0
                                            [level] => 2
                                            [0] => 
                                        )

                                    [1] => Array
                                        (
                                            [id] => 36
                                            [name] => No Crank, Low State Of Battery Charge TSB 91-10-8 for 85-91 Bronco, Bronco II, Econoline, F-150-350 Series, Ranger; 86-91 Aerostar; 88-91 F Super Duty, F47, F-53, F-59; 91 Explorer, etc.
                                            [safename] => no-crank-low-state-of-battery-charge-tsb-91-10-8-for-85-91-bronco-bronco-ii-econoline-f-150-350-series-ranger-86-91-aerostar-88-91-f-super-duty-f47-f-53-f-59-91-explorer-etc-
                                            [parent] => 3
                                            [linkcount] => 0
                                            [level] => 2
                                            [0] => 
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 4
                            [name] => BATTERY & ISOLATOR
                            [safename] => battery-and-isolator
                            [parent] => 1
                            [linkcount] => 0
                            [level] => 1
                            [0] => 
                        )

                    [2] => Array
                        (
                            [id] => 5
                            [name] => GENERAL INFORMATION
                            [safename] => general-information
                            [parent] => 1
                            [linkcount] => 0
                            [level] => 1
                            [0] => 
                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => AUDIO AND VIDEO
            [safename] => audio-and-video
            [parent] => 0
            [linkcount] => 0
            [level] => 0
            [0] => 
        )

    [2] => Array
        (
            [id] => 6
            [name] => BODY
            [safename] => body
            [parent] => 0
            [linkcount] => 0
            [level] => 0
            [0] => 
        )
    )
)

Could someone help me figure out how I would modify my function to build a single dimensional array?

Thanks for your time!

just use what your database gives you back, skipping the where parent = ... part, no function needed.

If you could explain how this could be ordered to place children alphabetically below it’s parent with no limitations of how far it could work recursively, I would definitely use this method but that’s why I abandoned it.

I’ve simplified the function considerably and if I print_r inside the function, it’s succeeding but for some reason, printing $cat_array outside of the function results in blank. Could someone explain why?

[code]function cat_array($parent, $level){
global $link;
global $cat_array;
if(!ISSET($cat_array)){
$cat_array = array();
}
$q_cats = “SELECT * FROM categories WHERE parent = ‘$parent’ ORDER BY name ASC”;
$r_cats = mysqli_query ($link, $q_cats) or die(‘Catastrophic failure [Super secret code 95162]’);
$num_cats=mysqli_num_rows($r_cats);
if($num_cats != 0){
while ($row_cats = mysqli_fetch_assoc ($r_cats)) {
$cat_id = $row_cats[‘id’];
$name = $row_cats[‘name’];
$safename = $row_cats[‘safename’];
$parent = $row_cats[‘parent’];
$catcounter = mysqli_query($link, “SELECT COUNT(*) AS id FROM links WHERE category = ‘$cat_id’”);
$catnum = mysqli_fetch_array($catcounter);
$catcount = number_format($catnum[“id”]);

		$cat_array1 = array("id"=>$cat_id, "name"=>$name, "safename"=>$safename, "parent"=>$parent, "linkcount"=>$catcount, "level"=>$level);
		
		$cat_array = array_merge($cat_array, $cat_array1);
					
		/* This is supposed to use the function again inside this loop to find the children of the current row */
		cat_array($cat_id, $level+1);
		
		/* Wiping the temp arrays */
		UNSET($cat_array1);
		
		
	}
	
}
return $cat_array;

}[/code]

Solved my issue:

function cat_array($parent, $level){
	$go = 1;
	global $link;
	if(!ISSET($cat_array)){
		$cat_array = array();
	}
	$q_cats = "SELECT * FROM categories WHERE parent = '$parent' ORDER BY name ASC";
	$r_cats = mysqli_query ($link, $q_cats) or die('Catastrophic failure [Super secret code 95162]');
	$num_cats=mysqli_num_rows($r_cats);
	if($num_cats != 0){
		while ($row_cats = mysqli_fetch_assoc ($r_cats)) {
			$cat_id = $row_cats['id'];
			$name = $row_cats['name'];
			$safename = $row_cats['safename'];
			$parent = $row_cats['parent'];
			$catcounter = mysqli_query($link, "SELECT COUNT(*) AS id FROM links WHERE category = '$cat_id'");
			$catnum = mysqli_fetch_array($catcounter);
			$catcount = number_format($catnum["id"]);
			
			$cat_array1[] = array("id"=>$cat_id, "name"=>$name, "safename"=>$safename, "parent"=>$parent, "linkcount"=>$catcount, "level"=>$level);
			
			$cat_array = array_merge($cat_array, $cat_array1);
						
			/* This is supposed to use the function again inside this loop to find the children of the current row */
			$cat_array3 = cat_array($cat_id, $level+1);
			
			if(ISSET($cat_array3) AND !empty($cat_array3)){
				$cat_array = array_merge($cat_array, $cat_array3);
			}
			
		}
		
	}
	return $cat_array;
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.