2012-09-12 30 views
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]

我该如何显示数组的顺序?

+0

这是嵌套的数组,弄得一塌糊涂,你会得到更好的重构你的代码的对象 - 至少,我没有得到你想要的最终结果是什么。 – moonwave99

回答

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被称为比尔和POS1工程; POS2,POS3
用户有14名为吉尔和pos134

工作
+0

它看起来像我需要的!我正在执行它。 – Hypn0tizeR

+0

有没有办法用'while($ row = blabla ...)'替换第一个'foreach'? – Hypn0tizeR

+1

@ Hypn0tizeR当然;我没有从样本数据中看到您是如何需要它的,但是如果您创建了手动计数器,则可以使用while循环进行迭代,而且如果您正在从数据库中读取数据也可以。 – newfurniturey