How to delete a nested array inside of an array - php

I have an multidimensional array made out of form data that can look like this example:
array [
"absender" => "Maxim Ivan",
"email" => "maximivan#example.com",
"telefon" => "1234567890",
"fax" => null,
"grund" => "Gehaltserhöhung",
"termin" => [
0 => [
"person" => "Some Name",
"meeting" => "10.05"
],
1 => [
"person" => "Another Name",
"meeting" => "18.05"
],
2 => [
"person" => "Again another name",
"meeting" => null,
"next-possible-meeting" => "1"
],
3 => [
"person" => "And again",
"meeting" => null,
"next-possible-meeting" => "1"
],
4 => [
"meeting" => null,
],
"bemerkung" => "some notes by Maxim"
]
'person' and 'next-possible-meeting' are checkboxes while 'meeting' is a textarea. I only need the 'termin'-data when 'person' exists, so I store each of them in different arrays (one for persons, one for meetings and one for next-possible-meetings).
That means I don't need the array to be nested anymore, the 'termin' can (and should) be deleted out of it. But I don't know how to access the nested array correctly.
How do I delete the whole 'termin'-array out of the whole array and therefore make it a normal and not a multidimensional array?

Use unset()
if(isset($array['termin'])){
unset ($array['termin']);
}
Output:- https://3v4l.org/1Yj4F
Note:- isset() used to check that index exist or not, if not function call will saved.

Related

Merge two multidimensional arrays using recursion, but not on full data set

I'm trying to add 2 array data to each other with array_merge(). It's just attached to the back. But lower levels are ignored.
Is there an alternative to array_merge() that will merge the user values without duolicating the color values?
Existing array data:
$existingtArr = [
"A" => [
"color" => 'red',
"user" => [
"Daniel" => ["01:18:08", "04:10:12"],
"Max" => ["01:04:00"],
"Serto" => ["02:00:02"],
]
],
"B" => [
"color" => 'blue',
"user" => [
"Franz" => ["08:40:52"],
"Hugo" => ["07:08:58"],
]
]
];
New array data:
$newArr = [
"A" => [
"color" => 'red',
"user" => [
"Fabian" => ["06:03:00"], // + 1 user
"Max" => ["04:10:12"], // + 1 new time
"Serto" => ["02:00:02"],
]
],
"B" => [
"color" => 'blue',
"user" => [
"Franz" => ["08:40:52", "14:05:32", "20:34:15"], // an older one is available, + 2 new times
"Hugo" => ["04:10:12"], // + 1 time
]
],
"C" => [ // + new whole group
"color" => 'green',
"user" => [
"Maxi" => ["07:08:58", "04:10:12"],
]
]
];
Supplement the existing data with the new data:
echo '<pre>';
print_r(array_merge($existingtArr, $newArr));
echo '</pre>';
Expected result array data:
$resultArr = [
"A" => [
"color" => 'red',
"user" => [
"Daniel" => ["01:18:08", "04:10:12"],
"Fabian" => ["06:03:00"],
"Max" => ["01:04:00", "04:10:12"],
"Serto" => ["02:00:02"],
]
],
"B" => [
"color" => 'blue',
"user" => [
"Franz" => ["08:40:52", "14:05:32", "20:34:15"],
"Hugo" => ["07:08:58", "04:10:12"],
]
],
"C" => [
"color" => 'green',
"user" => [
"Maxi" => ["07:08:58", "04:10:12"],
]
]
];
You cannot simply call array_merge_recursive() on the whole data sets because they will generated repeated color values, but you want the color values to remain singular and the user data to be recursively merged.
To accommodate this logic (assuming it is okay to simply mutate the $existingtArr array with the data from $newArr), perform a check for the existence of each letter-set, and either push the whole set for a non-present letter, or recursively merge the shared letter-sets.
Code: (Demo)
foreach ($newArr as $letter => $set) {
if (!isset($existingtArr[$letter])) {
$existingtArr[$letter] = $set;
} else {
$existingtArr[$letter]['user'] = array_merge_recursive(
$existingtArr[$letter]['user'],
$set['user']
);
}
}
var_export($existingtArr);

Laravel - restructuring array for easy sync of many-to-many with additional pivot data

I have created what feels like a clunky solution to restructuring a data array in order to pass it to a sync() to update a many-to-many relationship with additional data in the pivot table and wondered if anyone could suggest a simpler approach.
I have an array coming from a request, here's a relevant extract:
"papers" => [
0 => [
"id" => 2
"code" => "123-321-888"
"name" => "Pop out"
"pivot" => [
"job_id" => 46
"paper_id" => 2
"qty_required" => 500
]
]
1 => [
"id" => 1
"code" => "444-666-999"
"name" => "Premium pro"
"pivot" => [
"job_id" => 46
"paper_id" => 1
"qty_required" => 1000
]
]
]
In order to do an easy sync of a many-to-many relationship with extra pivot data one needs to restructure that to:
[
paper[id] => [
'qty_required' => paper[pivot][qty_required]
]
]
Which for the above example would be:
[
2 => [
"qty_required" => "500"
]
1 => [
"qty_required" => "1000"
]
]
I'm currently doing a 2-step process to achieve this as follows:
$paperUpdate = Arr::pluck($request->input('papers'), 'pivot.qty_required', 'id');
//output: [ 2 => 500, 1 => 1000]
foreach ($paperUpdate as $key => $value) {
$paperSync[$key]['qty_required'] = $value;
}
//output: [ 2 => [ "qty_required" => "500" ], 1 => [ "qty_required" => "1000" ]
$job->papers()->sync($paperSync);
Is there an easier approach?
Your approach seems fine to me. If you want to nit pick, you could do one less iteration using:
$sync = array_reduce($request->input('papers'), function ($sync, $paper) {
$id = $paper['id'];
$sync[$id] = [ 'qty_required' => $paper['pivot']['qty_required'] ];
return $sync;
}, []);

Removing an element from a nested array in PHP

Am working on a Laravel application whereby I have an associative array that am to pass to an API endpoint, Before posting to the API, I want to delete the img key together with its value . I have tried to use unset function but it is not removing the img key
Array where I want to remove the image property
$a[] = [
0 => array:4 [
"name" => "Martoo nnn"
"relationship" => "Spouse"
"dob" => "2001-02-03"
"img" => "img.png"
]
1 => array:4 [
"name" => "sdsdsd sdsdsd"
"relationship" => "Child"
"dob" => "2019-04-04"
"img" => "img1.png"
]
2 => array:4 [
"name" => "sdsdsd sddds"
"relationship" => "Child"
"dob" => "2019-04-05"
"img" => "img2.png"
]
3 => array:4 [
"name" => "dssdsd dsdsd"
"relationship" => "Child"
"dob" => "2019-04-02"
"img" => "img3.png"
]
4 => array:4 [
"name" => "dssdsd dssdsd"
"relationship" => "Child"
"dob" => "2019-04-04"
"img" => "img4.png"
]
];
Unset method
$array = $a;
unset($array['img']);
//dd($a);
You can do something like this,
foreach ($array as $key => &$value) { // & defines changes will be made # value itself
unset($value['img']);
}
And Yes, I don't understand why you initialised $a as $a[]?
$newarray = array_filter($a, function($k) {
return $k != 'img';
}, ARRAY_FILTER_USE_KEY);
and pass this new array

Find document by field in sub-array and return another field in same sub-array

I have a collection called products, which has documents containing several fields, along with a variants sub-array.
The variants sub-arrays have several fields, including sku and id.
I know the id value, and I need to use it to get the sku value.
The simplified collection looks like this:
[
"_id" => "whatever_id_1",
"field_2" => "some value 1",
"variants" =>
[
"id" => "some_id_123"
"sku" => "SKU-1"
],
[
"id" => "some_id_124"
"sku" => "SKU-2"
],
[
"id" => "some_id_125"
"sku" => "SKU-3"
]
],
[
"_id" => "whatever_id_2",
"field_2" => "some value 2",
"variants" =>
[
"id" => "some_id_126"
"sku" => "SKU-4"
],
[
"id" => "some_id_127"
"sku" => "SKU-5"
],
[
"id" => "some_id_128"
"sku" => "SKU-6"
]
],
[
"_id" => "whatever_id_3",
"field_2" => "some value 3",
"variants" =>
[
"id" => "some_id_129"
"sku" => "SKU-7"
],
[
"id" => "some_id_130"
"sku" => "SKU-8"
],
[
"id" => "some_id_131"
"sku" => "SKU-9"
]
]
I am retrieving the correct document with
// Set item_id
$item_id = 'some_id_127';
// Build 'find product with inventory item id' query
$find_product_with_id_query = [ 'variants' => ['$elemMatch' => ['id' => $item_id] ] ];
// Get the product document to process
$inventory_update_product = $client_products_collection->findOne($find_product_with_id_query);
This properly returns the parent document with "_id" => "whatever_id_2".
Now, I know I can iterate over that results (eg. $inventory_update_product['variants'), and find the sku value that way.
QUESTIONS
1. But is there some way to get the sku value with MongoDB?
2. Is there any benefit to using MongoDB for this last step, or is it more efficient to just use PHP for loop to find the sku?
Yes, actually. You can use projection:
// Set item_id
$item_id = 'some_id_127';
// Build 'find product with inventory item id' query
$find_product_with_id_query = [ 'variants' => ['$elemMatch' => ['id' => $item_id] ] ];
// Project and limit options
$options = [
'projection' => [ 'variants.$' => 1 ],
'limit' => 1
];
// Get the product document to process
$inventory_update_product = $client_products_collection->find($find_product_with_id_query, $options);
This will return a cursor containing the document with the array variants with only the element matching the one you searched for.
Exact syntax may vary depending on driver version and whether or not you're using the userland library.

Complex Laravel collection

I'm having some issues with the Laravel Collection class.
What I'm trying to do:
I have a multisite solution in which a site has "facilitators". Sometimes one facilitator appears on multiple sites, and not just one.
I want to list all the facilitators and the website they're on the main page, but I don't want multiple users.
So what I currently do is:
Get the Facilitators.
Use Collection to collect the facilitators and use unique('name').
This gives me unique facilitators, but only picks the first one it detects and then deletes the other ones.
So lets say I have this collection:
Collection {
#items: array:3 [
0 => array:2 [
"name" => "John"
"site" => "Example"
]
1 => array:2 [
"name" => "Martin"
"site" => "Another"
]
2 => array:2 [
"name" => "John"
"site" => "Another"
]
]
}
With unique() I would get:
Collection {
#items: array:3 [
0 => array:2 [
"name" => "John"
"site" => "Example"
]
1 => array:2 [
"name" => "Martin"
"site" => "Another"
]
]
}
And this is what I want to get:
Collection {
#items: array:3 [
0 => array:2 [
"name" => "John"
"site" => ["Example", "Another"]
]
1 => array:2 [
"name" => "Martin"
"site" => "Another"
]
]
}
Does anyone have an idea how I could accomplish this with Laravel's collection class?
When stuck with collections always remember reduce is a powerful tool in your arsenal.
Building on Sam's answer which I couldn't get to work, I think using reduce alongside groupBy should work...
$sites = collect([
["name" => "John", "site" => "Example"],
["name" => "Martin", "site" => "Another"],
["name" => "John", "site" => "Another"],
]);
$sites->groupBy('name')->reduce(function ($result, $item) {
$result[] = [
'name' => $item->first()['name'],
'sites' => $item->pluck('site')->toArray()
];
return $result;
}, collect([]))->toArray();
And from the console...
λ php artisan tinker
Psy Shell v0.8.2 (PHP 7.0.10 ÔÇö cli) by Justin Hileman
>>> $sites = collect([
... ["name" => "John", "site" => "Example"],
... ["name" => "Martin", "site" => "Another"],
... ["name" => "John", "site" => "Another"],
... ]);
=> Illuminate\Support\Collection {#698
all: [
[
"name" => "John",
"site" => "Example",
],
[
"name" => "Martin",
"site" => "Another",
],
[
"name" => "John",
"site" => "Another",
],
],
}
>>> $sites->groupBy('name')->reduce(function ($result, $item) {
... $result[] = ['name' => $item->first()['name'], 'sites' => $item->pluck('site')->toArray()];
...
... return $result;
... }, collect([]))->toArray();
=> [
[
"name" => "John",
"sites" => [
"Example",
"Another",
],
],
[
"name" => "Martin",
"sites" => [
"Another",
],
],
]
One thing to note is that you specified in your question that the sites should return a single string if there's only one site and an array if there's many.The above solution does not provide this! I think this is inconsistent and you should always return an array for the sites key, even if it only has one value as it will make it more difficult to read and manipulate later on.
However, if this is something important, you could instead check if there are many sites when using pluck to set an array and if not you could set it as a single string, like this:
$sites->groupBy('name')->reduce(function ($result, $item) {
$result[] = [
'name' => $item->first()['name'],
'sites' => $item->pluck('site')->count() > 1 ? $item->pluck('site') : $item->first()['site']
];
return $result;
}, collect([]))->toArray();
which would produce...
[
[
"name" => "John",
"sites" => [
"Example",
"Another",
],
],
[
"name" => "Martin",
"sites" => "Another",
],
]
you can do it by chaining to get exactly what you want, assuming $collection is the main collection
$collection->groupBy('name')->map(function($facilitators) {
return ['name' => $facilitators->first()['name'], 'site' => $facilitators->pluck('site')->toArray()];
})->values()->toArray();
first we group by name so it will give 2 dimensional array inside collection, then iterate to that and name will be common so get it from first element, then from all the element pluck site and convert it to array, using flatMap will make it single level nested.

Categories