簡體   English   中英

Laravel中的多維數組

[英]multidimensional array in laravel

我可以使用$post->user->name循環顯示用戶數據(第二個數組) ,但是如何顯示第三個數組數據? 它不能作為$post->likes->user_id
請幫忙。
這是我的數組列表:

    [{
    "id": 13,
    "user_id": 1,
    "content": "\"The reason most people never reach their goals is that they don't define them, or ever seriously consider them as believable or achievable. Winners can tell you where they are going, what they plan to do along the way, and who will be sharing the adventure with them.\" --Denis Waitley",
    "status": 0,
    "created_at": "2017-07-06 04:02:41",
    "updated_at": "2017-07-06 04:02:41",
    "user": {
        "id": 1,
        "name": "hardeep singh",
        "gender": "male",
        "slug": "hardeep-singh",
        "email": "hardeepphp@yahoo.com",
        "pic": "47224964-profile-pictures.jpg",
        "role": "admin",
        "created_at": "2017-02-21 06:06:28",
        "updated_at": "2017-02-21 06:06:28"
    },
    "likes": {
        "id": 2,
        "posts_id": 13,
        "user_id": 3,
        "created_at": "2017-07-07 17:52:18"
    }
}, {
    "id": 12,
    "user_id": 10,
    "content": "\"People who succeed have momentum. The more they succeed, the more they want to succeed, and the more they find a way to succeed. Similarly, when someone is failing, the tendency is to get on a downward spiral that can even become a self-fulfilling prophecy.\" --Tony Robbins",
    "status": 0,
    "created_at": "2017-07-06 04:00:14",
    "updated_at": "2017-07-06 04:00:14",
    "user": {
        "id": 10,
        "name": "Ogran Gysn",
        "gender": "male",
        "slug": "ogran-gysn",
        "email": "ogran@gmail.com",
        "pic": "ee.jpg",
        "role": "company",
        "created_at": "2017-03-10 10:48:19",
        "updated_at": "2017-03-10 10:48:19"
    },
    "likes": {
        "id": 3,
        "posts_id": 12,
        "user_id": 3,
        "created_at": "2017-07-07 17:52:33"
    }
}, {
    "id": 11,
    "user_id": 9,
    "content": "\"Thinking should become your capital asset, no matter whatever ups and downs you come across in your life.\" --A.P.J. Abdul Kalam",
    "status": 0,
    "created_at": "2017-07-06 03:59:47",
    "updated_at": "2017-07-06 03:59:47",
    "user": {
        "id": 9,
        "name": "john lewis",
        "gender": "male",
        "slug": "john-lewis",
        "email": "john@gmai.com",
        "pic": "scs.png",
        "role": "company",
        "created_at": "2017-03-10 04:04:27",
        "updated_at": "2017-03-10 04:04:27"
    },
    "likes": {
        "id": 17,
        "posts_id": 11,
        "user_id": 1,
        "created_at": "2017-07-13 10:07:56"
    }
}, {
    "id": 10,
    "user_id": 3,
    "content": "Happiness is a butterfly, which when pursued, is always beyond your grasp, but which, if you will sit down quietly, may alight upon you",
    "status": 0,
    "created_at": "2017-07-06 03:59:19",
    "updated_at": "2017-07-06 03:59:19",
    "user": {
        "id": 3,
        "name": "anita donal",
        "gender": "female",
        "slug": "anita-donal",
        "email": "anita@yahoo.com",
        "pic": "47354305-profile-pictures.jpg",
        "role": null,
        "created_at": "2017-02-21 06:07:39",
        "updated_at": "2017-02-21 06:07:39"
    },
    "likes": {
        "id": 13,
        "posts_id": 10,
        "user_id": 1,
        "created_at": "2017-07-13 10:01:43"
    }
}, {
    "id": 9,
    "user_id": 1,
    "content": "Take up one idea. Make that one idea your life--think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.",
    "status": 0,
    "created_at": "2017-07-06 03:58:40",
    "updated_at": "2017-07-06 03:58:40",
    "user": {
        "id": 1,
        "name": "hardeep singh",
        "gender": "male",
        "slug": "hardeep-singh",
        "email": "hardeepphp@yahoo.com",
        "pic": "47224964-profile-pictures.jpg",
        "role": "admin",
        "created_at": "2017-02-21 06:06:28",
        "updated_at": "2017-02-21 06:06:28"
    },
    "likes": {
        "id": 19,
        "posts_id": 9,
        "user_id": 1,
        "created_at": "2017-07-13 10:08:51"
    }
}, {
    "id": 3,
    "user_id": 9,
    "content": "have a nic day :)",
    "status": 0,
    "created_at": "2017-07-05 07:59:22",
    "updated_at": "2017-05-09 08:56:59",
    "user": {
        "id": 9,
        "name": "john lewis",
        "gender": "male",
        "slug": "john-lewis",
        "email": "john@gmai.com",
        "pic": "scs.png",
        "role": "company",
        "created_at": "2017-03-10 04:04:27",
        "updated_at": "2017-03-10 04:04:27"
    },
    "likes": {
        "id": 1,
        "posts_id": 3,
        "user_id": 1,
        "created_at": "2017-07-13 10:33:43"
    }
}, {
    "id": 1,
    "user_id": 3,
    "content": "This is demo post",
    "status": 0,
    "created_at": "2017-07-04 18:25:14",
    "updated_at": "-0001-11-30 00:00:00",
    "user": {
        "id": 3,
        "name": "anita donal",
        "gender": "female",
        "slug": "anita-donal",
        "email": "anita@yahoo.com",
        "pic": "47354305-profile-pictures.jpg",
        "role": null,
        "created_at": "2017-02-21 06:07:39",
        "updated_at": "2017-02-21 06:07:39"
    },
    "likes": null
}, {
    "id": 2,
    "user_id": 3,
    "content": "this is post by me, i am anita donal",
    "status": 0,
    "created_at": "2017-03-23 16:14:50",
    "updated_at": "-0001-11-30 00:00:00",
    "user": {
        "id": 3,
        "name": "anita donal",
        "gender": "female",
        "slug": "anita-donal",
        "email": "anita@yahoo.com",
        "pic": "47354305-profile-pictures.jpg",
        "role": null,
        "created_at": "2017-02-21 06:07:39",
        "updated_at": "2017-02-21 06:07:39"
    },
    "likes": {
        "id": 21,
        "posts_id": 2,
        "user_id": 1,
        "created_at": "2017-07-13 10:36:30"
    }
}]

在數組中,您不能使用->我假設您的數組在$ post對象中。

$post->likes->user_id.
  foreach($post as $row){
           $row['like']['user_id']
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM