0

以下是变量:

$ids = array_unique($_POST['id']);
$total_ids = count($ids);

$name = $_POST['name'];

$positions = $_POST['position'];
$total_positions = count($positions);

这是 print_r 显示的内容:

[id] => Array ( 
    [0] => 3 
    [1] => 7 ) 

[name] => Array ( 
    [0] => George 
    [1] => Barack ) 

[position] => Array ( 
    [1] => Array ( 
        [0] => 01 
        [1] => 01 ) 
    [2] => Array ( 
        [0] => 01 
        [1] => 01 
        [2] => 01 ) )

这是我想在刷新/提交时得到的结果:

[id][0];
    [name][0];
        [position][1][0];[position][1][1]
[id][1];
    [name][1];
        [position][2][0];[position][2][1];[position][2][2]

为了使想要的结果很清楚:

User with [id][0] 
    is called [name][0] 
        and works at [position][1][0];[position][1][1]
BUT

User with [id][1]
    is called [name][1];
        and works at [position][2][0];[position][2][1];[position][2][2]

请注意,[position]s 以 开头[1],而不是[0]

我怎样才能按我显示的顺序显示数组?

4

1 回答 1

1

我不是 100% 确定我了解您的需求,但是为了尝试匹配您显示的输出的最终示例,我想出了以下内容:

// iterate through each of the `$ids` as a "user"
foreach ($ids as $key => $value) {
    // output the user's ID
    echo 'User with ' . $value;
    if (isset($name[$key])) {
        // output the user's name
        echo ' is called ' . $name[$key];
    }
    if (isset($position[$key + 1])) {
        // output a ';'-delimited list of "positions"
        echo ' and works at ';
        $positions = '';
        // the `$positions` array starts with index 1, not 0
        foreach ($position[$key + 1] as $pos) {
            $positions .= (($positions != '') ? ';' : '') . $pos;
        }
        echo $positions;
    }
    echo '<br />';
}

这将提供类似于以下内容的输出:

拥有 1 的用户称为 Bill,在 pos1;pos2;pos3 工作
拥有 14 的用户称为 Jill,在 pos134 工作

于 2012-09-12T23:24:56.677 回答