2015-06-01 63 views
0

我嘗試使用foreach循環更新數組中的值。然後用更新的值打印數組。我在這裏發佈我的當前代碼。請回復示例代碼。感謝您。 這裏是我的數組:如何通過循環更新數組中的值

[associated_branch] => Array 
       (
        [0] => Array 
         (
          [store_id] => 0 
          [store_name] => Bazar Kolkata 
          [location] => Array 
           (
            [location] => Sonarpur 
           ) 

          [address_line1] => J L N Road 
          [address_line2] => Kolkata 
          [city] => 4 
          [state] => 24 
         ) 

        [1] => Array 
         (
          [store_id] => 16 
          [store_name] => KC DAS 
          [location] => Array 
           (
            [location] => Garia Hat 
           ) 

          [address_line1] => kolkata 
          [address_line2] => 
          [city] => 4 
          [state] => 24 
         ) 

       ) 

,我想:

[associated_branch] => Array 
       (
        [0] => Array 
         (
          [store_id] => 0 
          [store_name] => Bazar Kolkata 
          [location] => Sonarpur 
          [address_line1] => J L N Road 
          [address_line2] => Kolkata 
          [city] => 4 
          [state] => 24 
         ) 

        [1] => Array 
         (
          [store_id] => 16 
          [store_name] => KC DAS 
          [location] => Garia Hat 
          [address_line1] => kolkata 
          [address_line2] => 
          [city] => 4 
          [state] => 24 
         ) 

       ) 

我的SQL:

$assoBrSql="SELECT store_id, store_name, location, address_line1, address_line2, city, state FROM ".DB_MY_PREFIX."my_web_stores WHERE store_id IN($row[branch_ids]) AND cur_status=1 && is_more_branch =1 AND associated_branch IS NOT NULL"; 
       $branches = $db->ExecuteQuery($assoBrSql); 
       $row['associated_branch']=$branches; 

       foreach ($row['associated_branch'] as $key => $value) { 

        $locsql="SELECT location FROM " .DB_MY_PREFIX. "my_localities WHERE location_id='".$value['location']."'"; 
        $locrows=$db->ExecuteQuery($locsql); 
        $row['associated_branch'][$key]['location']=$locrows[0]; 

       } 

如何建立呢?

+0

你爲什麼不使用你的查詢連接上額外的查詢發出了在初始每條記錄的,而不是結果? –

回答

0

你應該只需要改變:

$row['associated_branch'][$key]['location']=$locrows[0]; 

$row['associated_branch'][$key]['location']=$locrows[0]['location']; 

您目前正在追加第二個查詢,這是一個包含單個元素「位置」的陣列的結果。

0

如果分支可以有多個位置,那麼您可以更改初始查詢以使用帶有groupconcat的子查詢來獲取所有位置。這樣

$assoBrSql="SELECT store_id, store_name, location, address_line1, address_line2, city, state, (SELECT GROUP_CONCAT(a.location) as locations FROM " .DB_MY_PREFIX. "my_localities a WHERE a.location_id=location) as locations FROM ".DB_MY_PREFIX."my_web_stores WHERE store_id IN($row[branch_ids]) AND cur_status=1 && is_more_branch =1 AND associated_branch IS NOT NULL"; 
       $branches = $db->ExecuteQuery($assoBrSql); 
       $row['associated_branch']=$branches; 

代碼的東西沒有測試寫在編輯器中,幸而沒有錯別字等;)