2

I have a script claiming that some of my array indices are not defined, and I just can't understand why.

Here is a minimal test case:

<?php

// Define the two-dimensional array
$lg = [
    "aapg-datapages" => [
        "lgid" => "3148425",
        "lgname" => "AAPG Datapages",
    ],
    "abstracts-of-international-conservation-literature" => [
        "lgid" => "3148426",
        "lgname" => "AATA online: Abstracts of International Conservation Literature",
    ],
    "academic-search-complete" => [
        "lgid" => "48083863",
        "lgname" => "Academic Search Complete",
    ],
];

// Dump just the first entry.
var_dump($lg['aapg-datapages']);

print "\n";

// Print the ID of the first entry.
print $lg['aapg-datapages']['lgid'];

print "\n";

// Print out all the names.
foreach($lg as $db => $properties){
    print $properties['lgname']."\n";
}

print "\n";

// Print out all the IDs.
foreach($lg as $db => $properties){
    print $properties['lgid']."\n";
}

When I run it on the command line, I get the following output:

$ php test.php
array(2) {
  ["lgid"]=>
  string(7) "3148425"
  ["lgname"]=>
  string(14) "AAPG Datapages"
}

PHP Notice:  Undefined index: lgid in /var/transfer/work/test.php on line 22

AAPG Datapages
AATA online: Abstracts of International Conservation Literature
Academic Search Complete

PHP Notice:  Undefined index: lgid in /var/transfer/work/test.php on line 31

PHP Notice:  Undefined index: lgid in /var/transfer/work/test.php on line 31

PHP Notice:  Undefined index: lgid in /var/transfer/work/test.php on line 31

I've been struggling with this for hours. When I var_dump one entry in the array, it sees both lgid and lgname. But if I try to access lgid it throws an "undefined index" error, even though it can get lgname with the exact same code just fine.

Yes, it's a multi-dimensional array, but I'm pretty sure I'm not pointing at the wrong level of the array.

I tried renaming lgid to something else (glerp, I think), but that had no effect. I don't think PHP cares about exactly which convention you're using for line-endings, but I've tried both \n and \r\n, just in case.

I've tried it in two different environments: PHP 7.2.24 on Ubuntu 18.04.4 (), and PHP 7.3.17 on Red Hat Enterprise Linux 7.8 (Maipo), with identical results on both.

At one point the first entry in my test array started working while the other two continued to read as undefined. Unfortunately I have no idea what I did to make that happen. The three entries appeared identical in my test file, except that one worked and the other two didn't.

Am I missing something obvious? Have I somehow triggered some exotic bug in PHP? I just don't know what else to try.

1 Answer 1

4

You seem to have invisible characters:

$lg = [
    "aapg-datapages" => [
        "<feff>lgid" => "3148425",
        "lgname" => "AAPG Datapages",
    ],
    "abstracts-of-international-conservation-literature" => [
        "<feff>lgid" => "3148426",
        "lgname" => "AATA online: Abstracts of International Conservation Literature",
    ],
    "academic-search-complete" => [
        "<feff>lgid" => "48083863",
        "lgname" => "Academic Search Complete",
    ],
];

Note the <feff>'s

Which will explain your issue.

I use vim to edit, and I can see them easily..

Removing those special/unprintable/invisible characters, returns:

array(2) {
  ["lgid"]=>
  string(7) "3148425"
  ["lgname"]=>
  string(14) "AAPG Datapages"
}

3148425
AAPG Datapages
AATA online: Abstracts of International Conservation Literature
Academic Search Complete

3148425
3148426
48083863

As output

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

2 Comments

Damned feff's. Left overs from a UTF-16 BOM probably from a windows unicode file. Nice find.
Oh for the love of ... argh. The API I pulled these records from must be sticking byte order marks on things. What a pain. I even know about these, having run into them before -- it's just usually they show up at the beginning of a file, instead of mixed into data. Thank you very much!

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.