Group PHP Array()

Asked

Viewed 197 times

0

I have the following array()

Array
(
    [0] => stdClass Object
        (
            [usu_id] => 1
            [mod_base] => 1
            [mod_id] => 4
        )

    [1] => stdClass Object
        (
            [usu_id] => 1
            [mod_base] => 1
            [mod_id] => 3
        )

    [2] => stdClass Object
        (
            [usu_id] => 1
            [mod_base] => 1
            [mod_id] => 2
        )

)

I need to group by the mod_base field. How do I do?

  • http://sandbox.onlinephpfunctions.com/code/020233e12163ed214345261b1738be6c0a3bea09

  • You need to group by mod_base, but, which layout you want it to look like, what would be the result?

1 answer

1

Can do for example a foreach where it stores the values in the mod_base in a new array and then prints it

$new_array = array();
foreach($array as $key => $value){
    if($key == 'mod_base'){
        array_push($new_array, $value);
    }
}

I don’t think I was wrong and it will work and solve your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.