2

Hi all i have the follow array

Classes\Form_Record Object
(
    [sent_data:protected] => Array
        (
            [cw6hwgw] => 11
            [ezlkp6m] => 11
        )

    [fields:protected] => Array
        (
            [cw6hwgw] => Array
                (...

And trying to read the vaules from cw6hwgw and ezlkp6m

What do i need to get the result in php as we tried [sent_data][0] which dose not work

Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
user34416
  • 21
  • 4
  • Post the full `Classes\Form_Record` class code. – Calimero Sep 12 '17 at 10:32
  • Those are protected properties - you aren't supposed to access them directly. See if there are any accessor methods on the class. – iainn Sep 12 '17 at 10:34
  • may this can help you : https://stackoverflow.com/questions/9474446/get-first-element-in-php-stdobject – Punit Gajjar Sep 12 '17 at 10:44
  • Possible duplicate of [How to get protected property of object in PHP](https://stackoverflow.com/questions/20334355/how-to-get-protected-property-of-object-in-php) – iainn Sep 12 '17 at 10:46

2 Answers2

1

if I understand correctly, you have used an array in the array so you should use two foreach loops, or indicate the number in the array for both arrays.

Mat.eo
  • 31
  • 1
  • 8
1

you need to cast the object to array.

sample code:

$my_obj =(object) ['sent_data:protected' => ['cw6hwgw' => 11, 'ezlkp6m' => 11 ]];

echo gettype($my_obj)."\n\n";


$to_array_ = (array) $my_obj;

foreach($to_array_['sent_data:protected'] as $k=>$v){
    echo "{$k} = $v \n";
} 
Shoaib Zafar
  • 312
  • 2
  • 8
  • interesting when converted to a array i get this and not sent_data:protected Array ( [ * sent_data] => Array ( [cw6hwgw] => 11 [ezlkp6m] => 11 ) and still cannot read it, not sure if the * or space before is invaild print_r ([ * sent_data]) – user34416 Sep 12 '17 at 13:00
  • use `print_r(get_object_vars($YOUR_OBJECT_VAR));` to get the indexes. – Shoaib Zafar Sep 12 '17 at 14:10