将一个bash数组作为argv传递给php

将一个bash数组作为argv传递给php

问题描述:

I am passing a shell script array to php file as below.

php file.php ${variable[@]}

The file.php is used to find all the permutations of the given shell script array.

<?php
function pc_array_power_set($array) {
    // initialize by adding the empty set
    $results = array(array( ));

    foreach ($array as $element)
        foreach ($results as $combination)
            array_push($results, array_merge(array($element), $combination));
    return $results;
}
$set = $argv;
$power_set = pc_array_power_set($set);
foreach (pc_array_power_set($set) as $combination) {
    if (2 == count($combination)) {
        print join("\t", $combination) . "
";
    }
}
?>

However, since I use argv as the command line argument for the php file, my output is considering the file name also as an element of the array.

Output:

echo ${variable[@]}
php checking
php file.php ${variable[@]}

The output is coming as,

php done.php
checking done.php
checking php

As we can see, I get the file name also in the output while I just expect the output as,

checking php

我将shell脚本数组传递给php文件,如下所示。 p>

  php file.php $ {variable [@]} 
  code>  pre> 
 
 

file.php用于查找 给定shell脚本数组的所有排列。 p>

 &lt;?php 
function pc_array_power_set($ array){
 //通过添加空集初始化
 $ results = array(array()); 
  
 \ foreach($ array as $ element)
 foreach($ results as $ combination)
 array_push($ results,array_merge(array($ element),$ combination)); 
返回$ results; 
} \  n $ set = $ argv; 
 $ power_set = pc_array_power_set($ set); 
foreach(pc_array_power_set($ set)as $ combination){
 if(2 == count($ combination)){
 print join(  “\ t”,$组合)。  “
”; 
} 
} 
?&gt; 
  code>  pre> 
 
 

但是,因为我使用argv作为php文件的命令行参数, 输出正在考虑文件名也作为数组的元素。 p>

输出: strong> p>

  echo $ {variable [@]} 
 nphp checking 
php file.php  $ {variable [@]} 
  code>  pre> 
 
 

输出结果为, p>

  php done.php 
checking  done.php 
checking php 
  code>  pre> 
 
 

正如我们所看到的,我在输出中也得到了文件名,而我只希望输出为, p> \ n

 检查php 
  code>  pre> 
  div>

Simply perform an array_shift() on the array in your PHP script, before using it, to discard the first element:

$set = $argv;
array_shift($set);
$power_set = pc_array_power_set($set);