php警告:为foreach()提供的参数无效

时间:2015-05-27 11:05:45

标签: php arrays json warnings

  

警告:为foreach()提供的参数无效

我真的不明白,也找不到原因:(

我在此行发出警告

foreach($formResponse as $obj){
    $formResponse = array();
    $formResponse = '[
        {"name": "Ds_Merchant_Amount", "value": '.$data['Amount'].'},
        {"name": "Ds_Merchant_Order", "value": '.$data['Id'].'}, 
        {"name": "Ds_Merchant_MerchantCode", "value": '.$data['Fuc'].'},
        {"name": "Ds_Merchant_Terminal", "value": '.$data['Terminal'].'},
        {"name": "Ds_Merchant_TransactionType", "value": '.$data['Trans'].'},
        {"name": "Ds_Merchant_Titular", "value": '.$data['Titular'].'},
        {"name": "Ds_Merchant_MerchantName", "value": '.$data['Nombre'].'},
        {"name": "Ds_Merchant_MerchantData", "value": '.sha1($data['Notify_url']).'},
        {"name": "Ds_Merchant_MerchantURL", "value": '.$data['Notify_url'].'},
        {"name": "Ds_Merchant_ProductDescription", "value": '.$data['Productos'].'},
        {"name": "Ds_Merchant_UrlOK", "value": '.$data['Notify_url'].'},
        {"name": "Ds_Merchant_UrlKO", "value": '.$data['Notify_url'].'},
        {"name": "Ds_Merchant_MerchantSignature", "value": '.$data['Firmafinal'].'},
        {"name": "Ds_Merchant_ConsumerLanguage", "value": '.$data['Idiomas'].'},
        {"name": "Ds_Merchant_PayMethods", "value": '.$data['Tipopago'].'}
    ]';             

    $formResult = "<form action=". $data['action']." method='post'>'";

    foreach($formResponse as $obj){
        $inputName = $obj -> name; 
        $inputValue = $obj -> value; 

        $formResult .= "<input type='hidden' name='". $inputName ."' value='". $inputValue ."'></input>";
    }

2 个答案:

答案 0 :(得分:0)

使用

$formResponse=json_decode($formResponse);

在foreach循环开始之前

答案 1 :(得分:0)

首先你必须在使用foreach循环之前解码json数据,因为它只接受你必须使用的数组和对象$formResponse=json_decode($formResponse, true);