$travels = [
0 => ['departure' => 'Paris', 'arrival' => 'Nantes'],
1 => ['departure' => 'Orleans', 'arrival' => 'Nantes'],
2 => ['departure' => 'Dublin', 'arrival' => 'Tours'],
3 => ['departure' => 'Paris', 'arrival' => 'Orleans'],
4 => ['departure' => 'Paris', 'arrival' => 'Nice'],
5 => ['departure' => 'Nice', 'arrival' => 'Nantes'],
6 => ['departure' => 'Nice', 'arrival' => 'Tours'],
7 => ['departure' => 'Tours', 'arrival' => 'Amboise'],
8 => ['departure' => 'Nice', 'arrival' => 'Nantes'],
];
所以这是我的代码,我想使用这个多个数组作为数据库,但没有查询对吗?
所以,我只是希望从一个出发点获得所有到达值,就像从巴黎开始的所有到达值一样,我已经在foreachloop中使用foreach循环尝试了很多东西,如果使用索引数组但是没有机会。有没有办法像mysql中的查询一样。
会是这样的:
"SELECT 'arrival' FROM 'travels' WHERE 'departure' = Paris"
答案 0 :(得分:2)
据我了解你的问题,你可以这样做:
$resultArr = [];
array_walk($travels, function($value, $key) use (&$resultArr) {
if($value['departure'] == 'Paris')
$resultArr[$key] = $value;
});
这将填补你的新阵列与那些仅来自巴黎的离场。
另一种方法是使用mfonda在array_filter帖子中建议的answer:
$filtered = array_filter($travels, function($v) { return $v['departure'] == 'Paris'; });
答案 1 :(得分:0)
这将使所有到达的人在离开时出现' Paris':
<?php
$travels = [
0 => ['departure' => 'Paris', 'arrival' => 'Nantes'],
1 => ['departure' => 'Orleans', 'arrival' => 'Nantes'],
2 => ['departure' => 'Dublin', 'arrival' => 'Tours'],
3 => ['departure' => 'Paris', 'arrival' => 'Orleans'],
4 => ['departure' => 'Paris', 'arrival' => 'Nice'],
5 => ['departure' => 'Nice', 'arrival' => 'Nantes'],
6 => ['departure' => 'Nice', 'arrival' => 'Tours'],
7 => ['departure' => 'Tours', 'arrival' => 'Amboise'],
8 => ['departure' => 'Nice', 'arrival' => 'Nantes'],
];
foreach ($travels as $key => $value) {
if ($value['departure'] == 'Paris') {
echo $value['arrival'];
}
}
?>
这将列出所有到货,但是,如果您想限制只显示一次到货名称,您必须添加一些代码。