2013-10-09 3 views
1

json 형식의 구성을 가진 구성 파일을 읽는 cron 작업을 개발 중이고 mysql tanble에 데이터를 삽입해야하는 csv 파일이 있습니다.
구성 파일을 읽는 중 첫 번째 부분을 완료하고 CSV 파일을 읽는 중
이제 구성 파일에있는 mysql 열을 csv에서 검색해야합니다. 그러면 csv에서 일치하는 열이 발견되면 해당 열의 값을 mysql 테이블에 저장한다.
나는이 부분에 붙어있어 이것을 끝내기 위해 도움이 필요하다.
이것은 샘플 구성 파일입니다. 나는 csv에서 소스를 검색해야하고 csv의 디터 네이 션 열에 넣어야합니다.csv 열을 검색하고 mysql에 삽입

"matchingField": [ 
      { 
       "source": "ProductLongDescription", 
       "detination": "Description" 
      }, 
      { 
       "source": "ExtraTextOne", 
       "detination": "EventDate" 
      }, 
      { 
       "source": "ExtraTextTwo", 
       "detination": "EventTime" 
      }, 
      { 
       "source": "ExtraTextThree", 
       "detination": "LocationInfo" 
      }, 
      { 
       "source": "Zupid", 
       "detination": "Unique_ID" 
      }, 
      { 
       "source": "ProductName", 
       "detination": "Title" 
      }, 
      { 
       "source": "ProductPrice", 
       "detination": "Price" 
      }, 
      { 
       "source": "MerchantProductCategory", 
       "detination": "Category" 
      }, 
      { 
       "source": "ImageMediumURL", 
       "detination": "ExternalImageUrl" 
      }, 
      { 
       "source": "ProductManufacturerBrand", 
       "detination": "LocationName" 
      }, 
      { 
       "source": "ZanoxProductLink", 
       "detination": "RedirectLink" 
      }, 
      { 
       "source": "TermsOfContract", 
       "detination": "sittingType" 
      } 
     ] 
+0

전체 csv 파일을 배열에 저장 한 다음 배열을 검색 할 수 있습니까? – joe42

+0

이렇게하려면 LESS와 같은 것을 사용 해본 적이 있습니까? http://lesscss.org/ – Machavity

+0

도움 코드를 보내 주시겠습니까? – Shani

답변

0

해결책 : 먼저 원본 열과 대상 열에 대해 두 개의 배열을 만든 다음 csv 머리글 열에 대한 배열을 만든 다음 원본 배열에서 머리글 열 값을 검색하고 일치하는 항목이 있으면 csv의 대상 열 이름과 해당 값을 포함하는 쿼리를 만듭니다 . 난 잘 코드를 설명하지 않을 수 있습니다 그러나 이것은 내 문제를 해결합니다.

while (($data = fgetcsv($handle, 10000, $columnDelimiter)) !== FALSE) 
{ 
    $number_of_fields = count($data); 
    if ($current_row == 1) 
    { 
    //Header line 
     for ($c=0; $c < $number_of_fields; $c++) 
     { 
      $header_array[$c] = $data[$c]; 
     } 
    } 
    else 
    { 
    //Data line 
     $sql_str = ''; 
     for ($c=0; $c < $number_of_fields; $c++) 
     { 
      if($key = array_search($header_array[$c],$source_arr)){ 
       $sql_str .= $dest_arr[$key]." = '".$data[$c]."',"; 
      } 
     } 
     $sql_str = "INSERT INTO $destinationTable SET ".trim($sql_str,','); 
     mysql_query($sql_str); 
    } 
    $current_row++; 
}