0

I want to return an array containing the keys:

<?php
if(isset($values[0]->Content)):
$jarray= json_decode($values[0]->Content);
print_r(array_keys($jarray));
exit;
endif;
?>

Output of $jarray:

stdClass Object
(
    [_token] => QjyTAyDDUeadYvO0qj0gAZyK7OVyudSsY7Sq8Hhp
    [datefrom] => 2017-07-07
    [dateto] => 2017-07-31
    [Productivity] => test1
    [Productivityrating] => 2
    [Technical_Skills] => test2
    [Technical_Skillsrating] => 3
    [Work_Consistency] => test3
    [Work_Consistencyrating] => 4
    [Presentation_skills] => test4
    [Presentation_skillsrating] => 5
    [test] => test5
    [testrating] => 3
    [cycle_id] => 1
    [save] => proceed
)

I want to return an array containing the keys so i tried

array_keys($jarray)

but it gives an error like

array_keys() expects parameter 1 to be array, object given (View: my path)

Expected output:

Array
(
    [0] =>_token
    [1] => datefrom
    [2] =>_dateto
    [3] => Productivity
    [4] =>Productivityrating
    [5] => Work_Consistency
    [6] =>_Work_Consistencyrating

)etc

Any help would be appreciated.

3
  • 2
    it's not an array it's an object. Commented Jul 12, 2017 at 9:19
  • its because $jarray is an object Commented Jul 12, 2017 at 9:19
  • try $jarray= (array) $jarray;, typecasting object to array Commented Jul 12, 2017 at 9:21

2 Answers 2

4

json_decode() returns a json string decoded into object in default (that's why get error). To get your json decoded into an array you should use it with second parameter equals true as following:

$jarray = json_decode($values[0]->Content, true);
Sign up to request clarification or add additional context in comments.

Comments

4

Since you tagged as Laravel, I would suggest to go with Laravel's Key method that returns all of the collection's keys

$keys = $collection->keys();

$keys->all();

// ['prod-100', 'prod-200']

Reference: Laravel Docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.