PHP:使用循环从多个 stdclass 对象的数组中提取值
PHP: extract values from array of multiple stdclass objects using loop
`array(2) {
["result"]=>
object(stdClass)#6 (2) {
["totalResults"]=>
int(10514658)
["products"]=>
array(20) {
[0]=>
object(stdClass)#7 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(141) "http://g01.a.alicdn.com/kf/HTB1v_x3LpXXXXbDXFXXq6xXFXXXs/Medium-Super-color-Lace-up-Zapato-Mujer-grid-print-font-b-Shoes-b-font-The-Trend.jpg"
["30daysCommission"]=>
string(8) "US [=11=].02"
["productId"]=>
float(32515275585)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .99"
["productUrl"]=>
string(48) "http://www.aliexpress.com/item//32515275585.html"
["productTitle"]=>
string(128) "Medium, Super color Lace-up Zapato Mujer grid print Shoes The Trend of Female Soft Leisure Sapatos chaussure femme winter basket"
["validTime"]=>
string(10) "2016-04-15"
["volume"]=>
string(1) "1"
["evaluateScore"]=>
string(3) "5.0"
["salePrice"]=>
string(9) "US .99"
}
[1]=>
object(stdClass)#8 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(134) "http://g02.a.alicdn.com/kf/HTB1Llk.IpXXXXasXpXXq6xXFXXXH/All-match-Women-font-b-shoes-b-font-high-canvas-font-b-shoes-b-font-women.jpg"
["30daysCommission"]=>
string(8) "US [=11=].39"
["productId"]=>
int(944204198)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .40"
["productUrl"]=>
string(46) "http://www.aliexpress.com/item//944204198.html"
["productTitle"]=>
string(118) "Hot selling! lady high canvas shoes summer & winter casual shoes sneakers for women Color block decoration top quality"
["validTime"]=>
string(10) "2016-04-25"
["volume"]=>
string(2) "18"
["evaluateScore"]=>
string(3) "4.9"
["salePrice"]=>
string(9) "US .40"
}
[2]=>
object(stdClass)#9 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(144) "http://g03.a.alicdn.com/kf/HTB1N..tKXXXXXc3XXXXq6xXFXXXV/Slimming-font-b-shoes-b-font-women-fashion-leather-casual-font-b-shoes-b-font-women.jpg"
["30daysCommission"]=>
string(8) "US .14"
["productId"]=>
float(32426274938)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .04"
["productUrl"]=>
string(48) "http://www.aliexpress.com/item//32426274938.html"
["productTitle"]=>
string(101) "Free Shipping!Slimming sneakers for women, lady's Fitness Shoes Trendy Health Lady Beauty Swing Shoes"
["validTime"]=>
string(10) "2016-04-11"
["volume"]=>
string(2) "55"
["evaluateScore"]=>
string(3) "4.8"
["salePrice"]=>
string(9) "US .04"
}
[3]=>
object(stdClass)#10 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(160) "http://g02.a.alicdn.com/kf/HTB1t5a9KFXXXXcdXXXXq6xXFXXXN/Hot-Fashion-Women-Genuine-Leather-font-b-Shoes-b-font-Height-Increasing-Platform-Wedge-Sneakers-Low.jpg"
["30daysCommission"]=>
string(8) "US [=11=].06"
["productId"]=>
int(1717026606)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .58"
["productUrl"]=>
string(47) "http://www.aliexpress.com/item//1717026606.html"
["productTitle"]=>
string(123) "2014 Summer Cutout Shoes Velcro Elevator Color Block Decoration Sneaker Shoes Sport Shoes Casual High-Top Women's Cool Boot"
["validTime"]=>
string(10) "2016-04-01"
["volume"]=>
string(1) "2"
["evaluateScore"]=>
string(3) "5.0"
["salePrice"]=>
string(9) "US .58"
}
}
}
["errorCode"]=>
int(20010000)
}`
this is the responce im getting i want to get the values in the base of keys using an loop for each 4 objects i tried many solution posted here but no one is working..
需要帮助请回复!
提前致谢
$productsTitles = array();
foreach ($result->result->products as $product) {
$productsTitles[] = $product->productTitle;
}
var_dump($productsTitles);
基本上,您在这里得到了数组和对象的完美组合。您的 2 个元素的外部数组包含 "results" 您关心的内容和 "errorCode".
首先你需要遍历你得到的每个结果,然后你需要找到想要的字段("imageURL"根据评论)并将它保存在某个地方(或者回显它,它没有事情)。
我假设您的对象数组称为 $result
$imageUrls = [];
if (isset($result["results"]) && isset($result["results"]->products) {
foreach ($result["results"]->products as $productObject) {
$imageUrls[] = $productObject->imageUrl;
}
}
print_r($imageUrls); // This just dumps the output, you can do whatever else you want with it.
`array(2) {
["result"]=>
object(stdClass)#6 (2) {
["totalResults"]=>
int(10514658)
["products"]=>
array(20) {
[0]=>
object(stdClass)#7 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(141) "http://g01.a.alicdn.com/kf/HTB1v_x3LpXXXXbDXFXXq6xXFXXXs/Medium-Super-color-Lace-up-Zapato-Mujer-grid-print-font-b-Shoes-b-font-The-Trend.jpg"
["30daysCommission"]=>
string(8) "US [=11=].02"
["productId"]=>
float(32515275585)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .99"
["productUrl"]=>
string(48) "http://www.aliexpress.com/item//32515275585.html"
["productTitle"]=>
string(128) "Medium, Super color Lace-up Zapato Mujer grid print Shoes The Trend of Female Soft Leisure Sapatos chaussure femme winter basket"
["validTime"]=>
string(10) "2016-04-15"
["volume"]=>
string(1) "1"
["evaluateScore"]=>
string(3) "5.0"
["salePrice"]=>
string(9) "US .99"
}
[1]=>
object(stdClass)#8 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(134) "http://g02.a.alicdn.com/kf/HTB1Llk.IpXXXXasXpXXq6xXFXXXH/All-match-Women-font-b-shoes-b-font-high-canvas-font-b-shoes-b-font-women.jpg"
["30daysCommission"]=>
string(8) "US [=11=].39"
["productId"]=>
int(944204198)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .40"
["productUrl"]=>
string(46) "http://www.aliexpress.com/item//944204198.html"
["productTitle"]=>
string(118) "Hot selling! lady high canvas shoes summer & winter casual shoes sneakers for women Color block decoration top quality"
["validTime"]=>
string(10) "2016-04-25"
["volume"]=>
string(2) "18"
["evaluateScore"]=>
string(3) "4.9"
["salePrice"]=>
string(9) "US .40"
}
[2]=>
object(stdClass)#9 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(144) "http://g03.a.alicdn.com/kf/HTB1N..tKXXXXXc3XXXXq6xXFXXXV/Slimming-font-b-shoes-b-font-women-fashion-leather-casual-font-b-shoes-b-font-women.jpg"
["30daysCommission"]=>
string(8) "US .14"
["productId"]=>
float(32426274938)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .04"
["productUrl"]=>
string(48) "http://www.aliexpress.com/item//32426274938.html"
["productTitle"]=>
string(101) "Free Shipping!Slimming sneakers for women, lady's Fitness Shoes Trendy Health Lady Beauty Swing Shoes"
["validTime"]=>
string(10) "2016-04-11"
["volume"]=>
string(2) "55"
["evaluateScore"]=>
string(3) "4.8"
["salePrice"]=>
string(9) "US .04"
}
[3]=>
object(stdClass)#10 (13) {
["lotNum"]=>
int(1)
["packageType"]=>
string(5) "piece"
["imageUrl"]=>
string(160) "http://g02.a.alicdn.com/kf/HTB1t5a9KFXXXXcdXXXXq6xXFXXXN/Hot-Fashion-Women-Genuine-Leather-font-b-Shoes-b-font-Height-Increasing-Platform-Wedge-Sneakers-Low.jpg"
["30daysCommission"]=>
string(8) "US [=11=].06"
["productId"]=>
int(1717026606)
["discount"]=>
string(2) "0%"
["originalPrice"]=>
string(9) "US .58"
["productUrl"]=>
string(47) "http://www.aliexpress.com/item//1717026606.html"
["productTitle"]=>
string(123) "2014 Summer Cutout Shoes Velcro Elevator Color Block Decoration Sneaker Shoes Sport Shoes Casual High-Top Women's Cool Boot"
["validTime"]=>
string(10) "2016-04-01"
["volume"]=>
string(1) "2"
["evaluateScore"]=>
string(3) "5.0"
["salePrice"]=>
string(9) "US .58"
}
}
}
["errorCode"]=>
int(20010000)
}`
this is the responce im getting i want to get the values in the base of keys using an loop for each 4 objects i tried many solution posted here but no one is working..
需要帮助请回复! 提前致谢
$productsTitles = array();
foreach ($result->result->products as $product) {
$productsTitles[] = $product->productTitle;
}
var_dump($productsTitles);
基本上,您在这里得到了数组和对象的完美组合。您的 2 个元素的外部数组包含 "results" 您关心的内容和 "errorCode".
首先你需要遍历你得到的每个结果,然后你需要找到想要的字段("imageURL"根据评论)并将它保存在某个地方(或者回显它,它没有事情)。
我假设您的对象数组称为 $result
$imageUrls = [];
if (isset($result["results"]) && isset($result["results"]->products) {
foreach ($result["results"]->products as $productObject) {
$imageUrls[] = $productObject->imageUrl;
}
}
print_r($imageUrls); // This just dumps the output, you can do whatever else you want with it.