0

我刚刚开始学习代码并正在编写一些代码来回显一个数组,它给了我这两个错误:

警告:array_keys() 期望参数 1 为数组,在第 70 行的 C:\xampp\htdocs\ogmt\rest_server_api.php 中给出 null

警告:在第 71 行的 C:\xampp\htdocs\ogmt\rest_server_api.php 中为 foreach() 提供的参数无效。

我该如何解决?这是我的代码:

    if($Result1){
      // script to get business no, amount & merchant id,output to merchant page
      $query="SELECT * FROM customer_order WHERE order_time=(SELECT max(order_time)from customer_order)";
      $result=mysql_query($query);
      while($row=mysql_fetch_assoc($result)){
          $amount=$row['amount'];
          $id=$row['merchant_id'];
          $payment_mode=$row['mobile_service'];
          switch($payment_mode){               
           case 'TIGO-PESA':
            $result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='TIGO-PESA'");
            while($row1=mysql_fetch_assoc($result1)){ 
                 $data=array(
                   'Business no'=>$row1['business_no'],
                   'Payment Mode'=>$payment_mode,
                   'Total Amount Tsh'=>$amount,
                   'Merchant ID'=>$id
                   );
                   }break;
                        
            case 'M-PESA':
            $result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='M-PESA'");
            while($row1=mysql_fetch_assoc($result1)){ 
                 $data=array(
                   'Business no'=>$row1['business_no'],
                   'Payment Mode'=>$payment_mode,
                   'Total Amount Tsh'=>$amount,
                   'Merchant ID'=>$id
                   );
                   }break;
            case 'AIRTEL-MONEY':
            $result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='AIRTEL-MONEY'");
            while($row1=mysql_fetch_assoc($result1)){ 
                 $data=array(
                   'Business no'=>$row1['business_no'],
                   'Payment Mode'=>$payment_mode,
                   'Total Amount Tsh'=>$amount,
                   'Merchant ID'=>$id
                    );
                   }break;
                        
           // default:
           $data=array('error'=>"no payment mode selected");        
        }
           $response=array('details'=>$data);
        }
    }
     
     else{
          $response=array('details'=>"query failed!");
          }
         
      }
 //print output to merchant page to complete payment
 $keys = array_keys($data);
  foreach($response['details'] as $keys=>$value){
       echo "$keys: $value</br>";
}
?>
4

4 回答 4

1

包装这段代码

<?php 
if(isset($response['details']) && is_array($response['details'])){
   foreach($response['details'] as $keys=>$value){
       echo "$keys: $value</br>";
   }
}else{
   echo "Array is empty";
}
?>
于 2013-08-16T11:00:38.783 回答
1

你不能回显一个数组,你可以尝试var_dump($data);或者print_r($data)

您也可以使用implode("", $_POST[$data]);更接近回声的 。

于 2013-08-16T11:05:50.257 回答
0

如果您发现它们是相当大的阵列,请尝试这些工具.. http://krumo.sourceforge.net/

加载后,您可以在 PHP 代码中执行类似这样的简单调用。从站点下载脚本并将其放入您的站点并使用 require() 函数调用它

require 'class.krumo.php';
krumo($your_array); // will output your array :)
于 2013-08-16T10:58:16.757 回答
0

只需使用 print_r 函数即可查看内容。[顺便说一句,你有一个额外的支架(去掉它)]

    <?php
if($Result1){
    // script to get business no, amount & merchant id,output to merchant page
    $query="SELECT * FROM customer_order WHERE order_time=(SELECT max(order_time)from customer_order)";
    $result=mysql_query($query) or die("MySQL ERROR: ".mysql_error());;
    while($row=mysql_fetch_assoc($result)){
        $amount=$row['amount'];
        $id=$row['merchant_id'];
        $payment_mode=$row['mobile_service'];
        switch($payment_mode){
            case 'TIGO-PESA':
                $result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='TIGO-PESA'");
                while($row1=mysql_fetch_assoc($result1)){
                    $data=array(
                        'Business no'=>$row1['business_no'],
                        'Payment Mode'=>$payment_mode,
                        'Total Amount Tsh'=>$amount,
                        'Merchant ID'=>$id
                    );
                }
                break;

            case 'M-PESA':
                $result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='M-PESA'");
                while($row1=mysql_fetch_assoc($result1)){
                    $data=array(
                        'Business no'=>$row1['business_no'],
                        'Payment Mode'=>$payment_mode,
                        'Total Amount Tsh'=>$amount,
                        'Merchant ID'=>$id
                    );
                }break;
            case 'AIRTEL-MONEY':
                $result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='AIRTEL-MONEY'");
                while($row1=mysql_fetch_assoc($result1)){
                    $data=array(
                        'Business no'=>$row1['business_no'],
                        'Payment Mode'=>$payment_mode,
                        'Total Amount Tsh'=>$amount,
                        'Merchant ID'=>$id
                    );
                }break;

             default:
                 $data=array('error'=>"no payment mode selected");
                break;

        }
        $response=array('details'=>$data);
    }
}

else{
    $response=array('details'=>"query failed!");

}
//print output to merchant page to complete payment
print_r($data);
?>
于 2013-08-16T11:23:41.130 回答