2016-12-01 137 views
1

我希望有人能幫助我。我讀了很多關於數組的知識,並嘗試了很多找到的示例,但我無法讓我的代碼正常工作。循環通過多維數組和輸出值

首先創建我的數組是這樣的:

$aryUsers= array (
    'users' => 
     array() 
); 

比我從我的分貝一些數據,我已把我的數組是這樣的:

$user = array("ID" => $ID, "User" => $User, "val" => $val); 
$aryUsers["users"][] = $user; 

如果我現在的print_r我的陣列,它看起來像這樣:

Array ([users] => Array 
    ( 
     [0] => Array ([ID] => 696 [User] => user 1 [val] => 341) 
     [1] => Array ([ID] => 708 [User] => user 2 [val] => 218) 
     [2] => Array ([ID] => 670 [User] => user 3 [val] => 73) 
     [3] => Array ([ID] => 718 [User] => user 4 [val] => 66) 
     [4] => Array ([ID] => 698 [User] => user 5 [val] => 66) 
     [5] => Array ([ID] => 721 [User] => user 6 [val] => 60) 
     [6] => Array ([ID] => 707 [User] => user 7 [val] => 60) 
     [7] => Array ([ID] => 720 [User] => user 8 [val] => 141) 
    ) 
) 

,如果我嘗試OUTP UT斯達康我陣列與此代碼(或其他我試過),然後我得到的只是虛無

$iterations = count($aryUsers[$keys[0]]); 
for($i = 0; $i < $iterations; $i++) { 
    foreach($aryUsers as $key => $value) { 
     echo $value[$i]; 
    } 
} 

什麼,我做錯了什麼?或者我正在做一些正確的事情? 謝謝你的閱讀!

+0

你爲什麼這樣做'$迭代=計數($ aryTeilnehmer [$鍵[0]);'?這與'$ aryUsers'有什麼關係? –

+0

哦對不起,我輸入錯誤:$ iterations = count($ aryUsers [$ keys [0]]); 我這樣做要知道我必須循環多少次:/ – egolive

+0

您不需要使用for循環。您可以簡單地使用'foreach($ aryUsers ['users'] as $ user){...}' –

回答

2

嗨給你的示例數據數據結構。

$arrayUsers = array ( 'users' => array ( 0 => array ('ID' => 696, 'User' => 'user 1', 'val' => 341), 1 => array ('ID' => 708, 'User' => 'user 2', 'val' => 218), 2 => array ('ID' => 670, 'User' => 'user 3', 'val' => 73), 3 => array ('ID' => 718, 'User' => 'user 4', 'val' => 66), 4 => array ('ID' => 698, 'User' => 'user 5', 'val' => 66), 5 => array ('ID' => 721, 'User' => 'user 6', 'val' => 60), 6 => array ('ID' => 707, 'User' => 'user 7', 'val' => 60), 7 => array ('ID' => 720, 'User' => 'user 8', 'val' => 141), ) );

foreach ($arrayUsers as $arrayUser) { foreach ($arrayUser as $arrayUser) { echo $arrayUser['ID']. ' '. $arrayUser['User'] . ' ' . $arrayUser['val'].'<br>'; } }

+0

這工作完美:)謝謝! – egolive