Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组输出修改_Php_Soap - Fatal编程技术网

PHP数组输出修改

PHP数组输出修改,php,soap,Php,Soap,我有下面的SOAP调用,我正在尝试将输出转换为不同的格式。我似乎无法让它正常工作。任何帮助都将不胜感激 public function __construct($coid, $env, $app_id, $product) { $url = APP_ROOT."dev/install/wsdl/my.wsdl"; try { $soapclient = new SoapClient($url); //This overwrites the l

我有下面的SOAP调用,我正在尝试将输出转换为不同的格式。我似乎无法让它正常工作。任何帮助都将不胜感激

public function __construct($coid, $env, $app_id, $product) 
{

    $url = APP_ROOT."dev/install/wsdl/my.wsdl";

    try {
        $soapclient = new SoapClient($url);
        //This overwrites the location at the bottom of the WSDL file
        $soapclient->__setLocation('http://exampleurl'); 
        $params = array('clientOid' => $coid);
        $response =$soapclient->getClientSetup($params);

        print_r($response);
        echo "--------";

        var_dump($array);

    } catch (Exception $e) {
        echo $e->getMessage();
    }
}   
我的输出是这样的(注意这里也没有productUrl,我需要这个键来显示)

我希望它看起来像这样:

[
    {
        code: 1001,
        description: "datapoint 1",
        productUrl: "NO_URL"
    },
    {
        code: 1089,
        description: "datapoint2",
        productUrl: "http://example.com/"
    },
    {
        code: 2101,
        description: "datapoint2",
        productUrl: "NO_URL"
    }
]

对调用中的数组解码
$response
后,只需循环该数组即可格式化所需的输出

public function __construct($coid, $env, $app_id, $product) 
{

    $url = APP_ROOT . "dev/install/wsdl/my.wsdl";

    try {
        $soapclient = new SoapClient($url);
        $soapclient->__setLocation('http://exampleurl'); 
        $params = ['clientOid' => $coid];
        $response = $soapclient->getClientSetup($params);

        $array = json_decode($response, true);
        $products = [];
        foreach($array[0]['clientSetup']['selectedProducts'] as $p) {
            $p['productUrl'] = $p['productUrl'] ?? 'NO_URL';

            $products[] = $p;
        }

        echo json_encode($products, JSON_PRETTY_PRINT);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
输出:

[
    {
        "code": "1001",
        "description": "datapoint 1",
        "productUrl": "NO_URL"
    },
    {
        "code": "1049",
        "description": "datapoint 2",
        "productUrl": "NO_URL"
    },
    {
        "code": "1032",
        "description": "datapoint 3",
        "productUrl": "NO_URL"
    },
    {
        "code": "1013",
        "description": "datapoint 4",
        "productUrl": "NO_URL"
    }
]

print\r($response)的输出是什么
[
    {
        "code": "1001",
        "description": "datapoint 1",
        "productUrl": "NO_URL"
    },
    {
        "code": "1049",
        "description": "datapoint 2",
        "productUrl": "NO_URL"
    },
    {
        "code": "1032",
        "description": "datapoint 3",
        "productUrl": "NO_URL"
    },
    {
        "code": "1013",
        "description": "datapoint 4",
        "productUrl": "NO_URL"
    }
]