2013-01-10 6 views
1

XMLRPC를 사용하여 제 3 자 시스템으로 제품 정보를 전달하는 XML 구조를 만듭니다. 제품 사용자 지정 옵션의 연관 배열을 작성해야하며 각각의 경우에 값을 사용할 때 어떤 구문을 사용해야할지 모릅니다.값이 객체 인 foreach를 사용하여 연관 배열 만들기

평소처럼 믿을 수 있을지 모르겠지만 라이브 사이트에서이 작업을 수행해야하므로 디버깅 및 재생이 불가능합니다. XMLRPC는 내가 만든 객체를 직렬화 할 수 없다는 오류를 던진다. 그런 다음 XMLRPC는 내가 만든 객체를 직렬화 할 수 없다는 오류를 던진다.

이렇게 하드 코딩하면 제대로 작동합니다.

$item_array = array(
     "product_id" => new xmlrpcval($item->getSku()), 
     "name" => new xmlrpcval($item->getName()), 
     "price" => new xmlrpcval($item->getBaseCalculationPrice(), 'double'), 
     "vat_inclusive" => new xmlrpcval(0,'int'), 
     "quantity" => new xmlrpcval($item->getQty(),'int'), 
     "option_text" => new xmlrpcval(
      array(
       "option_1" => new xmlrpcval("Colour: Military Black"), 
       "option_2" => new xmlrpcval("Sizes: L") 
      ), 
      "struct")       
     ); 

foreach 루프에서 배열을 생성해야하는 folowing 섹션입니다. 몇 가지 옵션이 있을지 모릅니다.

 "option_text" => new xmlrpcval(
      array(
       "option_1" => new xmlrpcval("Colour: Military Black"), 
       "option_2" => new xmlrpcval("Sizes: L") 
      ), 
      "struct") 

내가 좋아한다면 아래의 코드는 잘 나오지만 값은 XMLRPC가 직렬화 할 수없는 객체가 아닌 문자열입니다.

 $optioncount = 1; 
     $attList = array(); 

     foreach ($attributes as $attribute => $value) { 

      $attpair = implode(": ", $value); 

      $attList['option_'. $optioncount] = 'new xmlrpcval("'.$attpair.'")'; 

      $optioncount++; 

     } 

만약 내가 var_dump($attList) 인 경우, 나는 이것이 아주 기본적인해야 알 수 있지만,의 생명을위한 내 나는이 작업을 얻을 수 없다 -

array(2) { 
    ["option_1"]=> 
    string(39) "new xmlrpcval("Colour: Military Black")" 
    ["option_2"]=> 
    string(25) "new xmlrpcval("Sizes: L")" 
} 

다른 방법은 총 엉망으로 $attList를 설정하는 것 같다. 어떤 포인터 주셔서 감사합니다.

내가 var_dump($attList)을 사용하는 경우 new xmlrpcval($attpair);을 얻을 수 있습니다. 다음

$optioncount = 1; 
    $attList = array(); 

    foreach ($attributes as $attribute => $value) { 

     $attpair = implode(": ", $value); 

     $attList['option_'. $optioncount] = new xmlrpcval($attpair); 

     $optioncount++; 

    } 

: 그리고 같이해야합니다 배열을 구축

array(2) { 
    ["option_1"]=> 
    object(xmlrpcval)#469 (3) { 
    ["me"]=> 
    array(1) { 
     ["string"]=> 
     string(22) "Colour: Military Black" 
    } 
    ["mytype"]=> 
    int(1) 
    ["_php_class"]=> 
    NULL 
    } 
    ["option_2"]=> 
    object(xmlrpcval)#433 (3) { 
    ["me"]=> 
    array(1) { 
     ["string"]=> 
     string(8) "Sizes: L" 
    } 
    ["mytype"]=> 
    int(1) 
    ["_php_class"]=> 
    NULL 
} 

}

+0

새로운 xmlrpcval'주위에 따옴표 (''에 대해 당신이 확실 $. 무조건. "))? 그것은 이상하게 보입니다 ... 단지'new xmlrpcval ($ attpair)'가되어서는 안됩니까? – tmuguet

+0

하드 코딩되지 않은 나머지 코드는 무엇입니까? – tmuguet

+0

@tmuguet -보세요. 내가 그렇게하면 $ attList가 무엇인지 알 수있는 답을 편집했습니다. – McNab

답변

2

"option_text" => new xmlrpcval(
     $attList, 
     "struct") 
관련 문제