I am having this array :
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
)
)
)
I would like to loop through the array, and only return the price of each product, for each term (1 year, 2 years and 3 years), in a table.
...but i am even not able to loop through the array.
Here is the code to call the API and return an std object :
$api = new Api($url, $user, $key);
$model = "packages";
$method = "getAllPackages";
$params = array('package_group_id'=>'2');
$response = $api->get($model, $method, $params);
Here is what i tried :
the following returned an error : "Cannot use object of type STDClass as array"
for ($i=0;$i<sizeof($response);$i++) {
echo $response[$i]->pricing[$i]->price;
}
I've also tried :
$i = 165;
$response->pricing[$i]->price;
Also i tried :
//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);
And i tried as well :
foreach ($response as $item) {
foreach ($item->pricing as $pricing) {
var_dump($pricing->price);
}
}
...but still : nothing is working for me.
Thank you for your help on this!
$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>";
}
}
please use to print price of each term.
You can loop the response like this. This code will works only the price object id is unique across the object.
<?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);