4 回答
TA贡献1811条经验 获得超4个赞
您可以使用 foreach 循环执行此操作:
$source = ["hyderabd","banglore","hyderabad","chennai","pune","mumbai"];
$destination = ["pune","chennai","mumbai","hyderabad","banglore","hyderabad"];
$prices = ["500","300","400","600","500","500"];
foreach($prices as $key => $price) {
// Array keys as string for source and destination.
$srcToDest = $source[$key] . " -> " . $destination[$key];
$charges[$srcToDest] = $price;
}
echo "<pre>"; print_r($charges);
输出将是:
Array
(
[hyderabd -> pune] => 500
[banglore -> chennai] => 300
[hyderabad -> mumbai] => 400
[chennai -> hyderabad] => 600
[pune -> banglore] => 500
[mumbai -> hyderabad] => 500
)
TA贡献2019条经验 获得超9个赞
如果您可以控制来源、目的地和价格,您可能应该以不同的格式存储它们,而不是三个单独的数组。您可能可以这样存储:
$prices[$source][$destination] = $price;
因此,根据您的输入,这将变为:
$prices['hyderabad']['pune'] = 500;
$prices['hyderabad']['mumbai'] = 400;
$prices['banglore']['chennai'] = 300;
$prices['chennai']['hyderabad'] = 600;
$prices['pune']['banglore'] = 500;
$prices['mumbai']['hyderabad'] = 500;
TA贡献1818条经验 获得超3个赞
你可以做这样的事情来找到一个元素的索引
echo $prices[array_search("hyderabad", $source)]; // 400
然而,这似乎相当随机。如果源条目和目标条目的索引与价格不同,会发生什么情况?来源和目的地与价格有何关系?你到底想在这里实现什么?
TA贡献1853条经验 获得超6个赞
使用 array_column() 你得到你的结果
$all[]=$source = ["hyderabd","banglore","hyderabad","chennai","pune","mumbai"];
$all[]=$destination = ["pune","chennai","mumbai","hyderabad","banglore","hyderabad"];
$all[]=$prices = ["500","300","400","600","500","500"];
$search = array_column($all, '2');
print_r($search);
?>
````
- 4 回答
- 0 关注
- 166 浏览
添加回答
举报