rohitkhatri's avatar

How to add a new element to every item of collection?

I have a multidimensional array collection and I want to add a particular element of every sub array.

Code:

$posts = collect([
    ['id'=>1,'title'=>'Post one'],
    ['id'=>2,'title'=>'Post two'],
    ['id'=>3,'title'=>'Post three'],
    ['id'=>4,'title'=>'Post four'],
]);

Now I want to add 'url' attribute in every sub array, so how do I do that in laravel way? Is there any one line function in collection class? or any better way of doing this without looping?

0 likes
11 replies
coder's avatar

i think @deringer 's answer is correct but i would like to know that how remove an element from collection. example : imagine the same collection

$posts = collect([
    ['id'=>1,'title'=>'Post one'],
    ['id'=>2,'title'=>'Post two'],
    ['id'=>3,'title'=>'Post three'],
    ['id'=>4,'title'=>'Post four'],
]);

how to remove the title from the given collection ?

1 like
rohitkhatri's avatar

@coder try this

$posts = collect([
    ['id'=>1,'title'=>'Post one'],
    ['id'=>2,'title'=>'Post two'],
    ['id'=>3,'title'=>'Post three'],
    ['id'=>4,'title'=>'Post four'],
]);

$posts->forget(0);

{
    "1": {
        "id": 2,
        "title": "Post two"
    },
    "2": {
        "id": 3,
        "title": "Post three"
    },
    "3": {
        "id": 4,
        "title": "Post four"
    }
}
coder's avatar

@rohitkhatri that will remove the entire an array from not a specific element from all the array.

my question is how you'll remove the title completely from the collection .

think of just opposite of your question .

you wanted to add and i want to remove a specific element.

michaeldyrynda's avatar

In that example, @coder, it might make more sense to pluck the values you need:

$posts->pluck('id');

If you had a collection with more data in it, you can just iterate over the collection in the same way:

$posts_without_titles = $posts->map(function ($post) {
    unset($post['title']);
    return $post;
});
4 likes
thiagocardoso's avatar

I'm not able to do as @michaeldyrynda answer is saying.

I have this array (which I passed to my function under $scheduleData variable):

array:6 [▼
  "dates" => array:3 [▼
    0 => Carbon {#215 ▶}
    1 => Carbon {#220 ▶}
    2 => Carbon {#226 ▶}
  ]
  "starttime" => Carbon {#228 ▶}
  "jobtime" => Carbon {#229 ▶}
  "offsettime" => Carbon {#230 ▶}
  "user_owner" => User {#236 ▶}
  "users" => array:3 [▼
    0 => User {#237 ▶}
    1 => User {#238 ▶}
    2 => User {#239 ▶}
  ]
]

and what i need id (even in an array or collection):

Collection {#227 ▼
  #items: array:3 [▼
    0 => array:1 [▼
      "data" => Carbon {#215 ▶}
      "starttime" => Carbon {#228 ▶}
      "jobtime" => Carbon {#229 ▶}
      "offsettime" => Carbon {#230 ▶}
      "user_owner" => User {#236 ▶}
      "users" => array:3 [▼
          0 => User {#237 ▶}
          1 => User {#238 ▶}
          2 => User {#239 ▶}
        ]
    ]
    1 => array:1 [▼
      "data" => Carbon {#220 ▶}
      "starttime" => Carbon {#228 ▶}
      "jobtime" => Carbon {#229 ▶}
      "offsettime" => Carbon {#230 ▶}
      "user_owner" => User {#236 ▶}
      "users" => array:3 [▼
          0 => User {#237 ▶}
          1 => User {#238 ▶}
          2 => User {#239 ▶}
        ]
    ]
    2 => array:1 [▼
      "data" => Carbon {#226 ▶}
      "starttime" => Carbon {#228 ▶}
      "jobtime" => Carbon {#229 ▶}
      "offsettime" => Carbon {#230 ▶}
      "user_owner" => User {#236 ▶}
      "users" => array:3 [▼
          0 => User {#237 ▶}
          1 => User {#238 ▶}
          2 => User {#239 ▶}
        ]
    ]
  ]
}

I've created a blank collection and was able to separate the main dates:

        $data = collect([]);
        foreach($scheduleData['dates'] as $sd){
            $data->push(['data' => $sd]);
        }

which returned me the following structure

Collection {#227 ▼
  #items: array:3 [▼
    0 => array:1 [▼
      "data" => Carbon {#215 ▶}
    ]
    1 => array:1 [▼
      "data" => Carbon {#220 ▶}
    ]
    2 => array:1 [▼
      "data" => Carbon {#226 ▶}
    ]
  ]
}

and then I tried to use the answer above to add the rest of the info, but with no success

        $data->map(function($d){
            $d['starttime'] = $scheduleData['starttime'];
            return $d;
        });
RichardStyles's avatar

$scheduleData isn't available in the scope of the closure. Did you try:

$data->map(function($d) use ($scheduleData) {
            $d['starttime'] = $scheduleData['starttime'];
            return $d;
        });
1 like

Please or to participate in this conversation.