1

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?

2
  • 2
    It gives you undefined index when foreach reaches $results['TotalRows']. Commented Jun 21, 2014 at 7:28
  • ^ this is probably correct Commented Jun 21, 2014 at 7:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i tried this but now it gives me an error Invalid argument supplied for foreach()
@AllwinLopes Ya sorry, looking closer and my answer is not correct. Assuming that your data is from var_dump($results), then I think you have it right in your question. Not sure why it wouldn't be working the way you have it. Using your original foreach, add var_dump($licenseElement) to see what indices are actually there.
actually that data is from var_dump($licenseElement) and now when i give index 1 to $results array it works. foreach ($results[1] as $licenseElement). Thank you
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.