Array (
[0] => Array ([username] => khaled [0] => khaled)
[1] => Array ([username] => Nariman [0] => Nariman)
[2] => Array ([username] => test1 [0] => test1)
[3] => Array ([username] => jack [0] => jack)
[4] => Array ([username] => mmmm [0] => mmmm)
[5] => Array ([username] => qqq [0] => qqq)
[6] => Array ([username] => wwwdwd [0] => wwwdwd)
[7] => Array ([username] => wddww [0] => wddww)
[8] => Array ([username] => maxa [0] => maxa)
)
I tried $posts['username'][0]/[0]['username']
... didnt work!
我试着美元的文章(“用户名”)[0]/[0](“用户名”)……没有工作!
I want to print out some values of the array, like the username for example. How to do it?
我想打印一些数组的值,比如用户名。如何去做?
3 个解决方案
#1
1
You can use the following solution using foreach
:
使用foreach,您可以使用以下解决方案:
foreach ($arr as $arrItem) {
echo $arrItem['username'];
echo $arrItem[0];
}
You can also use a for
loop:
你也可以使用for循环:
for ($i = 0; $i < count($arr); $i++) {
echo $arr[$i]['username'];
echo $arr[$i][0];
}
demo: https://ideone.com/he6h7r
演示:https://ideone.com/he6h7r
#2
2
To get a single variable (so your username in your case), you do the following:
要获得一个变量(因此,您的用户名在您的示例中),您需要执行以下操作:
$username = $posts[0]['username'];
This will get the username from the first nested Array in your actual array. You can modify this to get every username from each nested array by making a for loop so you get the rest of the usernames.
这将从实际数组中的第一个嵌套数组中获取用户名。您可以通过创建一个for循环来修改它,以从每个嵌套数组中获取每个用户名,这样您就可以获得其余的用户名。
#3
1
On you array, key ['username'] does not exist.
在您的数组中,键['username']不存在。
If you indent the array, they read as this:
如果对数组进行缩进,它们会如下所示:
Array
[0]
[username] => khaled
[0] => khaled
[1]
[username] => Nariman
[0] => Nariman
...
Because this, you can read $posts[0][0] or $posts[0][username], but, you cant read $posts[username] because didn't exist.
因为,你可以读取$posts[0][0]或$posts[0][username],但是,你不能读取$posts[username],因为不存在。
#1
1
You can use the following solution using foreach
:
使用foreach,您可以使用以下解决方案:
foreach ($arr as $arrItem) {
echo $arrItem['username'];
echo $arrItem[0];
}
You can also use a for
loop:
你也可以使用for循环:
for ($i = 0; $i < count($arr); $i++) {
echo $arr[$i]['username'];
echo $arr[$i][0];
}
demo: https://ideone.com/he6h7r
演示:https://ideone.com/he6h7r
#2
2
To get a single variable (so your username in your case), you do the following:
要获得一个变量(因此,您的用户名在您的示例中),您需要执行以下操作:
$username = $posts[0]['username'];
This will get the username from the first nested Array in your actual array. You can modify this to get every username from each nested array by making a for loop so you get the rest of the usernames.
这将从实际数组中的第一个嵌套数组中获取用户名。您可以通过创建一个for循环来修改它,以从每个嵌套数组中获取每个用户名,这样您就可以获得其余的用户名。
#3
1
On you array, key ['username'] does not exist.
在您的数组中,键['username']不存在。
If you indent the array, they read as this:
如果对数组进行缩进,它们会如下所示:
Array
[0]
[username] => khaled
[0] => khaled
[1]
[username] => Nariman
[0] => Nariman
...
Because this, you can read $posts[0][0] or $posts[0][username], but, you cant read $posts[username] because didn't exist.
因为,你可以读取$posts[0][0]或$posts[0][username],但是,你不能读取$posts[username],因为不存在。