Refactoring objects using Laravel Collections fails - php

What I try to do is that I need to return an array with key-value pairs from an array of objects.
From an api I get objects that looks something like this.
array:8 [▼
0 => TargetingSearch {#229 ▼
#data: array:6 [▼
"id" => "6005609368513"
"name" => "Software"
"audience_size" => 565991450
"path" => array:4 [▶]
"description" => ""
"topic" => "Technology"
]
#_type_checker: TypeChecker {#228 ▶}
}
The simple way to do it is to loop through the objects and push the values into an array.
$codes = [];
foreach($objects as $object) {
$codes[] = [
'id' => $object->id,
'name' => $object->name,
];
}
Which will return this that is exactly what I need.
['id' => 321, 'name' => "item1"],
['id' => 321, 'name' => "item1"],
['id' => 321, 'name' => "item1"],
I think that this is a little hard to manage and what I try to do is something a little more readable.
For example, by using the Laravel collection classes I could do something like this.
$codes = collect($objects)->pluck('id', 'name')->toArray();
The problem here is that this does not return the keys.
["item1" => "123"],
["item2" => "321"],
["item3" => "213"],
If I use
$codes = collect($objects)->only('id', 'name')->toArray();
It returns an empty array because the object itself is messing it all up.
anyone knows how to solve this?

You can use "map" method
collect($objects)->map(function ($item) {
return [
'id' => $item->id,
'name' => $item->name
];
});

Related

Merge arrays in laravel including keys

I have 2 arrays and I want to merge them. (I can merge them) but I also need to include their unique keys in merged results and that part I cannot achieve.
sample
$prices = [
['112802' => "500000"],
['113041' => "1000000"],
];
$notes = [
['112802' => "note 2"],
['113041' => "note 1"],
];
$collection = collect($prices);
$zipped = $collection->zip($notes);
$zipped->toArray();
Unique keys are 112802 and 113041.
When I merge my array all I get is this:
[
[
"1000000",
"note 1"
],
[
"500000",
"note 2"
]
]
What I'm looking for is like this:
[
[
"id" => "112802",
"price" => "500000",
"note" => "note 2",
],
[
"id" => "113041",
"price" => "1000000",
"note" => "note 1",
]
}]
any suggestion?
This does what you want with the data you provide.
NOTE it will only work if your 2 arrays are the same size and the the keys are in the same order.
If this data comes from a database, it is likely it could have been produced in the format you actually wanted rather than having to fiddle with the data post fetch.
$prices = [
['112802' => "500000"],
['113041' => "1000000"],
];
$notes = [
['112802' => "note 2"],
['113041' => "note 1"],
];
$new = [];
foreach ($prices as $i=>$pr){
$k = key($pr);
$new[] = [ 'id' => $k,
'price' => $pr[$k],
'note' => $notes[$i][$k] ];
}
print_r($new);
RESULT
Array
(
[0] => Array (
[id] => 112802
[price] => 500000
[note] => note 2
)
[1] => Array (
[id] => 113041
[price] => 1000000
[note] => note 1
)
)
Here's another solution using some of Laravel's Collection methods.
It's not the most elegant, but it can be a starting point for you.
$prices = collect([
['112802' => "500000"],
['113041' => "1000000"],
])->mapWithKeys(function($item) {
// This assumes that the key will always be the ID and the first element is the price.
// Everythng else for each element will be ignored.
$id = array_keys($item)[0];
return [$id => ["id" => $id, "price" => reset($item)]];
});
$notes = collect([
['112802' => "note 2"],
['113041' => "note 1"],
])->mapWithKeys(function($item) {
$id = array_keys($item)[0];
return [$id => ["id" => $id, "note" => reset($item)]];
});
$result = $prices->zip($notes)->map(function ($item) {
// Feel free to call `toArray()` here if you don't want a Collection.
return collect($item)->mapWithKeys(function ($a) { return $a; });
});
Below is the $result (called using dd()).
Illuminate\Support\Collection {#1886 ▼
#items: array:2 [▼
0 => Illuminate\Support\Collection {#1888 ▼
#items: array:3 [▼
"id" => 112802
"price" => "500000"
"note" => "note 2"
]
}
1 => Illuminate\Support\Collection {#1889 ▼
#items: array:3 [▼
"id" => 113041
"price" => "1000000"
"note" => "note 1"
]
}
]
}
It's achieved by extracting the ID so that the zip can join there, but then we need a little hack with the map and mapWithKeys in the $result.
That's just because otherwise each element in $result will still have two separate arrays for $prices and $notes.

How to get multiple arrays from an array in php?

I have he next array looks like:
array:50 [▼
0 => array:39 [▶]
1 => array:39 [▶]
2 => array:39 [▶]
]
So I want to get arrays with a value in common, for example:
array:39 [▼
"id" => 121
"user" => 368
]
array:39 [▼
"id" => 121
"user" => 3687
]
array:39 [▼
"id" => 500
"user" => 452
]
I want to get the two arrays with the attribute
id 121, I was trying to looping the array with foreach looks like:
foreach ($info as $val){
foreach($info as $f ){
if($f["id"]==$val["id"]){
//get the multiple arrays
}
}
}
So, I can't get all the arrays, some idea to how can do that?
I'd use a Collection.
collect your array of arrays:
$collection = collect([
[
"id" => 121
"user" => 368
],
[
"id" => 121
"user" => 3687
],
[
"id" => 500
"user" => 452
]
]);
Use the where method to filter based on a specific key's value:
$filtered = $collection->where('id', 121);
$filtered->all();
/*
[
['id' => '121', 'user' => 368],
['id' => '121', 'user' => 3687],
]
*/
Other where-like methods are available. Be sure to read through all of the documentation on Collections, it's full of great examples!
If you're now convinced that you should use Collections for everything, check out Adam Wathan's awesome book (and other resources): Refactoring to Collections (not free)

Updating array keys inside of an array

My goal is to be able to update a key value inside of an array inside of an array and I'm don't know if I'm using the right php array function.
BEFORE:
array:2 [
"week_number" => 1
"games" => array:1 [
0 => array:3 [
"game_number" => 1
"umpires" => []
"teams" => []
]
]
]
AFTER:
array:2 [
"week_number" => 1
"games" => array:1 [
0 => array:3 [
"game_number" => 1
"umpires" => []
"teams" => [1,2]
]
]
]
Test Class:
private function validParams($overrides = [])
{
return array_merge_recursive([
'week_number' => 1,
'games' => [[
'game_number' => 1,
'umpires' => [],
'teams' => [],
]]
], $overrides);
}
$response = $this->actingAs($this->authorizedUser)
->post(route('games.store', ['week' => $this->week->id]), $this->validParams([
'games' => [][
[
'teams' => [1,2]
]
]
]));
If you want to update the keys... typing $array['new_key'] = $array['old_key'] will duplicate the value with 2 sets of keys.
You have a few options here. Either you create a new array and just set your desired keys or work with array_keys and array_values and mix them up... your choice
http://php.net/manual/en/ref.array.php
See the list above, there are a lot of array functions you can use... see the two above and array_map... there is virtually a great number of ways you can do this. See how your problem is best handled after reviewing the documentation.
Good luck!
This is the moment where you need unset(): Adding a value with a different key will not update or overwrite the old value but simply add another key-value pair.
Hence, add the new value fist, then unset the old one. We can use To array_walk to itterate over the array:
array_walk($array, function (& $item) {
$item['new_key'] = $item['old_key'];
unset($item['old_key']);
});
Take note of the & reference operator in the lambda function: it ensures we are working on the original array and not a copy of it.
I found this as a solution.
private function validParams($overrides = [])
{
return array_replace_recursive([
'week_number' => 1,
'games' => [
0 => [
'game_number' => 1,
'umpires' => [],
'teams' => [],
]
]
], $overrides);
}
->post(route('games.store', ['week' => $this->week->id]), $this->validParams([
'games' => [
0 => [
'teams' => [1,2]
]
]
]));

Laravel collection how to remove a specific key from all items?

It sounded crazy simple to me at first but I can't find the solution!
Here is My collection :
Collection {#433
#items: array:432 [
0 => array:5 [
"word_id" => 12218
"name" => "ordered"
"rank" => 12217
"is_real" => 1
"id" => 1
]
1 => array:5 [
"word_id" => 12097
"name" => "one-dimensional"
"rank" => 12096
"is_real" => 1
"id" => 2
]
2 => array:5 [
"word_id" => 19679
"name" => "watery"
"rank" => 19678
"is_real" => 1
"id" => 3
]
.
.
.
But I want it to be like this :
Collection {#433
#items: array:432 [
0 => array:5 [
"name" => "ordered"
"id" => 1
]
1 => array:5 [
"name" => "one-dimensional"
"id" => 2
]
2 => array:5 [
"name" => "watery"
"id" => 3
]
.
.
.
How can it be done with laravel collection? Do I have to change my collection to array and do the manipulation myself? How?
Or you can also use
$collection = $collection ->map(function ($item) {
return collect($item)->forget(['work_id','rank','is_real']);
});
$collection = $collection->map(function ($item) {
return array_only($item, ['id', 'name']);
});
You can use the only() method.
The only method returns the items in the collection with the specified
keys
$collection = $collection->map(function ($item) {
return $item->only(['id', 'name'])
});
This way your collection still remains as collections;
Please note, only() method is only available for Laravel collections
not for an array
An alternative approach could be to use the Arr::only() helper to reduce the items:
$collection = $collection->map(static fn($item) => Arr::only($item, ['id', 'name']));

Laravel collection contains

I'm using the Laravel contains method on a collection https://laravel.com/docs/5.3/collections#method-contains. But it does not work for me.
foreach ($this->options as $option) {
if($options->contains($option->id)) {
dd('test');
}
}
dd($options); looks like this:
Collection {#390
#items: array:1 [
0 => array:3 [
0 => array:7 [
"id" => 10
"slug" => "test"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
1 => array:7 [
"id" => 11
"slug" => "test-1"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
2 => array:7 [
"id" => 12
"slug" => "test-2"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
]
]
}
Result of dd($option->id); is 10.
What could be wrong? Or is there a better way?
You should pass a key / value pair to the contains method, which will
determine if the given pair exists in the collection. Use contains() method in this way:
foreach ($this->options as $option) {
// Pass key inside contains method
if($option->contains('id', $option->id)) {
dd('test');
}
}
Use the following, which tells Laravel you want to match the 'id':
$options->contains('id', $option->id);
Docs
foreach ($this->options as $option) {
if(!$options->flatten(1)->where('id',$option->id)->isEmpty()) {
dd('test');
}
}
The contains method determines whether the collection contains a given item.
There are basically three ways in which it can be used :
simply checking the item
$collection = collect(['name' => 'Sarah', 'age' => 23]);
$collection->contains('Desk');
// false
$collection->contains('Sarah');
// true
checking the key/value pair :
$collection = collect([
['name' => 'Sarah', 'age' => 23],
['name' => 'Vicky', 'age' => 34],
]);
$collection->contains('name', 'Hank');
// false
checking via callback function :
$collection = collect([3, 5, 7, 9, 11]);
$collection->contains(function ($value, $key) {
return $value < 2;
});
// false
Now, for your problem, we will use the second category, i.e :
foreach ($this->options as $option) {
// here id is key and $option->id is value
if($option->contains('id', $option->id)) {
dd('test');
}
}
Link to docs

Categories