2011-11-02 3 views
0

배열을 통해 API에 공급할 항목 목록이 있지만 별도의 개체로 작성됩니다. 개체에있는 항목을 반복하여 배열에 공급할 수있는 변수를 구성 할 수 있다고 생각했지만 배열이 끊어졌습니다. 설명하는 것보다보기가 쉽습니다.변수에서 PHP 배열 설정

내가 사용하고있는 코드는 이것이다 :

//Set up the parser object                
$parser = new XMLParser($xml);                  
$parser->Parse(); 

$skuList = ''; 
// Pull the inventory of the requested SKUs from Magento for comparison later   
foreach($parser->document->product as $product) 
{ 
    $skuList .= "'" . $product->sku[0]->tagData . "',"; 
} 
echo $skuList; 
print_r($proxy->call($sessionId, 'product_stock.list', array(array($skuList)))); 

내가 명령 줄에서이 작업을 실행하면 내가 변수의 내용을 바꾸어 인 print_r 라인을 변경하면 나는 지금

'1DAFPOT5','8GAIL','26BULK30',Array 
(
) 

를 얻을 수 이 같은 전화에 직접

print_r($proxy->call($sessionId, 'product_stock.list', array(array('1DAFPOT5','8GAIL','26BULK30',)))); 

나는 어떤 결과를 얻었 는가?

'1DAFPOT5','8GAIL','26BULK30',Array 
(
[0] => Array 
    (
     [product_id] => 2154 
     [sku] => 26BULK30 
     [qty] => 19.0000 
     [is_in_stock] => 1 
    ) 

[1] => Array 
    (
     [product_id] => 2255 
     [sku] => 8GAIL 
     [qty] => 16.0000 
     [is_in_stock] => 1 
    ) 

[2] => Array 
    (
     [product_id] => 2270 
     [sku] => 1DAFPOT5 
     [qty] => 23.0000 
     [is_in_stock] => 1 
    ) 

) 

변수를 잘못 구성했거나 배열에 다르게 공급해야합니까?

+0

을, 어쩌면 도움이 : http://stackoverflow.com/q/7933982/367456 – hakre

답변

1

$ skuList는 배열처럼 보이지만 여전히 문자열입니다. 당신은 foreach 루프 후이 작업을 수행해야한다 :

$skuList = explode(',',$skulist); 

또는 더 나은, beginnig 이후 skuList을 배열합니다 비슷한 질문 (그러나 더 복잡한)

$skuList = array(); 
foreach($parser->document->product as $product) 
{ 
    $skuList[] = $product->sku[0]->tagData; 
} 
print_r($proxy->call($sessionId, 'product_stock.list', array($skuList))); 

http://www.php.net/manual/en/function.explode.php

+0

배열을 시작으로 트릭을 설정합니다. 감사! –