从变量PHP阵列设置

问题描述:

我有一个项目列表,我想通过一个数组提供给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)))); 

如果我运行这个在命令行中,我得到

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

现在,如果我通过将变量的内容改变的print_r线直接在这样的电话中

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://*.com/q/7933982/367456 – hakre

$ skuList看起来像一个数组,但仍然是一个字符串。 你有foreach循环后,要做到这一点:

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

或者,更好,使skuList一个数组,因为beginnig:

$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

设置它作为一个数组开始做的伎俩。谢谢! –