吾愛編程在開發PHP的時候用到了對數組的處理,查閱了網上的一些資料沒有找到自己想要的,吾愛編程就自己謝了一個方法,現在分享給大家,代碼如下:
$data=array(
array(
'id' => 2,
'text' => '精選',
'is_select' => 1,
'open' => 0
),
array(
'id' => 3,
'text' => '養車',
'is_select' =>0,
'open' => 0
),
array(
'id' => 4,
'text' => '用車',
'is_select' =>0,
'open' => 0
),
);
輸出格式如下:
Array
(
[0] => Array
(
[id] => 2
[text] => 精選
[is_select] => 1
[open] => 0
)
[1] => Array
(
[id] => 3
[text] => 養車
[is_select] => 0
[open] => 0
)
[2] => Array
(
[id] => 4
[text] => 用車
[is_select] => 0
[open] => 0
)
)
以ID的值作為下標
使用一下方法處理即可
$tmp_data=array();
foreach($data as $Row){
$tmp_data[$Row['id']]= $Row;
}
處理之後輸出格式如下:
Array
(
[2] => Array
(
[id] => 2
[text] => 精選
[is_select] => 1
[open] => 0
)
[3] => Array
(
[id] => 3
[text] => 養車
[is_select] => 0
[open] => 0
)
[4] => Array
(
[id] => 4
[text] => 用車
[is_select] => 0
[open] => 0
)
)
*