使用php转换数组数据

            [original:protected] => Array
                (
                    [user_id] => 65751
                    [social_id] => 
                    [parent_id] => 
                    [org_id] => 1
                    [type] => 3
                    [s_id] => 1
                    [role_id] => 0
                    [active] => 1
                    [name] => RX
                    [first_name] => JJ
                    [last_name] => DKL
                    [email] => first@testmail.com
                    [secondary_email] => 
                    [username] => cLvcyUr2

                )


    [1] => User Object

                (
                    [user_id] => 82197
                    [social_id] => 
                    [parent_id] => 
                    [org_id] => 1
                    [type] => 2
                    [s_id] => 1
                    [role_id] => 0
                    [active] => 1
                    [name] => sec
                    [first_name] => XX
                    [last_name] => J3
                    [email] => first@testmail.com
                    [secondary_email] => 
                    [username] => VfTqXyvJ

                )

How to transform the array data mean to keep only two email and username rest should remove

Array (
[0] => Array (
    [email] => first@testmail.com
    [username] => cLvcyUr2
    )
[1] => Array (
    [email] => first@testmail.com
    [username] => VfTqXyvJ
    )
)

How could this possible i do not to unset data one by one it should automatically unset and pick only two value

Why not create an empty array and just copy the values you need? Trying to unset everything in your array is a lot of work and is not really maintainable, out of experience.

Note that your pasted code contains both an array and object, but your question is about an array only.

$newArray = [];

foreach ($oldArray as $item) {
    $newArray[] = [
        'email' => $item->email, 
        'username' => $item->username
    ]; // If $item is object
    $newArray[] = [
        'email' => $item['email'], 
        'username' => $item['username']
    ]; // If $item is array
}

var_dump($newArray);

Did you try something like that :

$index = array_search('user_id', $array);
unset($array[$index]);

Do that for all key you want to remove.

$newArray = [];

foreach ($oldArray as $item) {
$arr = [];
    $arr['email'] = $item->email;
    $arr['username'] = $item->username;

    $newArray[] = $arr;
}