Allwin Lopes
Allwin Lopes

Reputation: 29

Undifined index error in array

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

I am going to print the data from this array using following code:

<?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; ?>

But it gives me an error of Undefined index: for all columns.

What's wrong with my code?

Upvotes: 1

Views: 59

Answers (2)

albertdiones
albertdiones

Reputation: 733

Try:

<?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;
?>

Upvotes: 0

Mark Miller
Mark Miller

Reputation: 7447

You are dealing with a multidimensional array, and your foreach is looping through the first dimension - which does not contain the indices specified. Try this:

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

You need to loop through the array stored as the first element in the array $results, not $results itself. Thus $results[0] contains the array you want to loop through, with all the correct indices.

Upvotes: 2

Related Questions