2013-10-22 35 views
-1

我得到了一個數組的位置:腓 - 重置初始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'] 
     ); 
    } 
} 

如果有的話,請讓我知道

+0

我的意思是,我想在第一個數組之後將鍵[[id]'重置爲1。該鍵在'[items]'數組中。 – AimanB

回答

1

假設你的結果進行排序,以便每個類別的項目順序(一類不顯示達在你的結果不同的地方):

$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

+0

謝謝,我會在稍後介紹關鍵。較長的解決方案有效。但是,即使將$ oldCat放在行之前或之後,您的單行解決方案也會將所有ID都返回爲0。反正沒關係。 – AimanB

+1

'$ oldCat = $ row ['category'];'應該在一行之後立即執行。奇怪,它沒有工作..好吧,很高興爲您解決更長的解決方案! – DACrosby

1

首先,也是最重要的,請在使用mysql_* API爲這些功能現在已廢棄不要。相反,請使用MySQLiPDO(個人而言,我會使用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... 
} 
+0

謝謝你的工作。無論如何,我打算稍後遷移到'mysqli *'。感謝您的高舉。 – AimanB