2

I have an array that looks like this

Array
(
    [1] => Array
        (
            [name] => Zeze
            [city] => Denver, 
            [state] => Colorado, 
            [country] => United States
            [user_id] => 1
            [cars] => Array
                (
                    [140] => Array
                        (
                            [cars_name] => BMW
                        )

                    [162] => Array
                        (
                            [cars_name] => Mazda
                        )
                )
        )

    [8] => Array
        (
            [name] => Lex
            [city] => Schwelm, 
            [state] => North Rhine-Westphalia, 
            [country] => Germany
            [user_id] => 5
            [cars] => Array
                (
                    [140] => Array
                        (
                            [cars_name] => Mercedes
                        )

                    [162] => Array
                        (
                            [cars_name] => Audi
                        )
                )
        )
)

I need to extract the value from user_id and put it in a comma separated string.

For the above array, I would like to get:

1,5

I'm a bit confused how to loop this array with foreach and then how would I create the string? Or is there a better way?

pepe
  • 9,799
  • 25
  • 110
  • 188

5 Answers5

8
$uids = Array();
foreach($users as $u) $uids[] = $u['user_id'];
$list = implode(",",$uids);

This is assuming your array is named $users and $list is the output.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • This answer spares you the function definition and the callback operation. Additionally it might be easier to read. – hakre Sep 20 '11 at 16:12
2

You can use a combination of array_map and implode:

function get_uid($el) {
    return $el["user_id"];
}

$csv = implode(array_map("get_uid", $your_array), ',');
echo $csv;    
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 1
    Was just typing this answer and got the orange bar "A new answer has been posted". – Jonah Sep 20 '11 at 03:14
  • thx - do you know if this solution is more/less intensive than @kolink's below? – pepe Sep 20 '11 at 03:37
  • This solution calls get_uid once for every item in the array. On that score it rates the same as Alec Ananian's solution. – Niet the Dark Absol Sep 20 '11 at 03:48
  • thx jacob - sounds good, was asking b/c I'll use this to create a string with a user's friends which can get pretty long and require multiple runs of the function/implode/array_map (versus multiple foreach traversing the array + implode) – pepe Sep 20 '11 at 03:55
0

This will be the most easier method:

echo implode(",", array_column($myArray, "user_id"));

Eldho NewAge
  • 1,313
  • 3
  • 13
  • 17
0

Iterate over each item in the multimensional array with a foreach loop, and treat the item as a normal array. Then push the user_id value into another array and implode it with a comma to make it comma separated.

$user_ids = array();

foreach($arr in $multidim_arr) {
    array_push($user_ids, $arr["user_id"]);
}

$user_ids = implode(",", $user_ids);
rekamoyka
  • 1,122
  • 11
  • 15
  • `array_push` is advised against when pushing a single value in favour of `[]`, and `implode` has the seperator as the first argument (they can go in either order for historical reasons, but seperator-first is preferred) – Niet the Dark Absol Sep 20 '11 at 03:11
-1
$stateId = Array (
     [0] => Array
         (
             [id] => 9
             [state_id] => 81
             [rto_id] => 82
             [is_active] => 1
         )
     [1] => Array
         (
             [id] => 10
             [state_id] => 82
             [rto_id] => 83
             [is_active] => 1
         )

 );

 $stateIds = implode(",", array_column($stateId, "state_id"));

 echo $stateIds;
Blauharley
  • 4,186
  • 6
  • 28
  • 47