導航:首頁 > 編程語言 > php數組樹形

php數組樹形

發布時間:2023-01-21 09:47:49

php遞歸二維數組怎樣一步到位拼接

foreach 遍歷圈套循環也是可以 但是在數據上要做區分 主要做出符合自己邏輯的比較便利

❷ php遞歸生成樹型數組

public function tree(&$list, $parent) {
//保證找到的元素都放在一個數組$tree內,應該是$tree在所有的tree函數都共享
static $tree;
//遍歷該數組,找到parent值為當前傳遞進來的$parent_id;
foreach($list as $row) {
//判斷
if($row['parent'] == $parent) {
//保存起來
$tree[] = $row;
//依據當前所找到的分類,找到其子節點,操作相同,遞歸完成
$this->tree($list, $row['id']);
}
}
return $tree;
}
你把你的數組帶進去,這個是在類裡面實現的,如果你只是想單獨用個方法,你就把public以及'$this->'去掉就行了

❸ php sql條件參數為數組

你先網路php數組轉字元,中間用sql認識的,連接。自己查記得深
就是$id = "2,3";的效果
然後sql句子是select * from table where id in ($id)

❹ PHP遞歸樹形菜單

可以參考以下代碼,但需要把你自己的資料庫鏈接,表等改一下就可以了

<html>
<head>
<link href='style.css' rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javaScript" src="TreeMenu.js"></script>
</head>
<body>
<?php
//基本變數設置
$GLOBALS["ID"] =1; //用來跟蹤下拉菜單的ID號
$layer=1; //用來跟蹤當前菜單的級數
//連接資料庫
$Con=mysql_connect("localhost","root","1234");
mysql_select_db("wiki");
//提取一級菜單
$sql="SELECT * FROM wiki where pid=0";
$result=mysql_query($sql,$Con);
//如果一級菜單存在則開始菜單的顯示
if(mysql_num_rows($result)>0) ShowTreeMenu($Con,$result,$layer,$ID);
//=============================================
//顯示樹型菜單函數 ShowTreeMenu($con,$result,$layer)
//$con:資料庫連接
//$result:需要顯示的菜單記錄集
//layer:需要顯示的菜單的級數
//=============================================
function ShowTreeMenu($Con,$result,$layer)
{
//取得需要顯示的菜單的項目數
$numrows=mysql_num_rows($result);

//開始顯示菜單,每個子菜單都用一個表格來表示
echo "<table cellpadding='0' cellspacing='0' border='0'>";
for($rows=0;$rows<$numrows;$rows++)
{
//將當前菜單項目的內容導入數組
$menu=mysql_fetch_array($result);
//提取菜單項目的子菜單記錄集
$sql="select * from wiki where pid=$menu[cid]";
$result_sub=mysql_query($sql,$Con);
echo "<tr>";
//如果該菜單項目有子菜單,則添加JavaScript onClick語句
if(mysql_num_rows($result_sub)>0)
{
echo "<td width='20'><img src='folder.gif' border='0'></td>";
echo "<td class='Menu' onClick='javascript:ShowMenu(Menu".$GLOBALS["ID"].");'>";
}
else
{
echo "<td width='20'><img src='file.gif' border='0'></td>";
echo "<td class='Menu'>";
}
//如果該菜單項目沒有子菜單,並指定了超級連接地址,則指定為超級連接,
//否則只顯示菜單名稱
//if($menu[url]!="")
//echo "<a href='$menu[cid]'>$menu[name]</a>";
//else
echo $menu['name'];
echo "
</td>
</tr>
";
//如果該菜單項目有子菜單,則顯示子菜單
if(mysql_num_rows($result_sub)>0)
{
//指定該子菜單的ID和style,以便和onClick語句相對應
echo "<tr id=Menu".$GLOBALS["ID"]++." style='display:none'>";
echo "<td width='20'> </td>";
echo "<td>";
//將級數加1
$layer++;
//遞歸調用ShowTreeMenu()函數,生成子菜單
ShowTreeMenu($Con,$result_sub,$layer);
//子菜單處理完成,返回到遞歸的上一層,將級數減1
$layer--;
echo "</td></tr>";
}
//繼續顯示下一個菜單項目
}
echo "</table>";
}
?>
</body>
</html>

❺ PHP如何動態為多維數組的子數組分別添加元素

$a=array(0=>array("name"=>"a","mail"=>"b"),1=>array("name"=>"d","mail"=>"e"));
functionaddkey(&$val,$key,$param)
{
$val[$param['key']]=$param['val'];
}
array_walk($a,'addkey',array('key'=>'tel','val'=>'123'));
print_r($a);

❻ php中多維數組的問題

"Griffin"=>array()
表示索引"Griffin"是一個數組。=>可以簡單理解為賦值。這是php里特有的一種寫法。
數組分為2種,一種是自動索引數組。比如
$x=array ("Peter","Lois", "Megan");
那麼$X[0]值為"peter",$X[1]為lois。
還有一種是自定義索引數組。
比如
$x=array ("father"=>"Peter","mother"=>"Lois","son"=> "Megan");
那麼$x["father"] 就為"peter"
用引號圍起來表示這是一個索引字元串值。通常情況下你直接[Griffin]也可以。
但是如果你在系統里有一個變數
$Griffin="son";
那麼$families[Griffin]實際上會等於$families['son']。所以最好用引號圍起來。
更多詳細可以看php手冊數組一章。

==========================================================
數組
PHP 中的 數組 實際上是一個有序映射。映射是一種把 values 關聯到 keys 的類型。此類型在很多方面做了優化,因此可以把它當成真正的數組,或列表(向量),散列表(是映射的一種實現),字典,集合,棧,隊列以及更多可能性。數組元素的值也可以是另一個數組。樹形結構和多維數組也是允許的。

解釋這些結構超出了本手冊的范圍,但對於每種結構至少會提供一個例子。要得到這些結構的更多信息,建議參考有關此廣闊主題的其它著作。

語法
定義數組 array()
可以用 array() 語言結構來新建一個 array。它接受任意數量用逗號分隔的 鍵(key) => 值(value) 對。

array( key => value
, ...
)
// 鍵(key) 可是是一個 整數(integer) 或 字元串(string)
// 值(value) 可以是任意類型的值<?php
$arr = array("foo" => "bar", 12 => true);

echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
key 可以是 integer 或者 string。如果key是一個 integer 的標准表示,則被解釋為整數(例如 "8" 將被解釋為 8,而 "08" 將被解釋為 "08")。key 中的浮點數被取整為 integer。在 PHP 中索引數組與關聯 數組 是相同的,它們都可以同時包含 整型 和 字元串 的下標。

值可以是任意的 PHP 類型。

<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
如果對給出的值沒有指定鍵名,則取當前最大的整數索引值,而新的鍵名將是該值加一。如果指定的鍵名已經有了值,則該值會被覆蓋。

<?php
// 這個數組與下面的數組相同 ...
array(5 => 43, 32, 56, "b" => 12);

// ...
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Warning
自 PHP 4.3.0 起,上述的索引生成方法改變了。如今如果給一個當前最大鍵名是負值的數組添加一個新值,則新生成的的索引將為零(0)。以前新生成的索引為當前最大索引加一,和正值的索引相同。

使用 TRUE 作為鍵名將使 integer 1 成為鍵名。使用 FALSE 作為鍵名將使 integer 0 成為鍵名。使用 NULL 作為鍵名將等同於使用空字元串。使用空字元串作為鍵名將新建(或覆蓋)一個用空字元串作為鍵名的值,這和用空的方括弧不一樣。

不能用數組和對象作為鍵(key)。這樣做會導致一個警告:Illegal offset type。

用方括弧的語法新建/修改
可以通過明示地設定值來改變一個現有的數組。

這是通過在方括弧內指定鍵名來給數組賦值實現的。也可以省略鍵名,在這種情況下給變數名加上一對空的方括弧(「[]」)。

$arr[key] = value;
$arr[] = value;
// key 可以是 integer 或 string
// value 可以是任意類型的值如果 $arr 還不存在,將會新建一個。這也是一種定義數組的替換方法。要改變一個值,只要給它賦一個新值。如果要刪除一個鍵名/值對,要對它用 unset()。

<?php
$arr = array(5 => 1, 12 => 2);

$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script

$arr["x"] = 42; // This adds a new element to
// the array with key "x"

unset($arr[5]); // This removes the element from the array

unset($arr); // This deletes the whole array
?>
Note:

如上所述,如果給出方括弧但沒有指定鍵名,則取當前最大整數索引值,新的鍵名將是該值 + 1。如果當前還沒有整數索引,則鍵名將為 0。如果指定的鍵名已經有值了,該值將被覆蓋。

注意這里所使用的最大整數鍵名不一定當前就在數組中。它只要在上次數組重新生成索引後曾經存在過就行了。以下面的例子來說明:

<?php
// 創建一個簡單的數組
$array = array(1, 2, 3, 4, 5);
print_r($array);

// 現在刪除其中的所有元素,但保持數組本身不變:
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);

// 添加一個單元(注意新的鍵名是 5,而不是你可能以為的 0)
$array[] = 6;
print_r($array);

// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
以上常式會輸出:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
)
Array
(
[5] => 6
)
Array
(
[0] => 6
[1] => 7
)

實用函數
有很多操作數組的函數,參見數組函數一節。

Note:

unset() 函數允許刪除數組中的某個鍵。但要注意數組將不會重建索引。 If a true "remove and shift" behavior is desired, the array can be reindexed using the array_values() function.

<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* will proce an array that would have been defined as
$a = array(1 => 'one', 3 => 'three');
and NOT
$a = array(1 => 'one', 2 =>'three');
*/

$b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
?>

foreach 控制結構是專門用於數組的。它提供了一個簡單的方法來遍歷數組。

數組做什麼和不做什麼
為什麼 $foo[bar] 錯了?
應該始終在用字元串表示的數組索引上加上引號。例如用 $foo['bar'] 而不是 $foo[bar]。但是為什麼 $foo[bar] 錯了呢?可能在老的腳本中見過如下語法:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>
這樣是錯的,但可以正常運行。那麼為什麼錯了呢?原因是此代碼中有一個未定義的常量(bar)而不是字元串('bar'-注意引號),而 PHP 可能會在以後定義此常量,不幸的是你的代碼中有同樣的名字。它能運行,是因為 PHP 自動將裸字元串(沒有引號的字元串且不對應於任何已知符號)轉換成一個其值為該裸字元串的正常字元串。例如,如果沒有常量定義為 bar,PHP 將把它替代為 'bar' 並使用之。

Note: 這並不意味著總是給鍵名加上引號。用不著給鍵名為常量或變數的加上引號,否則會使 PHP 不能解析它們。

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
echo "\nChecking $i: \n";
echo "Bad: " . $array['$i'] . "\n";
echo "Good: " . $array[$i] . "\n";
echo "Bad: {$array['$i']}\n";
echo "Good: {$array[$i]}\n";
}
?>
以上常式會輸出:

Checking 0:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 1
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 1

Checking 1:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 2
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 2

演示此行為的更多例子:

<?php
// Show all errors
error_reporting(E_ALL);

$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// Correct
print $arr['fruit']; // apple
print $arr['veggie']; // carrot

// Incorrect. This works but also throws a PHP error of level E_NOTICE because
// of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple

// This defines a constant to demonstrate what's going on. The value 'veggie'
// is assigned to a constant named fruit.
define('fruit', 'veggie');

// Notice the difference now
print $arr['fruit']; // apple
print $arr[fruit]; // carrot

// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]"; // Hello apple

// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple

// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";

// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
當打開 error_reporting 來顯示 E_NOTICE 級別的錯誤(例如將其設為 E_ALL)時將看到這些錯誤。默認情況下 error_reporting 被關閉不顯示這些。

和在語法一節中規定的一樣,在方括弧(「[」和「]」)之間必須有一個表達式。這意味著可以這樣寫:

<?php
echo $arr[somefunc($bar)];
?>
這是一個用函數返回值作為數組索引的例子。PHP 也可以用已知常量,可能之前已經見過

<?php
$error_descriptions[E_ERROR] = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice";
?>
注意 E_ERROR 也是個合法的標識符,就和第一個例子中的 bar 一樣。但是上一個例子實際上和如下寫法是一樣的:

<?php
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>
因為 E_ERROR 等於 1, 等等.

那麼為什麼這樣做不好?
也許有一天,PHP 開發小組可能會想新增一個常量或者關鍵字,或者用戶可能希望以後在自己的程序中引入新的常量,那就有麻煩了。例如已經不能這樣用 empty 和 default 這兩個詞了,因為他們是保留字。

Note: 重申一次,在雙引號字元串中,不給索引加上引號是合法的因此 "$foo[bar]"是合法的(「合法」的原文為valid。在實際測試中,這么做確實可以訪問數組的該元素,但是會報一個常量未定義的notice。無論如何,強烈建議不要使用$foo[bar]這樣的寫法,而要使用$foo['bar']來訪問數組中元素。--haohappy注)。至於為什麼參見以上的例子和字元串中的變數解析中的解釋。

轉換為數組
對於任意類型: integer, float, string, boolean and resource,如果將一個值轉換為數組,將得到一個僅有一個元素的數組(其下標為 0),該元素即為此標量的值。換句話說, (array)$scalarValue 與 array($scalarValue) 完全一樣。

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:

<?php

class A {
private $A; // This will become '\0A\0A'
}

class B extends A {
private $A; // This will become '\0B\0A'
public $AA; // This will become 'AA'
}

var_mp((array) new B());
?>
The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'.

將 NULL 轉換到 數組(array) 會得到一個空的數組。

比較
可能使用 array_diff() 和數組運算符來比較數組。

Examples
PHP 中的數組類型有非常多的用途,因此這里有一些例子展示數組的完整威力。

<?php
// This:
$a = array( 'color' => 'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4 // key will be 0
);

$b = array('a', 'b', 'c');

// . . .is completely equivalent with this:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0

$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';

// After the above code is executed, $a will be the array
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round',
// 'name' => 'apple', 0 => 4), and $b will be the array
// array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c').
?>
Example #1 Using array()

<?php
// Array as (property-)map
$map = array( 'version' => 4,
'OS' => 'Linux',
'lang' => 'english',
'short_tags' => true
);

// strictly numerical keys
$array = array( 7,
8,
0,
156,
-10
);
// this is the same as array(0 => 7, 1 => 8, ...)

$switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (maximum of integer-indices was 5)
'8' => 2, // key = 8 (integer!)
'02' => 77, // key = '02'
0 => 12 // the value 10 will be overwritten by 12
);

// empty array
$empty = array();
?>
Example #2 集合

<?php
$colors = array('red', 'blue', 'green', 'yellow');

foreach ($colors as $color) {
echo "Do you like $color?\n";
}

?>
以上常式會輸出:

Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?
直接改變數組的值在 PHP 5 中可以通過引用傳遞來做到。之前的版本需要需要採取變通的方法:

Example #3 集合

<?php
// PHP 5
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */

// Workaround for older versions
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
}

print_r($colors);
?>
以上常式會輸出:

Array
(
[0] => RED
[1] => BLUE
[2] => GREEN
[3] => YELLOW
)
本例生成一個下標從1開始的數組。This example creates a one-based array.

Example #4 下標從1開始的數組

<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>
以上常式會輸出:

Array
(
[1] => 'January'
[2] => 'February'
[3] => 'March'
)
Example #5 填充數組

<?php
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>
數組是有序的。也可以使用不同的排序函數來改變順序。更多信息參見數組函數。可以用 count() 函數來數出數組中元素的個數。

Example #6 數組排序

<?php
sort($files);
print_r($files);
?>
因為數組中的值可以為任意值,也可是另一個數組。這樣可以產生遞歸或多維數組。

Example #7 遞歸和多維數組

<?php
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
);

// Some examples to address values in the array above
echo $fruits["holes"][5]; // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]); // remove "first"

// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
?>
數組(Array) 的賦值總是會涉及到值的拷貝。使用 引用操作符 通過引用來拷貝數組。

<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
// $arr1 is still array(2, 3)

$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>

❼ 用php調資料庫做樹狀顯示

資料庫設計的時候,通常的做法是用父ID來解決樹狀結構,也有二叉樹等等


id pid category_name


然後,用遞歸就能實現,也有引用數組的方式

<?php

/**
*此方法由@Tonton提供
*http://my.oschina.net/u/918697
*@date2012-12-12
*/
functiongenTree5($items){
foreach($itemsas$item)
$items[$item['pid']]['son'][$item['id']]=&$items[$item['id']];
returnisset($items[0]['son'])?$items[0]['son']:array();
}

/**
*將數據格式化成樹形結構
*@authorXuefen.Tong
*@paramarray$items
*@returnarray
*/
functiongenTree9($items){
$tree=array();//格式化好的樹
foreach($itemsas$item)
if(isset($items[$item['pid']]))
$items[$item['pid']]['son'][]=&$items[$item['id']];
else
$tree[]=&$items[$item['id']];
return$tree;
}

$items=array(
1=>array('id'=>1,'pid'=>0,'name'=>'江西省'),
2=>array('id'=>2,'pid'=>0,'name'=>'黑龍江省'),
3=>array('id'=>3,'pid'=>1,'name'=>'南昌市'),
4=>array('id'=>4,'pid'=>2,'name'=>'哈爾濱市'),
5=>array('id'=>5,'pid'=>2,'name'=>'雞西市'),
6=>array('id'=>6,'pid'=>4,'name'=>'香坊區'),
7=>array('id'=>7,'pid'=>4,'name'=>'南崗區'),
8=>array('id'=>8,'pid'=>6,'name'=>'和興路'),
9=>array('id'=>9,'pid'=>7,'name'=>'西大直街'),
10=>array('id'=>10,'pid'=>8,'name'=>'東北林業大學'),
11=>array('id'=>11,'pid'=>9,'name'=>'哈爾濱工業大學'),
12=>array('id'=>12,'pid'=>8,'name'=>'哈爾濱師范大學'),
13=>array('id'=>13,'pid'=>1,'name'=>'贛州市'),
14=>array('id'=>14,'pid'=>13,'name'=>'贛縣'),
15=>array('id'=>15,'pid'=>13,'name'=>'於都縣'),
16=>array('id'=>16,'pid'=>14,'name'=>'茅店鎮'),
17=>array('id'=>17,'pid'=>14,'name'=>'大田鄉'),
18=>array('id'=>18,'pid'=>16,'name'=>'義源村'),
19=>array('id'=>19,'pid'=>16,'name'=>'上壩村'),
);
echo"<pre>";
print_r(genTree5($items));
print_r(genTree9($items));
?>

❽ php sql條件參數為數組

1、將二維數組轉換為一維數組

$arr_2 (這是二維數組,就是你上邊那個類型的,我這里就不貼了)
$arr_1 (這個保存轉化後的一維數組)

下面是二維轉化一維數組過程:
for($i=0;$i<count($arr_1);$i++){
$arr[] = $arr_2[$i]['cat_id'];
}

下面是循環輸出一維數組:

foreach($arr_1 as $key=>$val){
echo $key . '<br>' .$val;
}

輸出結果是所有的cat_id 鍵值。至於sql 中的循環,我想你看了這個以後應該會了。
原理就是將二維轉換為一維數組,然後根據條件實現循環查詢。這個轉化的過程,你可以使用for循環,也可以使用php自帶的合並數組函數,也可以使用高級點的spl高級數組合並。當然看你程序需要,如果是應用比較簡單的,循環是最佳選擇。

另外,邀請你加入我們php實驗室,相互學習。在此,祝聖誕快樂。

❾ 求助,php遞歸裡面使用靜態變數保存數組,怎麼處理成樹形結構

$arr = [
['cid'=>1,'name'=>1,'pid'=>0],
['cid'=>2,'name'=>2,'pid'=>1],
['cid'=>3,'name'=>3,'pid'=>2],
['cid'=>4,'name'=>4,'pid'=>0],
['cid'=>5,'name'=>5,'pid'=>4],
['cid'=>6,'name'=>6,'pid'=>4],
['cid'=>7,'name'=>7,'pid'=>5],
['cid'=>8,'name'=>8,'pid'=>5],
];

$re = GetTree($arr);
print_r($re);

function GetTree($arr,$pid = 0,&$re = []){
foreach($arr as $key=>$val) {
if($val['pid'] == $pid) {
$val['sub'] = GetTree($arr , $val['cid']);
$re[] = $val;
}
}
return $re;
}

閱讀全文

與php數組樹形相關的資料

熱點內容
我的世界如何查看伺服器種子pc 瀏覽:284
linuxlamp編譯安裝 瀏覽:609
枚舉演算法ppt 瀏覽:184
cmd查看進程命令 瀏覽:956
手機內怎麼刪除APP 瀏覽:834
魚群和鳥群演算法區別 瀏覽:93
pdf尺寸設置 瀏覽:211
android訪問本地伺服器 瀏覽:512
程序員相親被刪除微信 瀏覽:790
centos命令窗口 瀏覽:596
編譯器有幾個好用的 瀏覽:500
資料庫和網站如何搭載伺服器 瀏覽:154
網路流理論演算法與應用 瀏覽:795
java和matlab 瀏覽:388
釘釘蘋果怎麼下app軟體 瀏覽:832
php網站驗證碼不顯示 瀏覽:859
鋁膜構造柱要設置加密區嗎 瀏覽:344
考駕照怎麼找伺服器 瀏覽:885
阿里雲伺服器如何更換地區 瀏覽:972
手機app調音器怎麼調古箏 瀏覽:505