数组

时间:2016-04-27 14:06:08

标签: php arrays xml soap

我需要SOAP请求看起来像这样:

<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:sgd="http://schemas.datacontract.org/2004/07/SGDDWebServiceContracts.Messages" xmlns:sgd9="http://schemas.datacontract.org/2004/07/SGDDWebServiceContracts.DataTypes.ProductStock">
<x:Header/>
<x:Body>
    <tem:RequestProductStock>
        <tem:criteria>
            <sgd:Values>
                <sgd9:ProductStockCriteria>
                    <sgd9:ProductNumber>0123456789-1</sgd9:ProductNumber>
                    <sgd9:SecurityKeyAPI>MY-SECRET-KEY</sgd9:SecurityKeyAPI>
                    <sgd9:SellerCompany>static</sgd9:SellerCompany>
                </sgd9:ProductStockCriteria>
                <sgd9:ProductStockCriteria>
                    <sgd9:ProductNumber>0123456789-2</sgd9:ProductNumber>
                    <sgd9:SecurityKeyAPI>MY-SECRET-KEY</sgd9:SecurityKeyAPI>
                    <sgd9:SellerCompany>static</sgd9:SellerCompany>
                </sgd9:ProductStockCriteria>
                <sgd9:ProductStockCriteria>
                    <sgd9:ProductNumber>0123456789-3</sgd9:ProductNumber>
                    <sgd9:SecurityKeyAPI>MY-SECRET-KEY</sgd9:SecurityKeyAPI>
                    <sgd9:SellerCompany>static</sgd9:SellerCompany>
                </sgd9:ProductStockCriteria>
            </sgd:Values>
        </tem:criteria>
    </tem:RequestProductStock>
</x:Body>

ProductNumber需要与foreachloop一起插入,rest是静态的。

我需要像这样在多重阵列中运行foreachloop:

$productSkus = array("0123456789-1","0123456789-2","0123456789-3","0123456789-4","0123456789-5","0123456789-6","0123456789-7","0123456789-8","0123456789-9","0123456789-10");

    $params = array(
        'criteria' => array(
            'Values' => array(
                'ProductStockCriteria' => array(
                    'ProductNumber' => FOREACHLOOP HERE, 
                    'SecurityKeyAPI' => 'MY-SECRET-KEY',
                    'SellerCompany' => 'static'
                )
            )
        )
    );

到目前为止,我有这个,但它没有用。以为我在这里丢失了线索。:

    $params = array(
        'criteria' => array(
            'Values' => array()
            )
        )
    );

    foreach (productSkus as $sku) {
        $params['criteria']['Values'][] = array(
            'ProductStockCriteria' => array(
                'ProductNumber' => $sku, 
                'SecurityKeyAPI' => 'MY-SECRET-KEY',
                'SellerCompany' => 'static'
            )
        );
    }

任何人都可以看到我做错了什么?我需要构建静态数组firstm然后插入动态值与foreach迭代正确吗?

1 个答案:

答案 0 :(得分:0)

好的,所以我发现了自己。

这是我的解决方案。需要分为两部分构建阵列。

1)制作静态阵列(父母)

$params = array(
    'criteria' => array(
        'Values' => array(
            'ProductStockCriteria' => array()
        )
    )
);      

2)用外表(儿童)建立动态零件

$productSkus = array("0123456789-1","0123456789-2","0123456789-3","0123456789-4","0123456789-5","0123456789-6","0123456789-7","0123456789-8","0123456789-9","0123456789-10");

foreach ($productSkus as $sku ) {
    $params['criteria']['Values']['ProductStockCriteria'][] = array(
        'ProductNumber' => $sku, 
        'SecurityKeyAPI' => 'MY-API-KEY',
        'SellerCompany' => 'static'
    );
}

这将创建一个这样的SOAP请求:

<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:sgd="http://schemas.datacontract.org/2004/07/SGDDWebServiceContracts.Messages" xmlns:sgd9="http://schemas.datacontract.org/2004/07/SGDDWebServiceContracts.DataTypes.ProductStock">
<x:Header/>
<x:Body>
<tem:RequestProductStock>
    <tem:criteria>
        <sgd:Values>
            <sgd9:ProductStockCriteria>
                <sgd9:ProductNumber>0123456789-1</sgd9:ProductNumber>
                <sgd9:SecurityKeyAPI>MY-SECRET-KEY</sgd9:SecurityKeyAPI>
                <sgd9:SellerCompany>static</sgd9:SellerCompany>
            </sgd9:ProductStockCriteria>
            <sgd9:ProductStockCriteria>
                <sgd9:ProductNumber>0123456789-2</sgd9:ProductNumber>
                <sgd9:SecurityKeyAPI>MY-SECRET-KEY</sgd9:SecurityKeyAPI>
                <sgd9:SellerCompany>static</sgd9:SellerCompany>
            </sgd9:ProductStockCriteria>
            <sgd9:ProductStockCriteria>
                <sgd9:ProductNumber>0123456789-3</sgd9:ProductNumber>
                <sgd9:SecurityKeyAPI>MY-SECRET-KEY</sgd9:SecurityKeyAPI>
                <sgd9:SellerCompany>static</sgd9:SellerCompany>
            </sgd9:ProductStockCriteria>
        </sgd:Values>
    </tem:criteria>
</tem:RequestProductStock>