[英]Sort 3d array into 2 seperate arrays php
I have an array just like the one below but with more people.我有一个类似于下面的数组,但有更多的人。
I want to get a separate array(s) for values such as age which would be [22, 13, 19], like a function I can call like this;-我想获得一个单独的数组,例如年龄为 [22, 13, 19] 的值,就像我可以这样调用的函数一样;-
sort($ageArray, "age");
function sort (newArray, value)
{
foreach (index in mainArray)
{
array_push(newArray, mainArray("age")
}
}
I know thats wrong but something like that would be nice我知道那是错的,但这样的事情会很好
Array ( [0] => Array (
[username] => Admin
[age] => 22
[gender] => M
[email] => Admin@gmail.com
[kills] => 10 [deaths] => 3
[score] => 33000
[posts] => 5 )
[1] => Array (
[username] => Jack23
[age] => 13
[gender] => M
[email] => Jack23@gmail.com
[kills] => 23
[deaths] => 22
[score] => 52000
[posts] => 0 )
[2] => Array (
[username] => LucySmith
[age] => 19
[gender] => F
[email] => Lucy2@hotmail.com
[kills] => 5
[deaths] => 52
[score] => 2400
[posts] => 0 ))
I managed to come up with this我设法想出了这个
$a = array_values($mainArray);
$b = array_values($a[0]);
echo $b[0];
which echos "admin", so I could in theory put that in a loop to echo all the usernames and put them in the array but how would I do it for stuff like age, or posts?这与“admin”相呼应,因此理论上我可以将其放入循环中以回显所有用户名并将它们放入数组中,但是对于年龄或帖子之类的内容我将如何做?
Based on the manual:- http://php.net/manual/en/function.array-column.php基于手册:- http://php.net/manual/en/function.array-column.php
you need to use array_column()
like below:-您需要使用array_column()
如下所示:-
$ages = array_column($myArray, 'age');
Also you can do it like below(@AbraCadaver suggested):-你也可以像下面这样做(@AbraCadaver建议):-
$ages = array_column($dataArray, 'age', 'username');
So you can do echo $ages['Admin'];
所以你可以做echo $ages['Admin'];
Example output:- https://eval.in/737005示例输出:- https://eval.in/737005
You can get result using foreach
loop.您可以使用foreach
循环获得结果。
foreach($ageArray as $array)
{
$age[] = $array['age'];
}
print_r($age);//prints array of ages
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.