puntable
puntable

Reputation: 337

Missing value in associative array, but value visible with var_dump()

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.

Upvotes: 0

Views: 103

Answers (2)

puntable
puntable

Reputation: 337

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"

Upvotes: 0

Shura
Shura

Reputation: 11

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).

Upvotes: 1

Related Questions