0

I have a simple associative array called $product.

This is how it looks with var_dump[$product]

array(5) {
  ["sku"]=>
  string(9) "001R00610"
  ["name"]=>
  string(28) "Xerox 001R00610 Transfer Kit"
  ["image_label"]=>
  string(28) "Xerox 001R00610 Transfer Kit"
  ["small_image_label"]=>
  string(28) "Xerox 001R00610 Transfer Kit"
  ["thumbnail_label"]=>
  string(28) "Xerox 001R00610 Transfer Kit"
}

But when i try and get the value of sku with var_dump($product['sku']) it returns null?

var_dump($product['sku']);

returns

NULL

I have noticed there seems to be a linebreak in at sku, but i am not sure what is causing this or if this is related to my issue.

puntable
  • 299
  • 6
  • 19
  • 2
    Show what you get for `var_dump(array_keys($product));` – aynber Apr 04 '19 at 15:05
  • 1
    This doesn't seem possible, however if there is a linebreak in the `"sku"` key, then then it would be something like `"sku\n"` and not `"sku"`? Did you edit the result of `var_dump()` to get rid of the linebreak? – KIKO Software Apr 04 '19 at 15:09
  • 2
    If this is from a loaded CSV file - check for a [BOM](https://stackoverflow.com/questions/2558172/utf-8-bom-signature-in-php-files) at the start of the file - so `sku` has a few control characters at the start. A quick check is for the length of the key. – Nigel Ren Apr 04 '19 at 15:24

2 Answers2

1

php doesnt print line breaks in keys with var_dump, they become space characters, but are still in the accessor a linebreak. This code:

$obj = array("foo\r" => "bar");
var_dump($obj);

prints this:

array(1) { ["foo "]=> string(3) "bar" }

and cannot be accessed by this:

$obj["foo"]; //returns null
$obj["foo "]; //returns null

only:

$obj["foo\n"] //returns bar

works as array keys get compared as bits(I think).

Shura
  • 11
  • 2
  • Excellent point Shura. I didn't know it turned into a space, does it? Testing. Well, it doesn't really. It is still a linebreak (see html source) but that looks like a space in your browser. Regretably, **puntable** doesn't seem to be reacting very much, so we will probably never know if this was the problem. – KIKO Software Apr 04 '19 at 15:30
0

SOLVED

As suggested by @aynber i tried doing a var_dump(array_keys($product)); which returned this:

array(5) {
  [0]=>
  string(6) "sku"
  [1]=>
  string(4) "name"
  [2]=>
  string(11) "image_label"
  [3]=>
  string(17) "small_image_label"
  [4]=>
  string(15) "thumbnail_label"
}

Lenght of array key sku is wrong.

Array is created from CVS as pointed out by @Nigel Ren. After converting from UTF-8-BOM to UTF-8 it return the expected value.

var_dump($product['sku']); returns string(9) "001R00610"

puntable
  • 299
  • 6
  • 19