2016-07-02 4 views
0

이러한 결과를 각 키 이름에 대해 하나의 레코드로 데이터베이스에 삽입하려면 어떻게해야합니까? 이 두 배열은 항상 같은 수의 레코드와 키 이름을 갖습니다.동일한 키를 가진 다중 배열을 통한 PHP 루프

[size_chart_data] =Array (
    [Width] =Array (
     [Small] =18 
     [Medium] =20 
     [Large] =22 
     [X-Large] =24 
     [2X-Large] =26 
     [3X-Large] =28 
     [4X-Large] =30 
     [5X-Large] =32 
    ) 
    [Height] =Array (
     [Small] =28 
     [Medium] =29 
     [Large] =30 
     [X-Large] =31 
     [2X-Large] =32 
     [3X-Large] =33 
     [4X-Large] =34 
     [5X-Large] =35 
    ) 
) 

나는 데이터베이스에 삽입 할 다음과 같은 SQL을 사용하고 있습니다 :

$chartData = db_insert('pa_size_chart_data') 
    ->fields(array(
     'width', 
     'height', 
    )); 

이 시간 내 주셔서 너무 감사드립니다. 설명이 필요할 경우 알려 주시기 바랍니다. 아론

+0

'size_id'란 무엇입니까? –

+0

size_id는이 질문에 중요하지 않으며 질문을 수정했습니다. 혼란을 드려 죄송합니다. –

답변

0

이 답이다. 아주 특별한 @u_mulder와 @blackandorangecat에 감사드립니다. 질문에서 너비와 높이 만 포함했습니다. 그러나 3 단계를 포함하면 다른 사람들에게 도움이 될 수 있다고 생각했습니다. 그래서 대답은 너비, 높이, & 슬리브를 포함합니다.

$size_chart_data = array(); 
    foreach ($size_chart_data['Width'] as $key => $value) { 
     // floatval() is changing string results to float 
     $value = floatval($value); 

     if (!empty($size_chart_data['Height']) && !empty($size_chart_data['Sleeve'])) { 
     // floatval() is changing string results to float 
     $size_chart_data['Height'][$key] = floatval($size_chart_data['Height'][$key]); 
     $size_chart_data['Sleeve'][$key] = floatval($size_chart_data['Sleeve'][$key]); 
     $db_data = array(
      'width' => $value,        // this is width 
      'height' => $size_chart_data['Height'][$key], // this is height 
      'sleeve' => $size_chart_data['Sleeve'][$key], // this is the sleeve 
     ); 
     } elseif (!empty($size_chart_data['Height'])) { 
     $size_chart_data['Height'][$key] = floatval($size_chart_data['Height'][$key]); 
     $db_data = array(
      'width' => $value,        // this is width 
      'height' => $size_chart_data['Height'][$key], // this is height 
      'sleeve' => null,        // this is the sleeve 
     ); 
     } elseif (!empty($size_chart_data['Width'])){ 
     $db_data = array(
      'width' => $value, // this is width 
      'height' => null, // this is height 
      'sleeve' => null, // this is the sleeve 
     ); 
     }    
     $sizeArr = array($db_data); 
     $chartData = db_insert('pa_size_chart_data') 
      ->fields(array(
      'width', 
      'height', 
      'sleeve', 
     )); 
     foreach ($sizeArr as $sizeRec) { 
      $chartData->values($sizeRec); 
     } 
     $chartData->execute(); 
    } 
0

나는이 작업을 수행해야한다고 가정

$size_chart_data = array(); 
foreach ($size_chart_data['Width'] as $key => $value) { 
    $db_data = array(); 
    if (!empty($size_chart_data['Height'][$key])) { 
     $db_data = array(
      'size_id' => $key,        // this is size_id 
      'width' => $value,        // this is width 
      'height' => $size_chart_data['Height'][$key], // this is height 
     ); 
     // check what you've got 
     print_r($db_data); 
     // add $db_data to database, I suppose it's like 
     $chartData = db_insert('pa_size_chart_data') 
      ->fields($db_data); 
    } 
} 
+0

U_Mulder 감사합니다! 데이터를 사용할 수있는 순서로 넣습니다. 어떤 이유로 데이터가 데이터베이스에 삽입되지 않습니다. 좀 더 조사를하고 잘하면 그것을 알아낼 수있을거야. –

+0

아마도 다차원 배열이기 때문에 사용할 수 없습니까? @aaronb – blackandorangecat

+0

'$ singleArray = call_user_func_array ('array_merge', $ multiArray);'@aaronb 이것은 배열을 평평하게합니다. 당신은 db에 삽입하기 전에 이것을 시도 할 수 있습니다 – blackandorangecat

관련 문제