PHP组合单数组
我不知道如何确切地解释我想做什么,但我试着用一个例子来解释PHP组合单数组,php,arrays,Php,Arrays,我不知道如何确切地解释我想做什么,但我试着用一个例子来解释 $products = array("35","37","43"); 假设我有上面的数组,我如何创建一个如下所示的结果数组 $related_products = array ( array (35,37), array (35,43), array (37,35), array (37.43), array (43,35), array (43, 37) ) 您可以使用两个循环来捕获所有组合: $products =
$products = array("35","37","43");
假设我有上面的数组,我如何创建一个如下所示的结果数组
$related_products = array (
array (35,37),
array (35,43),
array (37,35),
array (37.43),
array (43,35),
array (43, 37) )
您可以使用两个循环来捕获所有组合:
$products = array("35","37","43");
$result = array();
for($i = 0; $i < count($products); $i++) {
for($j = 0; $j < count($products); $j++) {
if($i !== $j) {
$result[] = array(
$products[$i],
$products[$j]
);
}
}
}
print_r($result);
您只需使用array_push方法将一个项添加到主数组中,如下所示
$products = array("35","37","43");
$data = array();
for($i = 0; $i < count($products); $i++) {
for($j = 0; $j < count($products); $j++) {
if($i !== $j) {
array_push($data,array($products[$i],$products[$j]));
);
}
}
}
print_r($data);
$products=数组(“35”、“37”、“43”);
$data=array();
对于($i=0;$i
$products = array("35","37","43");
$data = array();
for($i = 0; $i < count($products); $i++) {
for($j = 0; $j < count($products); $j++) {
if($i !== $j) {
array_push($data,array($products[$i],$products[$j]));
);
}
}
}
print_r($data);