dweb Posted July 15, 2014 Share Posted July 15, 2014 Hi I have this json file {"user":"RD32","time":"14:00","type":"staff"}{"user":"XC12","time":"21:00","type":"management"}{"user":"YG89","time":"09:00","type":"staff"} and I want to pull just "staff" from it and output like user: RD32 time: 14:00 ----- user: YG89 time: 09:00 is that possible? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 15, 2014 Share Posted July 15, 2014 Yes you would need to loop over each user in the array and check that the value of the type index is set to staff $json_string = '[{"user":"RD32","time":"14:00","type":"staff"}, {"user":"XC12","time":"21:00","type":"management"}, {"user":"YG89","time":"09:00","type":"staff"}]'; $users = json_decode($json_string,1); $staffUsers = array(); foreach($users as $user) { if(isset($user['type']) && $user['type'] == 'staff') { $staffUsers[] = $user; } } print_r($staffUsers); /* Returns Array ( [0] => Array ( [user] => RD32 [time] => 14:00 [type] => staff ) [1] => Array ( [user] => YG89 [time] => 09:00 [type] => staff ) ) */ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.