Is there a one-line solution (specific function) you can use to get a specific property of all Objects
in an Array
as:
是否有一个单行解决方案(特定函数)可用于获取Array中所有对象的特定属性:
- a (comma separated)
String
? - 一个(逗号分隔)字符串?
- the sum of all values?
- 所有价值的总和?
Example data (I know this isn't PHP code but it's an example of how the data looks like):
示例数据(我知道这不是PHP代码,但它是数据的示例):
$user1 = { id: 1, name: 'u1', score: 10 }
$user2 = { id: 2, name: 'u2', score: 20 }
$admins = array($user1, $user2);
Output I would like:
输出我想:
magicalArrayFunction($admins, 'name', ', ');
//SHOULD OUTPUT: "u1, u2"
magicalSumArrayFunction($admins, 'score');
//SHOULD OUTPUT: 30
3 个解决方案
#1
1
no there is no build in function to get this done, but you can use the array_sum
, array_map
and the PHP implode
Functions to get the work done.
没有功能构建来完成这项工作,但您可以使用array_sum,array_map和PHP implode函数来完成工作。
function magicalArrayFunction($array)
{
return implode(',', array_map(function($v){
return $v['name'];
}, $array));
}
$usersString = magicalArrayFunction($admins);
echo $usersString;
function magicalSumArrayFunction($array)
{
return array_sum(array_map(function($v){
return $v['score'];
}, $array));
}
$arraySum = magicalSumArrayFunction($admins);
echo $arraySum;
just edit the return $v['name'];
line to your object specific return value. like return $v->name;
只需编辑返回$ v ['name'];行到您的对象特定返回值。比如返回$ v-> name;
//edit after comment
//评论后编辑
one liner solution
单线解决方案
$array = ...
$userString = implode(',', array_map(function($v){return $v['name'];}, $array));
$arraySum = array_sum(array_map(function($v){return $v['score'];}, $array));
#2
0
You want to loop through your users. Foreach is your friend.
您想要遍历您的用户。 Foreach是你的朋友。
$user1 = json_decode( '{"id":1,"name":"u1","score":10}');
$user2 = json_decode( '{"id":2,"name":"u2","score":20}');
$admins = array($user1, $user2);
$names = array();
$score = 0;
foreach( $admins as $user) {
$names[] = $user->name;
$score += $user->score;
}
echo implode( ",", $names) . "\n";
echo $score;
#3
0
Here is your solution, as you need.. Though, validation for integer value in Sum function is remained to implement.. you can add it manually..
这是您的解决方案,因为您需要..虽然,Sum函数中的整数值验证仍然可以实现..您可以手动添加它..
$user1 = '{"id":1,"name":"u1","score":10}';
$user2 = '{"id":2,"name":"u2","score":20}';
$admins = array($user1, $user2);
echo magicalArrayFunction($admins, 'name', ', ');
//SHOULD OUTPUT: "u1, u2"
echo "<br>";
echo magicalSumArrayFunction($admins, 'score');
//SHOULD OUTPUT: 30
function magicalArrayFunction($array, $column, $sep = ', ') {
foreach($array as $jsons){
$new_Arr[] = json_decode($jsons, true);
}
return implode($sep,array_column_custom($new_Arr, $column));
}
function magicalSumArrayFunction($array, $column){
foreach($array as $jsons){
$new_Arr[] = json_decode($jsons, true);
}
return array_sum(array_column_custom($new_Arr, $column));
}
function array_column_custom($array, $column){
foreach($array as $keys=>$inner_array){
foreach($inner_array as $inner_keys=> $values){
if($inner_keys == $column){
$newArr[] = $values;
}
}
}
return $newArr;
}
Edit: One Liner Solution
编辑:一个班轮解决方案
$user1 = '{"id":1,"name":"u1","score":10}';
$user2 = '{"id":2,"name":"u2","score":20}';
$admins = array($user1, $user2);
// Fixed column name used here i.e. 'name','score'..
$userNames = implode(',', array_map(function($u){$u=json_decode($u, true); return $u['name'];}, $admins));
$totalScore = array_sum(array_map(function($u){ $u=json_decode($u, true); return $u['score'];}, $admins));
// If you need to call function..
echo magicalArrayFunction($admins, 'name', ', ');
echo magicalSumArrayFunction($admins, 'score');
function magicalArrayFunction($array, $column, $sep = ', ') {
return implode($sep, array_map(function($u,$c){ $u=json_decode($u, true); return $u[$c];}, $array, str_split(str_repeat($column, count($array)),strlen($column)) ));
}
function magicalSumArrayFunction($array, $column){
return array_sum(array_map(function($u,$c){ $u=json_decode($u, true); return $u[$c];}, $array,str_split(str_repeat($column, count($array)),strlen($column))));
}
#1
1
no there is no build in function to get this done, but you can use the array_sum
, array_map
and the PHP implode
Functions to get the work done.
没有功能构建来完成这项工作,但您可以使用array_sum,array_map和PHP implode函数来完成工作。
function magicalArrayFunction($array)
{
return implode(',', array_map(function($v){
return $v['name'];
}, $array));
}
$usersString = magicalArrayFunction($admins);
echo $usersString;
function magicalSumArrayFunction($array)
{
return array_sum(array_map(function($v){
return $v['score'];
}, $array));
}
$arraySum = magicalSumArrayFunction($admins);
echo $arraySum;
just edit the return $v['name'];
line to your object specific return value. like return $v->name;
只需编辑返回$ v ['name'];行到您的对象特定返回值。比如返回$ v-> name;
//edit after comment
//评论后编辑
one liner solution
单线解决方案
$array = ...
$userString = implode(',', array_map(function($v){return $v['name'];}, $array));
$arraySum = array_sum(array_map(function($v){return $v['score'];}, $array));
#2
0
You want to loop through your users. Foreach is your friend.
您想要遍历您的用户。 Foreach是你的朋友。
$user1 = json_decode( '{"id":1,"name":"u1","score":10}');
$user2 = json_decode( '{"id":2,"name":"u2","score":20}');
$admins = array($user1, $user2);
$names = array();
$score = 0;
foreach( $admins as $user) {
$names[] = $user->name;
$score += $user->score;
}
echo implode( ",", $names) . "\n";
echo $score;
#3
0
Here is your solution, as you need.. Though, validation for integer value in Sum function is remained to implement.. you can add it manually..
这是您的解决方案,因为您需要..虽然,Sum函数中的整数值验证仍然可以实现..您可以手动添加它..
$user1 = '{"id":1,"name":"u1","score":10}';
$user2 = '{"id":2,"name":"u2","score":20}';
$admins = array($user1, $user2);
echo magicalArrayFunction($admins, 'name', ', ');
//SHOULD OUTPUT: "u1, u2"
echo "<br>";
echo magicalSumArrayFunction($admins, 'score');
//SHOULD OUTPUT: 30
function magicalArrayFunction($array, $column, $sep = ', ') {
foreach($array as $jsons){
$new_Arr[] = json_decode($jsons, true);
}
return implode($sep,array_column_custom($new_Arr, $column));
}
function magicalSumArrayFunction($array, $column){
foreach($array as $jsons){
$new_Arr[] = json_decode($jsons, true);
}
return array_sum(array_column_custom($new_Arr, $column));
}
function array_column_custom($array, $column){
foreach($array as $keys=>$inner_array){
foreach($inner_array as $inner_keys=> $values){
if($inner_keys == $column){
$newArr[] = $values;
}
}
}
return $newArr;
}
Edit: One Liner Solution
编辑:一个班轮解决方案
$user1 = '{"id":1,"name":"u1","score":10}';
$user2 = '{"id":2,"name":"u2","score":20}';
$admins = array($user1, $user2);
// Fixed column name used here i.e. 'name','score'..
$userNames = implode(',', array_map(function($u){$u=json_decode($u, true); return $u['name'];}, $admins));
$totalScore = array_sum(array_map(function($u){ $u=json_decode($u, true); return $u['score'];}, $admins));
// If you need to call function..
echo magicalArrayFunction($admins, 'name', ', ');
echo magicalSumArrayFunction($admins, 'score');
function magicalArrayFunction($array, $column, $sep = ', ') {
return implode($sep, array_map(function($u,$c){ $u=json_decode($u, true); return $u[$c];}, $array, str_split(str_repeat($column, count($array)),strlen($column)) ));
}
function magicalSumArrayFunction($array, $column){
return array_sum(array_map(function($u,$c){ $u=json_decode($u, true); return $u[$c];}, $array,str_split(str_repeat($column, count($array)),strlen($column))));
}