0

Is there a one-line solution (specific function) you can use to get a specific property of all Objects in an Array as:

  1. a (comma separated) String?
  2. 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):

$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
7
  • $user1 = { id: 1, name: 'u1', score: 10 }. This syntax is invalid in PHP Commented Aug 20, 2014 at 13:17
  • 1
    array_column with join or array_sum would do. Commented Aug 20, 2014 at 13:18
  • 1
    @hindmost: It was just a representation of the data... The $user1 object has 'id', 'name' and 'score' as property in this example. Commented Aug 20, 2014 at 13:18
  • may be $user1 = array( 'id'=> 1, 'name' => 'u1', 'score' => 10 ); Commented Aug 20, 2014 at 13:18
  • 1
    There's a userland shim (always a good approach, since you're somewhen gonna use 5.5 anyway) in the manual. Albeit if your list really contained objects, you'd also need array_map("get_object_vars", $admin). Commented Aug 20, 2014 at 13:24

3 Answers 3

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.

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;


//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));
Sign up to request clarification or add additional context in comments.

Comments

0

You want to loop through your users. Foreach is your friend.

$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;

2 Comments

Thanks for the effort, but I was wondering if there was an one-line solution as mentioned...
You could compress this into a one liner :)
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..

$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 Comment

Thanks for the effort, but I was wondering if there was an one-line solution as mentioned...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.