PHP Web服务结果和数组
我的数组仍然有问题,我不知道为什么。。。 我收到了来自web服务的响应,其中包含许多房间(0,1,2,3,4,5,…)和相关的租赁价值。 然后,我需要计算每个房间的数量(平均值) 所以我认为数组是最好的 但我正在努力创建一个或多个数组并填充它/它们 提前谢谢 代码是:PHP Web服务结果和数组,php,arrays,web-services,Php,Arrays,Web Services,我的数组仍然有问题,我不知道为什么。。。 我收到了来自web服务的响应,其中包含许多房间(0,1,2,3,4,5,…)和相关的租赁价值。 然后,我需要计算每个房间的数量(平均值) 所以我认为数组是最好的 但我正在努力创建一个或多个数组并填充它/它们 提前谢谢 代码是: // if the search is buy then search for rent as well to have an estimate value if ($_POST['ListingType'] ==
// 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>';
}
}
}
}
//如果搜索是buy,那么也搜索rent以获得估计值
如果($\u POST['ListingType']=='buy'){
//初始化数组
$estimate=数组();
$estimate['requestCountry]='UK';
$estimate['requestEncoding']='json';
$estimate['searchPlaceName']=$_POST['Location'];
$estimate['filterListingType']='rent';
如果(isset($\u POST['PropertyType']))
$estimate['filterPropertyType']=$\u POST['PropertyType'];
其他的
$estimate['filterPropertyType']='all';
如果(isset($_POST['PriceMax']))
$estimate['filterPriceMax']=$_POST['PriceMax'];
其他的
$estimate['filterPriceMax']='max';
如果(isset($_POST['PriceMin']))
$estimate['filterPriceMin']=$_POST['PriceMin'];
其他的
$estimate['filterPriceMin']='min';
如果(isset($_POST['roomMax']))
$estimate['filterBedroomMax']=$_POST['roomMax'];
其他的
$estimate['filterBedroomMax']='max';
如果(isset($_POST['roomMin']))
$estimate['filterBedroomMin']=$_POST['roomMin'];
其他的
$estimate['filterBedroomMin']='min';
$nestoriaEstimate=新Nestoria_Nestoria($estimate);
//打印($nestoriaEstimate->decodedData);
如果(!empty($nestoriaEstimate->decodedData->response)){
$idxEst=0;
$rentEst=array();
foreach($nestoriaEstimate->decodedData->response->listingas$listingse){
开关($listingEst->卧室号码){
回显“租金”。$listingse->price_格式。
卧室。$listingse->卧室编号。';
}
}
}
}
将显示:
每月租金450英镑
卧室(s)1
每月租金295英镑
卧室(s)1
每月租金545英镑
卧室(s)2
每月租金650英镑
卧室(s)2
每月租金395英镑
卧室(s)1
每月租金425英镑
卧室(s)1
每月租金500英镑
卧室(s)1
每月租金995英镑
卧室(s)2
每月租金395英镑
卧室(s)1
每月租金535英镑
卧室(s)1
每月租金695英镑
卧室(s)2
每月租金450英镑
卧室(s)2
每月租金475英镑
卧室(s)2
每月租金375英镑
卧室1一维阵列的基本工作原理如下: 数组中的每个“条目”都有一个键/值对 例如:
0 - Dog
1 - Cat
2 - Mouse
pet - dog
job - programmer
hobby - kite flying
请注意,默认情况下,第一个索引(键)为0
通过将键插入数组变量的括号中,可以获得与键关联的值
例:
还有一种称为关联数组的数组类型,在这种情况下,索引不是数字,而是文本表示(字符串)
例如:
0 - Dog
1 - Cat
2 - Mouse
pet - dog
job - programmer
hobby - kite flying
因此,要从这样的数组中提取值,只需使用括号中的文本表示
例:
出于您的目的,因为服务返回的是一个基于0的列表,所以您应该使用数字索引,如上所示。要向数组中添加新的键/值对,可以循环结果集并执行以下操作:
$myArray[] = $myValue;
循环的每次迭代都将添加新的键/值对(仅从0开始递增键)
编辑(根据您的评论):
要在数组上循环,可以使用for循环或foreach循环
例如:
$studio = Array(200,250,240,150);
foreach($studio as $key => $val)
echo $studio[$key]."<br />";
$studio=Array(200250240150);
foreach($studio as$key=>$val)
echo$studio[$key]。“
”;
我们是否遗漏了一半的问题?这似乎是一个咆哮…如果我们能看到一个。你的代码是b。来自web服务的回复。我已经用代码和结果编辑了我的帖子。谢谢你的总结。但我还是很困惑,也许是因为时间晚了。。。我当时想的是要有一个阵列式的“工作室”,租金是$studio[200250240150],一套一居室的房子/公寓$oneBed[300350]。。。但是我在循环和给不同的数组赋值时遇到了问题。谢谢Christopher。对不起,我想我表达得不太好!如何将这些值放入数组中?让我们拿3个结果,房间数/租金:1-250,3-500,1-225。我想要一张床('250'、'200')和三张床('225')。对不起,你的问题没有多大意义。如果希望在数组中嵌套单独的键/值对,则需要使用多维数组。如果我理解您要做的事情(我很可能不理解,因为您没有很好地解释自己),您可能会更好地使用对象,因为它们可以包含和表示比数组更容易访问的数据。好的,明白了。。。我要去睡觉了!要指定一个值,请按您所说的操作:$myArray[]=$myValue;谢谢你,克里斯托弗。