我嘗試了很多方法來打印出一個特定的值,例如使用array_column,但沒有任何工作。如何在多維數組中打印特定值
我的數組
$items = [array('content' => 'test1','price' => 500,'itemno' => '1'),array('content' => 'test2','price' => 300,'itemno' => '2'),array('content' => 'test3','price' => 100,'itemno' => '3')];
或
Array
(
[0] => Array
(
[content] => test1
[price] => 500
[itemno] => 1
)
[1] => Array
(
[content] => test2
[price] => 300
[itemno] => 2
)
[2] => Array
(
[content] => test3
[price] => 100
[itemno] => 3
)
PHP代碼
foreach ($items as $key => $item) {
foreach ($item as $item1) {
echo $item1['content'] ;
}}
每一次我嘗試使用它的鍵打印值我得到這些錯誤
警告: 中的非法字符串偏移'內容'第28行中的C:\ xampp \ htdocs \ test \ classes \ index.php n警告:非法 C:\ xampp \ htdocs \ test \ classes中的字符串偏移'content' \ index.php on line 28 1警告: 中的非法字符串偏移'內容'第28行中的C:\ xampp \ htdocs \ test \ classes \ index.php警告:非法 C:\中的字符串偏移量'content'line 28警告: 中的非法字符串偏移「內容」第28行的C:\ xampp \ htdocs \ test \ classes \ index.php警告:非法 字符串偏移量'content'在C:\ xampp \ htdocs \ test \ classes \ index.php上 line 28 3
你根本不需要第二個循環:'foreach($ items as $ item){echo $ item ['content']; }' –
它工作。我覺得我好笨。無論如何,它是如何使用一個循環,而有兩個數組? – jack
'$ item'是具有'content'鍵的下一個數組。 'item ['content']'在'content'鍵下獲取下一個數組的值。 –