2013-10-24 111 views
-3

我的陣列如下所示,我如何顯示消息和任何鍵的值。PHP關聯子陣列

(
    [@@warning_count] => 2 

    [0] => Array 
    (
     [Level] => Warning 
     [Code] => 1366 
     [Message] => Incorrect integer value: ‘Johny’ for column ‘billing_cycle’ at row 1 
    ) 

    [1] => Array 
    (
    [Level] => Error 
    [Code] => 1062 
    [Message] => Duplicate entry ‘peace′ for key ‘groupname’ 
    ) 

) 
+0

那你試試?看來你的功課.... – Sal00m

回答

1

嘗試....

<?php 

$my_array = array ('@@warning_count' => 2, 
      0 => Array (
       'Level' => 'Warning', 
       'Code' => '1366', 
       'Message' => 'Incorrect integer value: ‘Johny’ for column ‘billing_cycle’ at row 1', 
      ), 
     1 => Array(
      'Level' => 'Error', 
      'Code' => '1062', 
      'Message' => 'Duplicate entry ‘peace′ for key ‘groupname’', 
      ), 
); 


foreach ($my_array as $arr_key => $arr_val){ 

    if(is_array($arr_val) == true){ 
     echo $arr_val['Message']; 
    } 
} 

?> 

讓我知道上面的代碼......

+0

謝謝@Mandip Darji它的工作原理 –

0
foreach($arr as $val) { 
    echo $val['Level'] . "\n"; 
    echo $val['Code'] . "\n"; 
    echo $val['Message'] . "\n"; 
} 

或下面的代碼

foreach($arr as $val) { 
    for($val as $innerKey => $innerValue) { 
     echo $innerKey . ' == ' . $innerValue . "\n"; 
    } 
} 
0

這是結果你想要實現?

<?php 
    $dataArray = Array(
     array(
      'Level' => 'Warning', 
      'Code' => '1366', 
      'Message' => 'Message here 1', 
      ), 

      array(
      'Level' => 'Warning', 
      'Code' => '1062', 
      'Message' => 'Message here 2', 
      ), 

      array(
      'Level' => 'Warning', 
      'Code' => '3233', 
      'Message' => 'Message here 3', 
      ) 

    ); 

    // This shows the whole array: 
    echo '<pre>'; 
    var_dump($dataArray); 
    echo '</pre>'; 


    // Select only the messages: 
    foreach ($dataArray as $key => $value) { 
     echo $value['Message'].'<br />'; 
    } 
?> 
+0

感謝您從各個一個響應。 –

0

我想你想要這樣的東西。參考the working code

<?php 
$myArray = array(
     '@@warning_count' => 2, 
     array(
      'level' => 'Warning', 
      'code' => 1266, 
      'message' => "Incorrect integer value: ‘Johny’ for column ‘billing_cycle’ at row 1"), 
     array(
      'level' => 'Warning', 
      'code' => 1266, 
      'message' => "Incorrect integer value: ‘Johny’ for column ‘billing_cycle’ at row 1"), 
); 

print "Warning Count: {$myArray['@@warning_count']}\n"; 
print "------------------\n\n"; 
foreach ($myArray as $key => $value){ 
    if (is_array($value)){ 
     foreach ($value as $subKey => $subValue){ 
      print $subKey . "=>" . $subValue . "\n"; 
     } 
     print "------------------\n"; 
    } 
} 
+0

謝謝@Bubba這可以幫助我理解處理數組 –