数组中未定义的索引错误

时间:2014-06-21 07:26:14

标签: php arrays

的var_dump:

array (size=2)
0 => 
array (size=16)
  'campaignID' => string '416' (length=3)
  'trackerID' => string '401' (length=3)
  'bannerID' => string '9901193' (length=7)
  'CookieID' => string '7ed9340d8f971e1a396afc3e01fb3ab7' (length=32)
  'affiliateid' => int 1782
  'ConversionConnectionID' => string '6712988' (length=7)
  'conversionTime' => string '2014-06-14 09:09:18' (length=19)
  'conversionStatus' => string 'Pending' (length=7)
  'userIp' => string '27.0.57.16' (length=10)
  'actionkey' => string 'gte500' (length=6)
  'window' => int 20410
  'variables' => 
    array (size=9)
      'cartvalue' => string '752.53' (length=6)
      'clickid' => string '' (length=0)
      'conversionid' => string '' (length=0)
      'event' => string '' (length=0)
      'leadid' => string '' (length=0)
      'optionaladver' => string 'producttype,saleamount,modeofpayment' (length=36)
      'quantity' => string '' (length=0)
      'timestamp' => string '1970-01-01 00:00:00' (length=19)
      'transactionid' => string '1107063NJP517A934' (length=17)
  'sub_conversion' => int 0
  'updated_date' => string '0000-00-00 00:00:00' (length=19)
  'sum_affiliate_commission' => float 125
  'type' => string 'Sale' (length=4)
'TotalRows' => int 1

我将使用以下代码打印此数组中的数据:

<?php foreach ($results as $licenseElement) :?>
<tr>
  <td><?php echo $licenseElement['campaignID']; ?></td>
  <td><?php echo $licenseElement['trackerID']; ?></td>
  <td><?php echo $licenseElement['conversionTime']; ?></td>
  <td><?php echo $licenseElement['userIp']; ?></td>
</tr>
<?php endforeach; ?>

但它为我提供了所有列的未定义索引:错误。

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:2)

您正在处理多维数组,而您的foreach正在循环第一维 - 它不包含指定的索引。试试这个:

foreach ($results[0] as $licenseElement) { ...

您需要遍历存储为数组$results中第一个元素的数组,而不是$results本身。因此$results[0]包含您要循环的数组,包含所有正确的索引。

答案 1 :(得分:0)

尝试:

<?php
$x = 0;
while (isset($results[$x])):
    $licenseElement = $results[$x];
?>
<tr>
  <td><?php echo $licenseElement['campaignID']; ?></td>
  <td><?php echo $licenseElement['trackerID']; ?></td>
  <td><?php echo $licenseElement['conversionTime']; ?></td>
  <td><?php echo $licenseElement['userIp']; ?></td>
</tr>
<?php
    $x++;
    endwhile;
?>