我有一个变量$ tinder->建议。当我把它保存在var_dump中时。
与var_dump($ tinder->推荐)相似;
它给出了一个在下面的数组。
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) }
现在我只想从' _id'中获得价值。 &安培; '名称'并将它们保存在数据库中。我已经尝试了下面的代码,但它没有给出任何结果,所以你能帮助我,我怎样才能提取_id&的值命名并将其保存在数据库中?
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";
}
结果:财产不存在。
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";
}
结果:对象不存在。
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";
}
结果:对象不存在。
我知道之前已经问过这个问题,但我没有弄明白 答案是我在这里问的原因。 任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
在PHP中对数组进行迭代是简单的循环,但是当你表达它们时可能会有点混乱:
$myArray = [
"foo" => "Foo",
"bar" => "Bar"
];
所以你可以迭代一个" tuple":
foreach($myArray as $key => $value) {
print($key);
print($value);
print($myArray[$key]);
}
或简单的超值:
foreach($myArray as $value) {
print($value);
}
您的情况是PHP中的经典对象迭代。由于PHP v5 PHP支持对象迭代为简单数组。
foreach($yourTinderObject->result()[0] as $contact) {
print($contact->name);
}
正如您已经观察到的那样,我使用$yourTinderObject->result()
,它是一个带有数组的函数。使用[0]
密钥,我只需针对数组' 0'键,这是一个对象。然后,您可以使用循环值返回对象属性:$contact->_id
或$contact->name
。
如果对象具有带数字键的属性,例如:
,则会出现特殊情况["arr"]=> array(1) {
[0]=> object(stdClass)#1 (3) {
["abc"]=> bool(false)
["123"]=> int(14)
["123abc"]=> int(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
}
一个非常有趣的发现如下:
$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 !?!!
通常" hack"是之前打电话:
$myObject = json_decode($myObject, true);
然后$ myObject将多态转换为一个可以像往常一样迭代的数组。
希望有所帮助!
LL