extract value from var_dump and save them on database
我有一个变量$tinder->建议。当我把它放在瓦鲁垃圾堆里的时候。
比如var_dump($tinder->recommendments);
它给出了一个位于下面的数组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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) } |
现在,我只想从'u id'&;'name'中获取值,并将其保存在数据库中。我已经尝试过下面的代码,但没有给出任何结果,所以您可以帮助我,如何提取ID&name的值并将其保存到数据库中?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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"; } |
号
结果:属性不存在。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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"; } |
结果:对象不存在。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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"; } |
。
结果:对象不存在。
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.
号
在PHP中对数组进行迭代是简单的循环,但是当您表达它们时,可能会有点混乱:
1 2 3 4 | $myArray = [ "foo" =>"Foo", "bar" =>"Bar" ]; |
所以您可以迭代一个"tuple":
1 2 3 4 5 | foreach($myArray as $key => $value) { print($key); print($value); print($myArray[$key]); } |
号
或简单超值:
1 2 3 | foreach($myArray as $value) { print($value); } |
您的情况是PHP中的经典对象迭代。因为php v5 php支持简单数组的对象迭代。
1 2 3 | foreach($yourTinderObject->result()[0] as $contact) { print($contact->name); } |
。
正如您已经观察到的,我使用
如果对象具有数字键的属性,例如:
1 2 3 4 5 6 7 | ["arr"]=> array(1) { [0]=> object(stdClass)#1 (3) { ["abc"]=> bool(false) ["123"]=> int(14) ["123abc"]=> int(5) } } |
在这种情况下,当您迭代对象时
1 2 3 4 5 | foareach($r[0] as $myObject){ print($myObject->abc); // this will work print($myObject->{"123"}); //this will not work print($myObject->{"123abc"}) //this will work } |
。
一个非常有趣的发现是:
1 2 3 4 5 6 7 | $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 !?!! |
。
所以通常的"黑客"是在以下情况之前打电话:
1 |
然后$myObject将被多态转换成一个数组,可以像往常一样迭代。
希望有帮助!
陆上通信线