0

Running into a problem where I can access every other property of this stdClass object except for the first. I am taking data from an uploaded csv and turning it into an array. from the array I am them converting it to an object. Code below

case($_FILES['csv_upload'] != NULL);
            $data = [];
            $csv = array_map('str_getcsv', file($_FILES['csv_upload']['tmp_name']));
            $header = array_map('strtolower' , $csv[0]);
            foreach($csv as $row)
            {
               $row = preg_replace("/[\$,]/", "", $row );
               $data[] = array_combine($header, $row);
            }
            $layout->items = json_decode(json_encode($data), FALSE);
            //unsetting the headers
            unset($layout->items[0]);

this gives me an array of stdClass objects. When I do a foreach, I am able to access every property of the object except for the first one (id). A print_r($layout->items) returns the below

Array
(
    [1] => stdClass Object
        (
            [id] => 253
            [fet] =>  157.50 
            [salecost] => 78.75
            [price] => 157.50 
            [retail] => 0
        )

    [2] => stdClass Object
        (
            [id] => 243
            [fet] => 150.00 
            [salecost] => 75
            [price] => 150.00 
            [retail] => 0
        )

a vardump returns the below

array(47) { [1]=> object(stdClass)#2856 (5) { ["id"]=> string(6) "253" ["fet"]=> string(10) " 157.50 " ["salecost"]=> string(5) "78.75" ["price"]=> string(8) "157.50 " ["retail"]=> string(1) "0" } [2]=> object(stdClass)#243 (5) { ["id"]=> string(6) "245815" ["fet"]=> string(8) "150.00 " ["salecost"]=> string(2) "75" ["price"]=> string(8) "150.00 " ["retail"]=> string(1) "0" }

I need to be able to access the 'id' property but, when I try to, it does not return anything, however, I can access any other property in the object and it returns correctly. I'm stumped.

  • 1
    You have a array of stdclass-objects.. Does this answer your question? [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – jspit Jan 06 '23 at 14:33
  • 1
    Can you show how you are accessing the `id` property? I mocked up your code but see no issue accessing `id`: https://3v4l.org/ZvXMW – cOle2 Jan 06 '23 at 14:37
  • I figured it out via @jspit comment. There was a hidden \ufeff in the objects 'id' key, I guess left over from parsing the csv. I do a preg replace on the headers and problem solved. Thanks all! – WhocaresMcgee Jan 06 '23 at 14:47

0 Answers0