-4
[{"id":1,"username":"example","email":"example@gmail.com","password":"example123","created_at":"2015-01-13 11:39:24","updated_at":"2015-01-13 11:39:24"},
{"id":2,"username":"ex2","email":"ex@ex.com","password":"example","created_at":"2015-01-13 11:39:02","updated_at":"2015-01-13 11:39:02"}]

I got an object like above. The thing I want to is select the object that's id is 1 for example. Like

select * from object where id=1

with SQL

How to do it?

4 Answers4

0

Decode your object (http://php.net/manual/it/function.json-decode.php) and then traverse your array with a foreach checking the id value inside of it with an if statement.

$obj_to_array = json_decode($json_object);
$target = '';
foreach($obj_to_array as $row){
  if($row['id'] == 1)
    $target = $row;
}

// $target holds your row with id = 1
Masiorama
  • 1,066
  • 17
  • 26
0
$data = '[{"id":1,"username":"example","email":"example@gmail.com","password":"example123","created_at":"2015-01-13 11:39:24","updated_at":"2015-01-13 11:39:24"},
{"id":2,"username":"ex2","email":"ex@ex.com","password":"example","created_at":"2015-01-13 11:39:02","updated_at":"2015-01-13 11:39:02"}]';

$data = json_decode($data);

$id = 1;
$key = 'id';
$data = array_filter(
    $data,
    function($value) use ($key, $id) {
        return ($value->$key == $id);
    }
);
var_dump($data);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

Decode the object using json_decode. Use the code below

    <?php
    $json='[{"id":1,"username":"example","email":"example@gmail.com","password":"example123","created_at":"2015-01-13 11:39:24","updated_at":"2015-01-13 11:39:24"},
    {"id":2,"username":"ex2","email":"ex@ex.com","password":"example","created_at":"2015-01-13 11:39:02","updated_at":"2015-01-13 11:39:02"}]';
    $p = json_decode($json,true);
    $l=count($p);
    for($i=0;$i<=$l;$i++){
    $id=$p[$i]["id"];
if($id==1){
    print_r($p[$i]); // WIll print the id if 1
}
    }

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
0

I hope i got your question right. Try the following

<?php
$array = json_decode('[{"id":1,"username":"example","email":"example@gmail.com","password":"example123","created_at":"2015-01-13 11:39:24","updated_at":"2015-01-13 11:39:24"},
{"id":2,"username":"ex2","email":"ex@ex.com","password":"example","created_at":"2015-01-13 11:39:02","updated_at":"2015-01-13 11:39:02"}]');

foreach( $array as $v ) {
  if( $v["id"] == 1 ) {
    echo "I am ID 1";
    break;
  }
}
Jonas m
  • 2,646
  • 3
  • 22
  • 43
  • 1
    Well. No matter what method you are using, filtering or direct traversing ( Same stuff basically ) It's going to be slow and ressource hungry if you have a thousand objects, so dont get yourself a thousand objects. Filter them out before recieving the JSON string. Where do you get the data from? Don't the service have a limit parameter or something like that? – Jonas m Jan 13 '15 at 10:26
  • I got comments table and user_id row. In single post page I got all of the usernames for now but yes I can filter but that will not make statement clearer.. – Philosopher Jan 13 '15 at 10:28
  • 1
    The point is to do searches/filterings on the platforms ment for it and optimized for it. If you can do the select through sql before getting the JSON or through the API, do so, if not. Well, then do the traversing/foreach. It wont break your server :-) – Jonas m Jan 13 '15 at 10:31