php - 重置多维数组中的初始 id


Php - Resetting initial id in a multidimensional array

我在这里得到了一个数组:

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']
        );
    }
}

如果有的话,请告诉我。

假设您的结果已排序,因此每个类别的项目都是连续的(一个类别不会显示在结果的不同位置):

$oldCat = "";
while ($row = mysql_fetch_assoc($result)) {
// If the current category is different than the previous, reset $i
if ($row['category'] != $oldCat)
    $i == 0;
// Set $oldCat to the current categery (used for next loop's comparison)
$oldCat = $row['category'];
$i++;
$results[] = array(
       [ ... ]
)
}

此外,您可以将设置$i设置为单行:

// If the current category is different than the previous, reset $i
$i = $row['category'] != $oldCat ? 0 : $i++;

为了清楚起见,请注意,您可能希望制作更具描述性的数组键。例如,您必须id两次 - 这可能是category_iditem_id

首先,也是最重要的,请不要使用 mysql_* API,因为这些函数现已弃用。相反,请使用MySQLi或PDO(就个人而言,我会使用PDO,因为它支持各种数据库连接,但这只是我的意见)

要解决id问题,您需要做的就是将以前的 id 存储在变量中并对其进行测试。以下内容可能不正确,因为您的代码非常混乱,但它应该足以让您走上正确的轨道:

$i = 0;
while($row = mysql_fetch_assoc($result)){
    // Only increment i if it is still the same row
    // otherwise reset it
    if($row_id==$row['id']){
        $i++;
    }else{
        $i = 0;
    }
    // Set the new row id
    $row_id = $row['id'];
    // Do your stuff...
}