user8253791
user8253791

Reputation:

array_keys not working in laravel

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.

Upvotes: 0

Views: 5386

Answers (2)

Tomasz
Tomasz

Reputation: 5162

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

Upvotes: 4

Thamilhan
Thamilhan

Reputation: 13313

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

Upvotes: 4

Related Questions