drtkyykai004574380 2018-05-28 12:23
浏览 84

使用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

  • 写回答

3条回答 默认 最新

  • dongye9453 2018-05-28 12:28
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)