2017-04-25 16 views
-3

サブカテゴリの名前とその特定のサブカテゴリに関連する製品を配列に格納したいと考えています。予想通り別の配列内で配列をプッシュする方法

以下

私のコードは、

f ($sub_category->num_rows > 1) { 
    while ($row = mysqli_fetch_array($sub_category)) { 
    $subcategory_id = $row['sub_cat_id']; 
    $subcategory_name['sub_category_name'][] = $row['sub_cat_name'];    
    $sql_subrec = "SELECT es_product_description.name AS prod_name FROM es_product_to_category LEFT JOIN es_product_description ON es_product_description.product_id=es_product_to_category.product_id LEFT JOIN es_product ON es_product_description.product_id = es_product.product_id WHERE es_product_to_category.category_id = $subcategory_id AND es_product.status=1"; 
    //echo "<pre>";print_r($sql_subrec); 
    $sub_product = $conn->query($sql_subrec); 
    while ($prow = mysqli_fetch_assoc($sub_product)) { 
     $subcategory_name['sub_category_name']['products_name'][] = $prow['prod_name']; 
    } 
    } 

    //$subcategory_name[] = array('product_data' => $subcategory_name,); 
    //echo "<pre>";print_r($subcategory_name); 
    echo "<pre>";print_r($subcategory_name);  
} 

結果が来ていません。

関連するサブカテゴリ配列内の製品を取得したいのですが、最初の配列にすべてが入っています。

Array 
(
    [sub_category_name] => Array 
     (
      [0] => Weighing Scale 
      [products_name] => Array 
       (
        [0] => BS-250 Baby Scale 
        [1] => DS-415N Bench Scale 
        [2] => DS-415N Platform Scale 
        [3] => DS-65 Counter Scale 
        [4] => DS-852 Weighing Scale 
        [5] => DS-252 Weighing Scale 
        [6] => DS-215N Platform Scale 
        [7] => DS-75 Counter Scale 
        [8] => DS-415 Smile 
        [9] => DS-215SS Platform Scale 
        [10] => DS-450 Table Top Weighing Scale 
        [11] => DS-450SS Bench Scale 
        [12] => DS-451HP Weighing Scale 
        [13] => DS-215 Hanging Scale 
        [14] => DS-215N Trolley Scale 
        [15] => DS-673 Weighing Scale 
        [16] => DS-315 Series Crane Scale 
        [17] => DS-515 Weighing Scale 
        [18] => DS-450CW Check Weigher Scale 
        [19] => DC-810 Counting, Barcode Printing Scale 
        [20] => DC-85 Counting Scale 
        [21] => DS-252C Counting Scale 
        [22] => DC-810 Counting, Barcode Printing Scale 
        [23] => SI-810 Label Printing Scale 
        [24] => SI-810PR Receipt Printing 
        [25] => DS-252PR Receipt Printing Scale 
        [26] => Barcode Label Printer Scales 
        [27] => Receipt Printer Scales 
        [28] => SI-810PR Barcode Label Printer Scale 
        [29] => SM - 100EV+ Barcode Label Printer Scale 
        [30] => SI-810PRSS Receipt Printer Scale 
        [31] => SI-810PR Receipt Printer Scale 
        [32] => DS-252PR Receipt Printer Scale 
        [33] => DC-810 Counting, Barcode Printing Scale 
        [34] => SI-810 Label Printing Scale 
        [35] => DS-451TW Tank/Vessel Weighing 
        [36] => DS-451 Milk Weighing 
        [37] => SI-810 System Scale Bench Type 
        [38] => SI-810 System Scale Platform Type 
        [39] => HT-HTR Series Analytical Balance 
        [40] => MX-50 Moisture Analyzer 
        [41] => AJ Series Precision Balance 
        [42] => PG/FB Precision Balance 
        [43] => DS-852G Precision Balance 
        [44] => LF-225DR Semi-micro balance 
       ) 

      [1] => Industrial Counting Scale 
      [2] => Retail Printer Scale 
      [3] => Weighing System & Solutions 
      [4] => Weighing Balance 
     ) 

) 
+0

あなたのコメントにあなたのものが正しいようです...何が問題なのですか? –

+0

array_merge()を使用できます。 PHPで –

+0

あなたはあなたの実際の出力とあなたが達成したいものを投稿することができます...私はあなたが欲しいものについて少し混乱しています。 – Daniel

答えて

1

限り私はあなたからの理解などの限られたあなたは自分の第二whileにこのような何かをする必要があるかもしれません。説明:

$subcategory_name['sub_category_name'][$row['sub_cat_name']]['products_name'][] = $prow['prod_name']; 

これはちょうど推測です...期待される出力と実際の出力を投稿してください。

1

これを試してください。 「サブカテゴリ名」&「の製品名は」単一の下に来る「サブカテゴリーID」キーインデックス

if ($sub_category->num_rows > 1){ 
     while ($row = mysqli_fetch_array($sub_category)){ 
      $subcategory_id = $row['sub_cat_id']; 
      $subcategory_name[$subcategory_id]['sub_category_name'][] = $row['sub_cat_name']; 
      $sql_subrec = "SELECT es_product_description.name AS prod_name FROM es_product_to_category LEFT JOIN es_product_description ON es_product_description.product_id=es_product_to_category.product_id LEFT JOIN es_product ON es_product_description.product_id = es_product.product_id WHERE es_product_to_category.category_id = $subcategory_id AND es_product.status=1"; 
      //echo "<pre>";print_r($sql_subrec); 
      $sub_product = $conn->query($sql_subrec); 
      while ($prow = mysqli_fetch_assoc($sub_product)){ 
        $subcategory_name[$subcategory_id]['products_name'][] = $prow['prod_name']; 
      } 
     } 
     echo "<pre>";print_r($subcategory_name);   
    } 
+0

Thx Danielとashokが必要な出力を得ました。カテゴリ名が必要なので、danielsコードを使用しました。 –

関連する問題