为了账号安全,请及时绑定邮箱和手机立即绑定

我有三个数组:源、目标和费用。我想收取特定来源和目的地的费用

我有三个数组:源、目标和费用。我想收取特定来源和目的地的费用

PHP
素胚勾勒不出你 2022-07-16 18:13:08
source = ["hyderabd","banglore","hyderabad","chennai","pune","mumbai"];destination = ["pune","chennai","mumbai","hyderabad","banglore","hyderabad"];prices = ["500","300","400","600","500","500"];我有源变量和目标变量:src="hyderabad" & des="mumbai"我想从价格数组中获得从海得拉巴到孟买的 400 的价格。我怎样才能得到这个?
查看完整描述

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

)


查看完整回答
反对 回复 2022-07-16
?
慕少森

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;


查看完整回答
反对 回复 2022-07-16
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

你可以做这样的事情来找到一个元素的索引

echo $prices[array_search("hyderabad", $source)]; // 400

然而,这似乎相当随机。如果源条目和目标条目的索引与价格不同,会发生什么情况?来源和目的地与价格有何关系?你到底想在这里实现什么?


查看完整回答
反对 回复 2022-07-16
?
墨色风雨

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);

?>


````


查看完整回答
反对 回复 2022-07-16
  • 4 回答
  • 0 关注
  • 166 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信