Laravel is invalid to add a field to a two-dimensional array

I want to add another preview field to each element below the pic field, but why is it printed out and not added to each element? Did I add it in the wrong way?

foreach($res as $k=>$v){
    foreach($v["pic"] as $k2=>$v2){
        $v2["preview"]=uniqid();
    }
}
dd($res);

foreachdd($v2)


the original data is as follows

array ( 0 => array ( "id" => 1, "uid" => 10001, "href" => "111111", "content" => "6666", "create_at" => 1513264258, "user" => array ( "id" => 1, "uid" => 10001, "email" => "5414@qq.com", "password" => "111000", "tel" => NULL, "role" => 0, "created_at" => NULL, ), "info" => array ( "uid" => 10001, "nickname" => "", "image" => "static/img/3.jpeg", ), "pic" => array ( 0 => array ( "id" => 1, "uid" => 10001, "href" => "111111", "path" => "static/img/0.jpg", "number" => 0, "create_at" => 1513264258, ), 1 => array ( "id" => 2, "uid" => 10001, "href" => "111111", "path" => "static/img/1.jpg", "number" => 0, "create_at" => 1521442222, ), 2 => array ( "id" => 5, "uid" => 10001, "href" => "111111", "path" => "static/img/0.jpg", "number" => 0, "create_at" => 22222, ), ), "likebar" => array ( 0 => array ( "href" => "111111", "uid" => "10001", ), 1 => array ( "href" => "111111", "uid" => "10001", ), "hot" => 2, "comments" => 3, "status" => true, ), "comments" => array ( 0 => array ( "id" => 1, "href" => "111111", "uid" => 10001, "comment" => "hahhahah", "time" => 14751452, ), 1 => array ( "id" => 2, "href" => "111111", "uid" => 10001, "comment" => "666", "time" => 14521415, ), 2 => array ( "id" => 3, "href" => "111111", "uid" => 10002, "comment" => "ffffff", "time" => 1522441, ), ), "userinfo" => array ( "uid" => 10001, "nickname" => "", "image" => "static/img/3.jpeg", ), "isFollow" => false, ), 1 => array ( "id" => 3, "uid" => 10001, "href" => "111333", "content" => "55555", "create_at" => 22542, "user" => array ( "id" => 1, "uid" => 10001, "email" => "5414@qq.com", "password" => "111000", "tel" => NULL, "role" => 0, "created_at" => NULL, ), "info" => array ( "uid" => 10001, "nickname" => "", "image" => "static/img/3.jpeg", ), "pic" => array ( 0 => array ( "id" => 6, "uid" => 10001, "href" => "111333", "path" => "static/img/0.jpg", "number" => 0, "create_at" => 0, ), ), "likebar" => array ( "hot" => 0, "comments" => 0, "status" => false, ), "comments" => array ( ), "userinfo" => array ( "uid" => 10001, "nickname" => "", "image" => "static/img/3.jpeg", ), "isFollow" => false, ), 2 => array ( "id" => 2, "uid" => 10002, "href" => "111222", "content" => "fffffffff", "create_at" => 211545220, "user" => array ( "id" => 2, "uid" => 10002, "email" => "5555@qq.com", "password" => "123123", "tel" => NULL, "role" => 0, "created_at" => NULL, ), "info" => array ( "uid" => 10002, "nickname" => "", "image" => "static/img/3.jpeg", ), "pic" => array ( 0 => array ( "id" => 3, "uid" => 10001, "href" => "111222", "path" => "static/img/0.jpg", "number" => 0, "create_at" => 15221122, ), 1 => array ( "id" => 4, "uid" => 10001, "href" => "111222", "path" => "static/img/0.jpg", "number" => 0, "create_at" => 222222, ), ), "likebar" => array ( 0 => array ( "href" => "111222", "uid" => "10002", ), "hot" => 1, "comments" => 1, "status" => false, ), "comments" => array ( 0 => array ( "id" => 4, "href" => "111222", "uid" => 10001, "comment" => "22222", "time" => 55241212, ), ), "userinfo" => array ( "uid" => 10002, "nickname" => "", "image" => "static/img/3.jpeg", ), "isFollow" => false, ), )[{"id":1,"uid":10001,"href":"111111","content":"\u54c8\u54c86666","create_at":1513264258,"user":{"id":1,"uid":10001,"email":"5414@qq.com","password":"111000","tel":null,"role":0,"created_at":null},"info":{"uid":10001,"nickname":"\u5c0f\u660e","image":"static\/img\/3.jpeg"},"pic":[{"id":1,"uid":10001,"href":"111111","path":"static\/img\/0.jpg","number":0,"create_at":1513264258},{"id":2,"uid":10001,"href":"111111","path":"static\/img\/1.jpg","number":0,"create_at":1521442222},{"id":5,"uid":10001,"href":"111111","path":"static\/img\/0.jpg","number":0,"create_at":22222}],"likebar":{"0":{"href":"111111","uid":"10001"},"1":{"href":"111111","uid":"10001"},"hot":2,"comments":3,"status":true},"comments":[{"id":1,"href":"111111","uid":10001,"comment":"hahhahah","time":14751452},{"id":2,"href":"111111","uid":10001,"comment":"666","time":14521415},{"id":3,"href":"111111","uid":10002,"comment":"ffffff","time":1522441}],"userinfo":{"uid":10001,"nickname":"\u5c0f\u660e","image":"static\/img\/3.jpeg"},"isFollow":false},{"id":3,"uid":10001,"href":"111333","content":"55555","create_at":22542,"user":{"id":1,"uid":10001,"email":"5414@qq.com","password":"111000","tel":null,"role":0,"created_at":null},"info":{"uid":10001,"nickname":"\u5c0f\u660e","image":"static\/img\/3.jpeg"},"pic":[{"id":6,"uid":10001,"href":"111333","path":"static\/img\/0.jpg","number":0,"create_at":0}],"likebar":{"hot":0,"comments":0,"status":false},"comments":[],"userinfo":{"uid":10001,"nickname":"\u5c0f\u660e","image":"static\/img\/3.jpeg"},"isFollow":false},{"id":2,"uid":10002,"href":"111222","content":"fffffffff","create_at":211545220,"user":{"id":2,"uid":10002,"email":"5555@qq.com","password":"123123","tel":null,"role":0,"created_at":null},"info":{"uid":10002,"nickname":"\u5c0f\u534e","image":"static\/img\/3.jpeg"},"pic":[{"id":3,"uid":10001,"href":"111222","path":"static\/img\/0.jpg","number":0,"create_at":15221122},{"id":4,"uid":10001,"href":"111222","path":"static\/img\/0.jpg","number":0,"create_at":222222}],"likebar":{"0":{"href":"111222","uid":"10002"},"hot":1,"comments":1,"status":false},"comments":[{"id":4,"href":"111222","uid":10001,"comment":"22222","time":55241212}],"userinfo":{"uid":10002,"nickname":"\u5c0f\u534e","image":"static\/img\/3.jpeg"},"isFollow":false}]
May.20,2022


:

$res = [
        [
            'id' => 1,
            'uid' => 3,
            'pic' => [
                'id' => 2,
                'uid' => 33,
            ]
        ],
        [
            'id' => 11,
            'uid' => 31,
            'pic' => [
                'id' => 21,
                'uid' => 331,
            ]
        ],
    ];
    
foreach ($res as $key => $value) {
    $res[$key]['pic']['preview'] = uniqid();
}

that's fine.

you have chosen the wrong object. Add a key-value pair to the pic array and traverse it once. In your way, the $v2 after the second traversal is the specific value in the pic array, but if you want to add the key-value pair to the pic array, it will not be successful

.

this has nothing to do with laravel, it's php array knowledge

I hope it will be helpful to you

Menu