我有这个阵列:
Array
(
[0] => stdClass Object
(
[id] => 165
[id_format] => {num}
[id_value] => 315
[module_id] => 3
[name] => Product 1
[description] => Product 1
[description_html] =>
Product 1
[qty] =>
[module_row] => 2
[module_group] =>
[taxable] => 1
[single_term] => 0
[status] => active
[company_id] => 1
[prorata_day] =>
[prorata_cutoff] =>
[id_code] => 315
[module_name] => Module
[pricing] => Array
(
[0] => stdClass Object
(
[id] => 1903
[pricing_id] => 1903
[package_id] => 165
[term] => 1
[period] => year
[price] => 23.4000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[1] => stdClass Object
(
[id] => 1904
[pricing_id] => 1904
[package_id] => 165
[term] => 2
[period] => year
[price] => 46.8000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[2] => stdClass Object
(
[id] => 1905
[pricing_id] => 1905
[package_id] => 165
[term] => 3
[period] => year
[price] => 70.2000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[3] => stdClass Object
(
[id] => 1906
[pricing_id] => 1906
[package_id] => 165
[term] => 4
[period] => year
[price] => 93.6000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[4] => stdClass Object
(
[id] => 1907
[pricing_id] => 1907
[package_id] => 165
[term] => 5
[period] => year
[price] => 117.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[5] => stdClass Object
(
[id] => 1908
[pricing_id] => 1908
[package_id] => 165
[term] => 6
[period] => year
[price] => 140.4000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[6] => stdClass Object
(
[id] => 1909
[pricing_id] => 1909
[package_id] => 165
[term] => 7
[period] => year
[price] => 163.8000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[7] => stdClass Object
(
[id] => 1910
[pricing_id] => 1910
[package_id] => 165
[term] => 8
[period] => year
[price] => 187.2000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[8] => stdClass Object
(
[id] => 1911
[pricing_id] => 1911
[package_id] => 165
[term] => 9
[period] => year
[price] => 210.6000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
)
)
[1] => stdClass Object
(
[id] => 168
[id_format] => {num}
[id_value] => 318
[module_id] => 3
[name] => Product 2
[description] => Product 2
[description_html] =>
Product 2
[qty] =>
[module_row] => 20
[module_group] =>
[taxable] => 1
[single_term] => 0
[status] => active
[company_id] => 1
[prorata_day] =>
[prorata_cutoff] =>
[id_code] => 318
[module_name] => Module
[pricing] => Array
(
[0] => stdClass Object
(
[id] => 1993
[pricing_id] => 1993
[package_id] => 168
[term] => 1
[period] => year
[price] => 34.5000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[1] => stdClass Object
(
[id] => 1994
[pricing_id] => 1994
[package_id] => 168
[term] => 2
[period] => year
[price] => 69.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[2] => stdClass Object
(
[id] => 1995
[pricing_id] => 1995
[package_id] => 168
[term] => 3
[period] => year
[price] => 103.5000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[3] => stdClass Object
(
[id] => 2006
[pricing_id] => 2006
[package_id] => 168
[term] => 4
[period] => year
[price] => 138.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
1
[4] => stdClass Object
(
[id] => 2017
[pricing_id] => 2017
[package_id] => 168
[term] => 5
[period] => year
[price] => 172.5000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[5] => stdClass Object
(
[id] => 1998
[pricing_id] => 1998
[package_id] => 168
[term] => 6
[period] => year
[price] => 207.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[6] => stdClass Object
(
[id] => 1999
[pricing_id] => 1999
[package_id] => 168
[term] => 7
[period] => year
[price] => 241.5000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[7] => stdClass Object
(
[id] => 2000
[pricing_id] => 2000
[package_id] => 168
[term] => 8
[period] => year
[price] => 276.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[8] => stdClass Object
(
[id] => 2001
[pricing_id] => 2001
[package_id] => 168
[term] => 9
[period] => year
[price] => 310.5000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
)
)
[2] => stdClass Object
(
[id] => 169
[id_format] => {num}
[id_value] => 319
[module_id] => 4
[name] => Product 3
[description] => Product 3
[description_html] =>
Product 3
[qty] =>
[module_row] => 3
[module_group] =>
[taxable] => 1
[single_term] => 0
[status] => active
[company_id] => 1
[prorata_day] =>
[prorata_cutoff] =>
[id_code] => 319
[module_name] => Module
[pricing] => Array
(
[0] => stdClass Object
(
[id] => 2023
[pricing_id] => 2023
[package_id] => 169
[term] => 1
[period] => year
[price] => 15.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[1] => stdClass Object
(
[id] => 2024
[pricing_id] => 2024
[package_id] => 169
[term] => 2
[period] => year
[price] => 30.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[2] => stdClass Object
(
[id] => 2025
[pricing_id] => 2025
[package_id] => 169
[term] => 3
[period] => year
[price] => 45.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[3] => stdClass Object
(
[id] => 2026
[pricing_id] => 2026
[package_id] => 169
[term] => 4
[period] => year
[price] => 60.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[4] => stdClass Object
(
[id] => 2027
[pricing_id] => 2027
[package_id] => 169
[term] => 5
[period] => year
[price] => 75.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[5] => stdClass Object
(
[id] => 2028
[pricing_id] => 2028
[package_id] => 169
[term] => 6
[period] => year
[price] => 90.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[6] => stdClass Object
(
[id] => 2029
[pricing_id] => 2029
[package_id] => 169
[term] => 7
1 [period] => year
[price] => 105.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[7] => stdClass Object
(
[id] => 2030
[pricing_id] => 2030
[package_id] => 169
[term] => 8
[period] => year
[price] => 120.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
[8] => stdClass Object
(
[id] => 2031
[pricing_id] => 2031
[package_id] => 169
[term] => 9
[period] => year
[price] => 135.0000
[setup_fee] => 0.0000
[cancel_fee] => 0.0000
[currency] => CAD
)
)
)
我想在数组中循环,只返回每个产品的价格,每个期限(1年,2年和3年),在表格中。
...但我甚至无法遍历数组。
以下是调用API并返回std对象的代码:
$api = new Api($url, $user, $key);
$model = "packages";
$method = "getAllPackages";
$params = array('package_group_id'=>'2');
$response = $api->get($model, $method, $params);
以下是我尝试的内容:
以下内容返回错误:"无法使用STDClass类型的对象作为数组"
for ($i=0;$i<sizeof($response);$i++) {
echo $response[$i]->pricing[$i]->price;
}
我也尝试过:
$i = 165;
$response->pricing[$i]->price;
我也尝试过:
//loop the response
foreach($response as $obj){
//check is it contains the pricing details
if(is_array($obj->pricing)){
//loop the price for each object
foreach($obj->pricing as $pricingObj){
//store the unique id and price
$data[$pricingObj->id] = $pricingObj->price;
}
}
}
//print the result
print_r($data);
我也尝试过:
foreach ($response as $item) {
foreach ($item->pricing as $pricing) {
var_dump($pricing->price);
}
}
......但仍然:没有什么对我有用。
感谢您对此的帮助!
答案 0 :(得分:0)
$api = new Api($url, $user, $key);
$model = "packages";
$method = "getAllPackages";
$params = array('package_group_id'=>'2');
$response = $api->get($model, $method, $params);
foreach($response as $resp)
{
foreach($resp->pricing as $key=>$pricing )
{
echo $pricing[$key]->price."<br>";
}
}
请用来打印每个学期的价格。
答案 1 :(得分:0)
您可以像这样循环响应。此代码仅适用于价格对象ID在整个对象中是唯一的。
<?php
$data = array();
$api = new Api($url, $user, $key);
$model = "packages";
$method = "getAllPackages";
$params = array('package_group_id'=>'2');
$response = $api->get($model, $method, $params);
//loop the response
foreach($response as $obj){
//check is it contains the pricing details
if(is_array($obj->pricing)){
//loop the price for each object
foreach($obj->pricing as $pricingObj){
//store the unique id and price
$data[$pricingObj->id] = $pricingObj->price;
}
}
}
//print the result
print_r($data);