d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Php Foreach
Add Reply New Topic New Poll
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Oct 28 2015 10:24pm
I am extremely rusty on my php. I am working with an API and in the response it returns a Rates array object.

The Rates object is an array holding Rate(s) which each hold information such as Amount and PackageType. I am trying to save the Rate object which has Amount=x and PackageType=y.


This code outputs what I want to see, but it is not at all what I want to program.
Code
foreach($Rates as $key => $value)
{
echo $value[0]->Amount . "===" . $value[0]->PackageType;
echo $value[1]->Amount . "===" . $value[1]->PackageType;
echo $value[2]->Amount . "===" . $value[2]->PackageType;
}


This is the pseudo-code for what I need in php:
Code
for($i=0;$Rates;$i++)
{
$Rate = $Rates[$i];
if($Rate->Amount == x && $Rate->PackageType == y)
{
return $Rate;
}
}


Thanks for helping out a noob...
Member
Posts: 4,977
Joined: Dec 5 2014
Gold: 25,000.14
Nov 1 2015 10:48am
PHP Rule 1: Never start Big initials letter for variable..

Code
/* foreach example 3: key and value */

$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);

foreach ($a as $k => $v) {
echo "\$a[$k] => $v.\n";
}


This post was edited by RubyRose on Nov 1 2015 10:48am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll