我今天下午试图解决这个问题:
如何检查此数组中的{from,to}元素是否相同?用语言:我需要知道如何在递归函数中匹配数组元素。
示例
此数组必须返回FALSE,因为$ array [4] [0] ['from']和$ array [4] [0] ['to']在所有$ array中都不相同[ 2]和$ array [3]。
Array
(
[4] => Array
(
[0] => Array
(
[from] => 1
[to] => 2.0000
[price] => 8.0000
)
[1] => Array
(
[from] => 2.0000
[to] => 4.0000
[price] => 6.0000
)
)
[2] => Array
(
[0] => Array
(
[from] => 1
[to] => 3.0000
[price] => 70.0000
)
[1] => Array
(
[from] => 3.0000
[to] => 5.0000
[price] => 60.0000
)
[2] => Array
(
[from] => 5.0000
[to] => 9.0000
[price] => 50.0000
)
)
[3] => Array
(
[0] => Array
(
[from] => 1
[to] => 2.0000
[price] => 25.0000
)
[1] => Array
(
[from] => 2.0000
[to] => 4.0000
[price] => 20.0000
)
[2] => Array
(
[from] => 4.0000
[to] => 6.0000
[price] => 15.0000
)
)
)
此数组必须返回TRUE,因为$ array [4] [0] ['from']和$ array [4] [0] ['to']在所有$ array中相同[2] ]和$ array [3]。
Array
(
[4] => Array
(
[0] => Array
(
[from] => 1
[to] => 3.0000
[price] => 7.0000
)
[1] => Array
(
[from] => 3.0000
[to] => 5.0000
[price] => 6.0000
)
[2] => Array
(
[from] => 5.0000
[to] => 9.0000
[price] => 5.0000
)
)
[2] => Array
(
[0] => Array
(
[from] => 1
[to] => 3.0000
[price] => 70.0000
)
[1] => Array
(
[from] => 3.0000
[to] => 5.0000
[price] => 60.0000
)
[2] => Array
(
[from] => 5.0000
[to] => 9.0000
[price] => 50.0000
)
)
[3] => Array
(
[0] => Array
(
[from] => 1
[to] => 3.0000
[price] => 170.0000
)
[1] => Array
(
[from] => 3.0000
[to] => 5.0000
[price] => 160.0000
)
[2] => Array
(
[from] => 5.0000
[to] => 9.0000
[price] => 150.0000
)
)
)
我想得到一个真或假值。
答案 0 :(得分:2)
试试这个(没有经过测试,绝对不是最好的方法):
$match = true;
foreach ($main_array as $arrays)
{
foreach($arrays as $key => $array)
{
$from = $array['from'];
$to = $array['to'];
foreach($main_array as $tmp_array)
{
if($tmp_array[$key]['from'] != $from || $tmp_array[$key]['to'] != $to) {
$match = false;
break 3;
}
}
}
}
return $match;
答案 1 :(得分:1)
这是我的例子,我已经测试过了。在这里,您可以通过可自定义字段和可自定义的$ depth
来比较数组数组主要功能:
function array_compare(array $haystack, array $to = array(), array &$matches = array(), $depth = 1)
{
$total = 0;
if ($depth === 1) // We're in right depth, let's check the elements
{
foreach ($haystack as $key => $value)
{
$match = true;
foreach ($to as $to_key => $to_value) // Check every key
{
if (!isset($value[$to_key]) || $value[$to_key] !== $to_value)
{
$match = false; // If at least one doesn't match - break and block adding to $matches
break;
}
}
if ($match)
{
$matches[] = $value; // OK, match, let's add to matches.
}
$total++; // Increase total searched elements
}
}
else // We're not on the right depth level
{
foreach ($haystack as $key => $value)
{
$total += array_compare($value, $to, $matches, $depth - 1); // let's gather into
}
}
return $total; // return total searched
}
在这里你怎么能用它:
$test = array(
0 => array(
0 => array(
'from' => 1,
'to' => 3,
'value' => 25,
),
1 => array(
'from' => 1,
'to' => 3,
'value' => 25,
)
),
1 => array(
0 => array(
'from' => 2,
'to' => 5,
'value' => 25,
),
1 => array(
'from' => 1,
'to' => 3,
'value' => 25,
)
),
2 => array(
1 => array(
'from' => 1,
'to' => 15,
'value' => 25,
)
),
);
$matches = array();
$total = array_compare($test, array('from' => 1, 'to' => 3), $matches, 2);
var_dump($matches, $total);
如果计数($匹配)=== $ total;
,则必须如此希望这是你所需要的;