get the id of a php array

0

I have this array

object(stdClass)[138]
  public 'articles' => 
    array (size=39)
      0 => 
        object(stdClass)[140]
          public 'id' => int 56
          public 'title' => string 'prueba con servicio json es tt aa' (length=33)
          public 'body' => string 'prueba de hoy' (length=13)
          public 'created' => string '2018-06-22T19:49:34+00:00' (length=25)
          public 'modified' => string '2018-06-22T19:49:34+00:00' (length=25)
      1 => 
        object(stdClass)[139]
          public 'id' => int 55
          public 'title' => string 'prueba con servicio json es tt' (length=30)
          public 'body' => string 'prueba de hoy' (length=13)
          public 'created' => string '2018-06-22T19:49:12+00:00' (length=25)
          public 'modified' => string '2018-06-22T19:49:12+00:00' (length=25)
      2 => 
        object(stdClass)[137]
          public 'id' => int 54
          public 'title' => string 'prueba jhon bernal json' (length=23)
          public 'body' => string 'prueba de hoy' (length=13)
          public 'created' => string '2018-06-22T19:46:55+00:00' (length=25)
          public 'modified' => string '2018-06-27T19:05:45+00:00' (length=25)

I want to perform an operation to obtain only one object according to its id.

 object(stdClass)[137]
          public 'id' => int 54
          public 'title' => string 'prueba jhon bernal json' (length=23)
          public 'body' => string 'prueba de hoy' (length=13)
          public 'created' => string '2018-06-22T19:46:55+00:00' (length=25)
          public 'modified' => string '2018-06-27T19:05:45+00:00' (length=25
    
asked by Jhon Bernal 29.06.2018 в 00:42
source

1 answer

1

Suppose that the object you show is called $objArticles , you would have to:

  • access the array of objects inside the property articles
  • traverse that array of objects looking for the property / value that interests you.
  • once found, you can use get_object_vars to create an array with all the non-static properties of that part only

The code would look like this:

$arrArticles=$objArticles->articles;
$arrResult=array();

    foreach ($arrArticles as $theArticle) {

        if ($theArticle->id==54){
              $arrResult =get_object_vars($theArticle);  
        }
    }

print_r($arrResult);

That's all! Use $arrResult according to your need.

    
answered by 29.06.2018 / 01:30
source