1

While parsing a CSV file(I have used a Class found on Google), I ran into a problem. Here is an example of an array made from a .csv file(print_r):

  Array
    (
        [0] => Array
            (
                [Site] => ViralNova
                [Impressions] => 104719
                [CTR] => 0.30%
                [Clicks] => 311
                [Average CPC] => $ 0.400
                [CPM] => $ 1.19
                [Conversion Rate] => 1.29%
                [Actions] => 4
                [CPA] => $ 31.100
                [Spent] => $ 124.40
            )

        [1] => Array
            (
                [Site] => TMZ - Desktop
                [Impressions] => 103276
                [CTR] => 0.29%
                [Clicks] => 295
                [Average CPC] => $ 0.400
                [CPM] => $ 1.14
                [Conversion Rate] => 0.68%
                [Actions] => 2
                [CPA] => $ 59.000
                [Spent] => $ 118.00
            )
)

The Problem is that I cannot use the "Site" index. Whenever I try to, I get this notice:

<b>Notice</b>:  Undefined index: Site in <b>C:\xampp\htdocs\panel\update\assets\php\core\core-ajax.php</b> on line <b>104</b><br />

Line 104 being:

print $data[0]['Site'];

Also,

print $data[0]['Impressions'];

or

print_r($data[0]);

works without any problem and I get the correct value/array.

Any ideas?

Thanks!!

7
  • Do: var_dump($data) and look at the output in the source code! <- Show that Commented Apr 29, 2015 at 14:22
  • paste.ofcode.org/kt6iAzCZM6ymbFkDsaARfp Commented Apr 29, 2015 at 14:24
  • 2
    You have an extra space and therefore your index is ' Site' not 'Site'. Do echo $data[0][' Site'] and see if it works. Commented Apr 29, 2015 at 14:25
  • It is not working kidA. Undefined Index: Site Commented Apr 29, 2015 at 14:28
  • It should work if you use [' Site'] instead of ['Site']. Tested it on my local and worked fine. Commented Apr 29, 2015 at 14:36

1 Answer 1

1

Acc. to me your code is running fine. this problem is occur when in loop in a point your index is missing.

to resolve this issue..

use this..

<?php 
if($data[0]['Site']){
    print $data[0]['Site'];
}

// Also

if($data[0]['Impressions']){
   print print $data[0]['Impressions'];
}

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

1 Comment

Surely you meant if (isset($data[0]['Site']))?

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.