Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP组合单数组_Php_Arrays - Fatal编程技术网

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