Php 使用所有数组中的值并跳过其余的
我有一个包含3个子数组的数组,如:Php 使用所有数组中的值并跳过其余的,php,arrays,Php,Arrays,我有一个包含3个子数组的数组,如: $array = array( width => array( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5 ), height => array( 0 => 1, 1 => 2, 2 => 7, 3 =
$array = array(
width => array(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5
),
height => array(
0 => 1,
1 => 2,
2 => 7,
3 => 8
),
color => array(
0 => 2,
1 => 7,
2 => 8
)
);
在本例中,计数是3,因为我有3个数组。这有时或多或少,这就是为什么我有一个计数
现在我想找出哪一个数字在所有3个数组中,并且只使用它们。在上面的示例中,唯一返回的数字应该是2,因为它存在于所有3个子数组中
我试过下面这样的方法,但我真的被卡住了什么是最好的方法
$i = count($array); // gives me back the count of 3 which is correct
$n = 0;
foreach($array as $key=>values) {
foreach($values as $value) {
// do not how to proceed :(
}
}
您可以使用所有子数组作为参数调用
array\u intersect
$common_values = call_user_func_array('array_intersect', $array);
您可以使用所有子数组作为参数调用
array\u intersect
$common_values = call_user_func_array('array_intersect', $array);
也许您可以影响某些
var
中的行,并在foreach
比如:
<?php
$width = 0;
$height = 0;
$color = 0 ;
$arrayBundleWidth = array();
$arrayBundleHeight = array();
$arrayBundleColor = array();
foreach ($array as $arrayRow){
$arrayBundleWidth = $arrayRow['width'];
$arrayBundleColor = $arrayRow['color'];
$arrayBundleHeight = $arrayRow['height'];
foreach ($arrayBundleWidth as $arrayBundleWidthtRow) {
# code...
#you got each index of your width array here
}
foreach ($arrayBundleHeight as $arrayBundleHeightRow) {
# code...
#you got each index of your height array here
}
foreach ($arrayBundleColor as $arrayBundleColorRow) {
# code...
#you got each index of your color array here
}
}
?>
试试看,告诉我?这是你想要的吗?
不知道这是不是你想做的 也许您可以在一些
var
中影响您的行,并在foreach
比如:
<?php
$width = 0;
$height = 0;
$color = 0 ;
$arrayBundleWidth = array();
$arrayBundleHeight = array();
$arrayBundleColor = array();
foreach ($array as $arrayRow){
$arrayBundleWidth = $arrayRow['width'];
$arrayBundleColor = $arrayRow['color'];
$arrayBundleHeight = $arrayRow['height'];
foreach ($arrayBundleWidth as $arrayBundleWidthtRow) {
# code...
#you got each index of your width array here
}
foreach ($arrayBundleHeight as $arrayBundleHeightRow) {
# code...
#you got each index of your height array here
}
foreach ($arrayBundleColor as $arrayBundleColorRow) {
# code...
#you got each index of your color array here
}
}
?>
试试看,告诉我?这是你想要的吗?
不知道这是不是你想做的 比我的简洁多了,+1比我的简洁多了,+1