我得到了一個數組的位置:腓 - 重置初始ID多維數組
Array
(
[1] => Array
(
[id] => 1
[items] => Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 2
)
[2] => Array
(
[id] => 3
)
)
)
[2] => Array
(
[id] => 2
[items] => Array
(
[0] => Array
(
[id] => 4
)
[1] => Array
(
[id] => 5
)
)
)
)
問題:我如何重置的項目時,它進入第二陣列id
? 我一直在試圖擠我的頭爲這個算法,但找不到方法:(
這是我如何獲得數組,如果它有幫助的源代碼我簡化了數組爲清楚起見,下面的代碼是擴展的一個:
$results = array();
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
$i++;
$results[] = array(
$row['id'] => array(
'category' => $row['category'],
'items' => array(
array(
'id' => $i, //THIS IS THE PROBLEM
'name' => $row['name'],
'user_name' => $row['user_name'],
'price' => $row['price'],
'item_photo' => $row['item_photo'],
'item_description' => $row['item_description']
)
)
)
);
}
// Begin rebuilding trees
$output = array();
//$results is array from mysql
foreach ($results as $data) {
//var_dump($data);
//dumping each block of array
foreach ($data as $categoryId => $item) {
//check if NOT yet set
if (!isset($output[$categoryId])) {
//insert values in the first Array()
$output[$categoryId] = array(
'id' => $categoryId,
'category' => $item['category'],
'items' => array()
);
}
//populate 'items' array with stuff
$output[$categoryId]['items'] =
array_merge(
$output[$categoryId]['items'],
$item['items']
);
}
}
如果有的話,請讓我知道
我的意思是,我想在第一個數組之後將鍵[[id]'重置爲1。該鍵在'[items]'數組中。 – AimanB