I’m still having problems with arrays, I don’t know why …
I’m getting response from web services with a number of room (0,1,2,3,4,5,…) and a renting value associated.
I will then need to do some calculation (average) per number of room.
So I thought that an array would be best.
But I’m struggling to create the array or arrays and populate it/them …
Thanks in advance.
The code is:
// if the search is buy then search for rent as well to have an estimate value
if ($_POST['ListingType'] == 'buy'){
// Initialise the array
$estimate = array();
$estimate['requestCountry'] = 'UK';
$estimate['requestEncoding'] = 'json';
$estimate['searchPlaceName'] = $_POST['Location'];
$estimate['filterListingType'] = 'rent';
if(isset($_POST['PropertyType']))
$estimate['filterPropertyType'] = $_POST['PropertyType'];
else
$estimate['filterPropertyType'] = 'all';
if(isset($_POST['PriceMax']))
$estimate['filterPriceMax'] = $_POST['PriceMax'];
else
$estimate['filterPriceMax'] = 'max';
if(isset($_POST['PriceMin']))
$estimate['filterPriceMin'] = $_POST['PriceMin'];
else
$estimate['filterPriceMin'] = 'min';
if(isset($_POST['roomMax']))
$estimate['filterBedroomMax'] = $_POST['roomMax'];
else
$estimate['filterBedroomMax'] = 'max';
if(isset($_POST['roomMin']))
$estimate['filterBedroomMin'] = $_POST['roomMin'];
else
$estimate['filterBedroomMin'] = 'min';
$nestoriaEstimate = new Nestoria_Nestoria($estimate);
//print_r( $nestoriaEstimate->decodedData);
if (!empty($nestoriaEstimate->decodedData->response)) {
$idxEst=0;
$rentEst = array();
foreach($nestoriaEstimate->decodedData->response->listings as $listingEst) {
switch ($listingEst->bedroom_number) {
echo '<div>Rent' . $listingEst->price_formatted . '<br/>Bedroom(s)' . $listingEst->bedroom_number . '</div>';
}
}
}
}
Which will display:
Rent450 GBP per month
Bedroom(s)1
Rent295 GBP per month
Bedroom(s)1
Rent545 GBP per month
Bedroom(s)2
Rent650 GBP per month
Bedroom(s)2
Rent395 GBP per month
Bedroom(s)1
Rent425 GBP per month
Bedroom(s)1
Rent500 GBP per month
Bedroom(s)1
Rent995 GBP per month
Bedroom(s)2
Rent395 GBP per month
Bedroom(s)1
Rent535 GBP per month
Bedroom(s)1
Rent695 GBP per month
Bedroom(s)2
Rent450 GBP per month
Bedroom(s)2
Rent475 GBP per month
Bedroom(s)2
Rent375 GBP per month
Bedroom(s)1
Single dimensional arrays basically work as follows:
You have a key/value pair for each “entry” in the array.
an example would be:
Notice that by default the first index (key) is 0.
You can obtain the value associated with the key by inserting the key into the brackets for the array variable.
ex:
there is also an array type called an associative array in which case the indices are not numeric, but rather have textual representation (string).
an example would be:
so to pull a value out of an array like this, you would just use the textual representation in the brackets
ex:
for your purposes, since the service is returning a 0 based list, you should use numeric indices as I showed above. to add new key/value pairs to your array, you can loop over the result set and do something like this:
where each iteration of the loop will add the new key/value pair (just incrementing the keys from 0).
EDIT (per your comment):
to loop over an array you can use a for loop or a foreach loop.
foreach example: