-2
我寫了一個函數,以構建一個無限遞歸子類別的類別數組。我遇到的問題是它將子類別添加爲嵌套子數組,因此它是一個多維數組:PHP:需要一維數組,但建設多維
[0] [0],[0] [1],[0] [1] [0],[0] [2]
但我想它看起來像它是一維:
[0],[1],[2],[3],[ 4],[5]
My功能:
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;
}
}
下面是它的建築物的數組:
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] =>
)
)
)
有人可以幫我弄清楚如何修改我的函數來構建單維數組嗎?
謝謝你的時間!
刪除'[]'從變量'$ cat_array3 []'簡單地使用'$ cat_array3 = cat_array($ CAT_ID,$級+ 1);'因爲你調用'cat_array ()'在'$ cat_array3 []'中,沒有必要使用'[]',因爲您已經在'$ cat_array3 []' –
上方的$ cat_array'中創建一個數組了。感謝您的幫助,Manjeet。我嘗試按照指示進行修改,但是當我完成時,$ cat_array上的print_r開始返回完全空白。 –
'print_r($ cat_array3)'在數組合並之前的條件.. –