我在这里有一个数组:
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']
);
}
}
如果有什么请告诉我。