0

I hava a variable $tinder->recommendations. When i keep it in var_dump.

Like var_dump($tinder->recommendations);

It gives an array which is in below.

object(stdClass)#7 (2) {
  ["status"]=> int(200)
  ["results"]=> array(47) {
    [0]=> object(stdClass)#34 (23) {

      ["group_matched"]=> bool(false)
      ["distance_mi"]=> int(14)
      ["content_hash"]=> string(47) "5nVT6PhabtGrSRqiRiLAfqXH1LFkps25sagHawimxt5Eh4r"
      ["common_friends"]=> array(0) { } 

      ["common_likes"]=> array(0) { } 
      ["common_friend_count"]=> int(0) 
      ["common_like_count"]=> int(0) 
      ["connection_count"]=> int(0) 
      ["_id"]=> string(24) "583c9fd7298a09eb42f32df1" 
      ["bio"]=> string(353) "Family, friends, my pooches, music. Old fashioned as far as relationships go. I'm not an apple polisher. Not sure if this is the place to find a keeper, but who knows. I'd say this is better than meeting someone at a bar haha. Music: Blues, Soul, Doo Wop, Folk, Bluegrass, Country, Doom, Punk Red Heeler & Old English Bulldog 6'-0" for those who care" 
      ["birth_date"]=> string(24) "1989-11-23T20:12:56.493Z" 
      ["name"]=> string(5) "Trent" 
      ["ping_time"]=> string(24) "2014-12-09T00:00:00.000Z" 
      ["photos"]=> array(6) { 
        [0]=> object(stdClass)#33 (3) { 
          ["id"]=> string(36) "318ad35d-3e14-44c6-8348-3aacfc4594bd" 
          ["url"]=> string(92) "http://images.gotinder.com/583c9fd7298a09eb42f32df1/318ad35d-3e14-44c6-8348-3aacfc4594bd.jpg" ["processedFiles"]=> array(4) { 
            [0]=> object(stdClass)#35 (3) { 
              ["url"]=> string(100) "http://images.gotinder.com/583c9fd7298a09eb42f32df1/640x640_318ad35d-3e14-44c6-8348-3aacfc4594bd.jpg" 
              ["height"]=> int(640) 
              ["width"]=> int(640) }

            [1]=> object(stdClass)#36 (3) { 
              ["url"]=> string(100) "http://images.gotinder.com/583c9fd7298a09eb42f32df1/320x320_318ad35d-3e14-44c6-8348-3aacfc4594bd.jpg" 
              ["height"]=> int(320) 
              ["width"]=> int(320) }

            [2]=> object(stdClass)#37 (3) { 
              ["url"]=> string(100) "http://images.gotinder.com/583c9fd7298a09eb42f32df1/172x172_318ad35d-3e14-44c6-8348-3aacfc4594bd.jpg" 
              ["height"]=> int(172) 
              ["width"]=> int(172) }

Now i want to get only value from '_id' & 'name' and save them on database. I've tried code bellow but it gives no result, so can you help me, how can I extract value of _id & name and save them on database?

   if ($tinder->recommendations() > 0) {
      foreach($tinder->recommendations() as $contact) {

          if( property_exists( $contact, '_id') &&
            property_exists( $contact, 'name')){

              echo "$contact->_id";

          } else {

              echo "property doesn't exists";

          }

      }
    } else {

      echo "object doesn't exists";

    }

result: property doesn't exists.

if ($tinder->recommendations > 0) {
  foreach($tinder->recommendations as $contact) {

      if( property_exists( $contact, '_id') &&
        property_exists( $contact, 'name')){

          echo "$contact->_id";

      } else {

          echo "property doesn't exists";

      }

  }
} else {

  echo "object doesn't exists";

}

result: object doesn't exists.

if ($tinder->recommendations->results > 0) {
  foreach($tinder->recommendations->results as $contact) {

      if( property_exists( $contact, '_id') &&
        property_exists( $contact, 'name')){

          echo "$contact->_id";

      } else {

          echo "property doesn't exists";

      }

  }
} else {

  echo "object doesn't exists";

}

result: object doesn't exists.

I know this question has been asked before, but I've failed to figure out with the answer that's why I'm asking here. any help would be appreciated, Thanks.

Joy Kalyan
  • 13
  • 7
  • Possible duplicate of [How can I capture the result of var\_dump to a string?](https://stackoverflow.com/questions/139474/how-can-i-capture-the-result-of-var-dump-to-a-string) – Mark Nov 21 '17 at 01:29
  • Shouldn't it be `foreach($tinder->recommendations->results as $contact) {` and where is `fields` set? – Lawrence Cherone Nov 21 '17 at 01:56
  • foreach($tinder->recommendations->results as $contact) { if( property_exists( $contact, '_id') && property_exists( $contact, 'name')){ $query = $mysqli->query("INSERT INTO rec SET token_id = '". $row['token_id'] ."', rec_key = '_id', rec_value = '". $contact->name ."', rec_time = '$time'"); } } I've tried this what You sugested, but no result. – Joy Kalyan Nov 21 '17 at 02:30
  • @LawrenceCherone I've updated question with some test based on your suggestion. but non of them works for me. thanks. – Joy Kalyan Nov 21 '17 at 10:55
  • Whats `print_r($tinder->recommendations->results)` show? – Lawrence Cherone Nov 21 '17 at 10:59
  • You can try `foreach($tinder->recommandation->results[0] as $contact { ... }` – Leonard Lepadatu Nov 21 '17 at 11:02
  • @LawrenceCherone print_r($tinder->recommendations->results) show nothing. – Joy Kalyan Nov 21 '17 at 11:02
  • @LeonardLepadatu this also show nothing as result, just empty. – Joy Kalyan Nov 21 '17 at 11:07
  • first check if you have data: `var_dump( $tinder->recommendations); die();`; then check `var_dump( $tinder->recommendations->results); die();`, then check `var_dump( $tinder->recommendations->results[0]); die();`, then if you have data just iterate and check if you have obj property `var_dump($contact->_id)` – Leonard Lepadatu Nov 21 '17 at 11:10
  • @LeonardLepadatu `var_dump($tinder->recommendations()->results[0]);` show expected result. and `var_dump($tinder->recommendations()->results[0]->_id);` get result as: **string(24) "59db02622bc0cd2d261c81de"**. – Joy Kalyan Nov 21 '17 at 11:37
  • `foreach($tinder->recommendations()->results[0] as $contact) { var_dump($contact->_id); }` get result: **NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL** – Joy Kalyan Nov 21 '17 at 11:40
  • Sorry for delay. Hmmm it seams that you iterate over a new instance object and every time because the _id hash is changed than original. Try to cache the instance to a new variable like `$recommendations = $tinder->recommendation();` Because I don't know if you have numerical keys for your props, first call `$recommendation = json_decode($recommendation, true)`. This will return an array. Then iterate the array `foreach($recommendation as $key => $contacts) { var_dump($key, $contacts); } and see what is printing ... – Leonard Lepadatu Nov 21 '17 at 15:49
  • thanks @LeonardLepadatu, I've figured out this way as you suggested, `foreach($tinder->recommendations()->results[0] as $contact) { $rec_id = $tinder->recommendations()->results[0]->_id; $rec_name = $tinder->recommendations()->results[0]->name; } }` and this works, Please let me know If I've anything messy. thanks again. – Joy Kalyan Nov 21 '17 at 18:48
  • should be: $rec_id=$contact->_id; //etc – Leonard Lepadatu Nov 21 '17 at 19:30
  • thanks @LeonardLepadatu . I've figured out with Your help, but I'm still confused with this mechanism. please let me know what should I learn. by the way, thanks a lot. – Joy Kalyan Nov 21 '17 at 19:52
  • I will post an answer right away – Leonard Lepadatu Nov 21 '17 at 19:54

1 Answers1

0

Iteration over arrays in PHP are simple loops but when you express them can be a little bit confusing:

$myArray = [
   "foo" => "Foo",
   "bar" => "Bar"
];

so you can iterate over a "tuple":

foreach($myArray as $key => $value) {
   print($key);
   print($value);
   print($myArray[$key]);
}

or simple over value:

foreach($myArray as $value) {
   print($value);
}

Your situation is a classical object iteration in PHP. Since PHP v5 PHP support object iteration as simple arrays.

foreach($yourTinderObject->result()[0] as $contact) {
  print($contact->name);
}

As you are already observed, I use $yourTinderObject->result(), that it is a function with an array as result. With [0] key I just address to array's '0' key, that is an object. Then you can use your looping value to return your object properties: $contact->_id or $contact->name.

A particularity can arise when you have objects with properties with numerical keys like:

["arr"]=> array(1) {
      [0]=> object(stdClass)#1 (3) {
         ["abc"]=> bool(false)
         ["123"]=> int(14)
         ["123abc"]=> int(5)
      }
 }

In this situation when you iterate over object

 foareach($r[0] as $myObject){
     print($myObject->abc); // this will work
     print($myObject->{"123"}); //this will not work
     print($myObject->{"123abc"}) //this will work
 }

A very interesting findings is the following:

 $arr = ['abc'=>'ABC', '123'=>'123', '123abc'=>'123abc'];
 $oA = (object)$arr; //casting the array to object working in dynamic languages like PHP
 $oB = new stdClass; //create plan object
 $oB->{"123"} = "123" //setting "123" property is OK

 print($oA->{"123"}); //will not working
 print($oB->{"123"}); //will work !?!! 

So the usual "hack" is to call before:

$myObject = json_decode($myObject, true);

and then $myObject will be polymorphic transforming into an array that can be iterate as usual.

Hope that help!

LL

Leonard Lepadatu
  • 606
  • 8
  • 14