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

时间:2014-04-08 22:12:35

标签: php bash

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

php file.php ${variable[@]}

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

<?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) . "\n";
    }
}
?>

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

输出:

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

输出即将出现,

php done.php
checking done.php
checking php

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

checking php

1 个答案:

答案 0 :(得分:3)

在使用PHP脚本之前,只需在PHP脚本中对数组执行array_shift()即可丢弃第一个元素:

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