Laravel PHP move collection item to the top of the collections - php

This is just in case someone else has the same question and like me did not find a suitable answer to solve it.
I had a collection that had to be filtered so the active item comes first on the collections when a certain value was passed.
Illuminate\Database\Eloquent\Collection {
0 => array:2 [
"id" => 1
"name" => "Bogan, Weissnat and Jenkins"
]
1 => array:2 [
"id" => 4
"name" => "Grady-Barrows"
]
2 => array:2 [
"id" => 7
"name" => "Howe and Sons"
]
3 => array:2 [
"id" => 3
"name" => "Macejkovic-Altenwerth"
]
]
}
Needed to move an item top based on the id which is passed by URL

$activeId = 3; // Your active item id
$collection = $collection
->sortBy('id')
->sortBy(fn($item) => $item->id !== $activeId);
This will sort your collection by id and move a specific item to the top.
(PHP7.4+ for arrow function)

You can simply sort the collection by a custom function:
use Illuminate\Support\Collection;
$data = collect([
["id" => 1, "name" => "Bogan, Weissnat and Jenkins"],
["id" => 4, "name" => "Grady-Barrows"],
["id" => 7, "name" => "Howe and Sons"],
["id" => 3, "name" => "Macejkovic-Altenwerth"],
]);
$key = "name";
$value = "Grady-Barrows";
public function moveFirst(Collection $data, string $key, mixed $value): Collection
{
return $data->sortBy(fn($v) => $v[$key] !== $value);
}
It will return false (0) for the matching entry and true (1) for the rest, so the matching entry gets put on top. Using arrow functions makes for a much simpler syntax.

This is how I did solve it.
public function moveOtherToTop($collection, $key, $item)
{
return $collection->reject(function ($value) use ($item){
return $value[$key] == $item;
})->prepend($collection->filter(function ($value) use ($item) {
return $value[$key] == $item;
})[$item]);
}
Idea came out from Laracast and Stillat article.
In case there is a better solution to this answer am open to suggestions.

I run this in Laravel 5.8
use Illuminate\Support\Collection;
function moveItemToTopCollection(Collection $collection, String $key, $value) :Collection
{
$item_to_first = null;
// Search element to top
foreach ($collection as $item) {
if ($item->$key == $value){
$item_to_first = $item;
break;
}
}
// If element not found, return original collection
if (!$item_to_first){
return $collection;
}
// Element to top, first remove of collection, then insert to top
return $collection->reject(function ($value) use ($item_to_first){
return $value == $item_to_first;
})->prepend($item_to_first);
}

You can use the prioritize method from the great spatie/laravel-collection-macros package.

Related

Create a new array from a unknown depth multidimensional and keep the same structure

I have a multidimensional array that can have any depth. What im trying to do is to filter the whole path based on dynamic keys and create a new array of it.
Example of the array
$originalArray = [
"title" => "BACKPACK MULTICOLOUR",
"description" => "description here",
"images" => [
[
"id" => 12323123123,
"width" => 635,
"height" => 560,
"src" => "https://example.com",
"variant_ids": [32694976315473, 32863017926737],
],
[
"id" => 4365656656565,
"width" => 635,
"height" => 560,
"src" => "https://example.com",
"variant_ids": [32694976315473, 32863017926737],
]
],
"price" => [
"normal" => 11.00,
"discount" => [
"gold_members" => 9.00,
"silver_members" => 10.00,
"bronze_members" => null
]
]
];
Example how the output should look like with the key "title, width, height, gold_members" filtered out. Only keys from the end of the array tree should be valid, so nothing must happen when images is in the filter
$newArray = [
"title" => "BACKPACK MULTICOLOUR",
"images" => [
[
"width" => 635,
"height" => 560,
],
[
"width" => 635,
"height" => 560,
]
],
"price" => [
"discount" => [
"gold_members" => 9.00,
]
]
];
I guess that i should create a function that loop through each element and when it is an associative array, it should call itself again
Because the filtered paths are unknown i cannot make a hardcoded setter like this:
$newArray["images"][0]["width"] = 635
The following filter will be an example but it should basically be dynamic
example what i have now:
$newArray = handleArray($originalArray);
handleArray($array)
{
$filter = ["title", "width", "height", "gold_members"];
foreach ($array as $key => $value) {
if (is_array($value)) {
$this->handleArray($value);
} else {
if (in_array($key, $filter)) {
// put this full path in the new array
}
}
}
}
[Solved] Update:
I solved my problem thanks to #trincot
I used his code and added an extra check to add an array with multiple values to the new array
My code to solve the issue:
<?php
function isListOfValues($array) {
$listOfArrays = [];
foreach ($array as $key => $value) {
$listOfArrays[] = ! is_array($value) && is_int($key);
}
return array_sum($listOfArrays) === count($listOfArrays);
}
function filterKeysRecursive(&$arr, &$keep) {
$result = [];
foreach ($arr as $key => $value) {
if (is_array($value) && ! isListOfValues($value)) {
$value = filterKeysRecursive($value, $keep);
if (count($value)) {
$result[$key] = $value;
}
} else if (array_key_exists($key, $keep)) {
$result[$key] = $value;
}
}
return $result;
}
$keep = array_flip(["title", "width", "height", "gold_members"]);
$result = filterKeysRecursive($originalArray, $keep);
You could use a recursive function, with following logic:
base case: the value associated with a key is not an array (it is a "leaf"). In that case the new object will have that key/value only when the key is in the list of desired keys.
recursive case: the value associated with a key is an array. Apply recursion to that value. Only add the key when the returned result is not an empty array. In that case associate the filtered value to the key in the result object.
To speed up the look up in the list of keys, it is better to flip that list into an associative array.
Here is the implementation:
function filter_keys_recursive(&$arr, &$keep) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
$value = filter_keys_recursive($value, $keep);
if (count($value)) $result[$key] = $value;
} else if (array_key_exists($key, $keep)) {
$result[$key] = $value;
}
}
return $result;
}
$originalArray = ["title" => "BACKPACK MULTICOLOUR","description" => "description here","images" => [["id" => 12323123123,"width" => 635,"height" => 560,"src" => "https://example.com"],["id" => 4365656656565,"width" => 635,"height" => 560,"src" => "https://example.com"]],"price" => ["normal" => 11.00,"discount" => ["gold_members" => 9.00,"silver_members" => 10.00,"bronze_members" => null]]];
$keep = array_flip(["title", "width", "height", "gold_members"]);
$result = filter_keys_recursive($originalArray, $keep);
My proposition to you is to write a custom function to transform structure from one schema to another:
function transform(array $originalArray): array {
array_walk($originalArray['images'], function (&$a, $k) {
unset($a['id']); unset($a['src']);
});
unset($originalArray['description']);
unset($originalArray['price']['normal']);
unset($originalArray['price']['discount']['silver_members']);
unset($originalArray['price']['discount']['bronze_members']);
return $originalArray;
}
var_dump(transform($originalArray));
If you are familiar with OOP I suggest you to look at how DTO works in API Platform for example and inject this idea into your code by creating custom DataTransformers where you specify which kind of structers you want to support with transformer and a method where you transform one structure to another.
Iterate over the array recursively on each key and subarray.
If the current key in the foreach is a required key in the result then:
If the value is not an array, simply assign the value
If the value is an array, iterate further down over value recursively just in case if there is any other filtering of the subarray keys that needs to be done.
If the current key in the foreach is NOT a required key in the result then:
Iterate over value recursively if it's an array in itself. This is required because there could be one of the filter keys deep down which we would need. Get the result and only include it in the current subresult if it's result is not an empty array. Else, we can skip it safely as there are no required keys down that line.
Snippet:
<?php
function filterKeys($array, $filter_keys) {
$sub_result = [];
foreach ($array as $key => $value) {
if(in_array($key, $filter_keys)){// if $key itself is present in $filter_keys
if(!is_array($value)) $sub_result[$key] = $value;
else{
$temp = filterKeys($value, $filter_keys);
$sub_result[$key] = count($temp) > 0 ? $temp : $value;
}
}else if(is_array($value)){// if $key is not present in $filter_keys - iterate over the remaining subarray for that key
$temp = filterKeys($value, $filter_keys);
if(count($temp) > 0) $sub_result[$key] = $temp;
}
}
return $sub_result;
}
$result = filterKeys($originalArray, ["title", "width", "height", "gold_members"]);
print_r($result);
Online Demo
Try this way.
$expectedKeys = ['title','images','width','height','price','gold_members'];
function removeUnexpectedKeys ($originalArray,$expectedKeys)
{
foreach ($originalArray as $key=>$value) {
if(is_array($value)) {
$originalArray[$key] = removeUnexpectedKeys($value,$expectedKeys);
if(!is_array($originalArray[$key]) or count($originalArray[$key]) == 0) {
unset($originalArray[$key]);
}
} else {
if (!in_array($key,$expectedKeys)){
unset($originalArray[$key]);
}
}
}
return $originalArray;
}
$newArray = removeUnexpectedKeys ($originalArray,$expectedKeys);
print_r($newArray);
check this on editor,
https://www.online-ide.com/vFN69waXMf

If statement inside PHP object

Given the below code, how do I add an if statement inside obj so that the final PHP result does not display a field if it has a NULL value? For example, if id_info is NULL, then do not display "id":[id_info] in the actual PHP page? I couldn't find this info anywhere in the documentation.
<?php
$id_info = ($db->query("SomeSQL query")->fetch_assoc())['id'];
$name_info = ....;
//some more queries
$obj = (object) ["id" => strval($id_info),
"Name" => (object) [
"eng_name" => strval($name_info)
]];
echo json_encode($obj);
?>
As mentioned by knittl you could check if a specific value is null and not add it to your object.
If it is necessary though to dynamically create objects withouth the hustle of checking. You have to use array_filter or any other custom filtering function for that.
I wrote a custom filtering function that accepts a deeply nested array and returns it back filtered.
function arrayFilter($inputArr){
$output = null;
if (is_array($inputArr)){
foreach ($inputArr as $key=>$val){
if(!$inputArr[$key]) continue;
if (is_array($val)) {
$tmpArr = arrayFilter($val);
if($tmpArr) $output[$key] = array_filter($tmpArr);
}
else $output[$key] = $val;
}
} else {
$output[$key] = $val;
}
return $output;
}
So, lets say you have a deeply nested object similar to the one you provided
$obj = (object) [
"id" => null,
"Name" => (object) [
"eng_name" => strval('some name2'),
"de_name" => null,
"more" => (object) [
"fr_name" => strval('some name3'),
"ru_name" => null,
]
]
];
Below i convert the stdClass object you have to an array with json_encode and json_decode and if you pass it as a parameter into the function like:
$filtered = arrayFilter(json_decode(json_encode($obj), true));
Your output will be something like the following:
{
"Name":{
"eng_name":"some name2",
"more":{
"fr_name":"some name3"
}
}
}
Simply don't add it to the object in the first place:
$obj = [ "Name" => [ "eng_name" => strval($name_info) ] ];
if ($id_info != null) {
$obj["id"] = strval($id_info);
}

Arrange Array in PHP

How can I sort the array ["Name":"OVERALL"] always last in the array element. This array ["Name":"OVERALL"] always on index 1. I think my current method is not a really good implementation. Is there any better ways?
[
["Name":"AHMAD SUFFIAN BIN AHMAD LOTFI","SLAData":[0,0,0,0,0,0],"RatingData":[0,0,0,0,0,0]],
["Name":"OVERALL","SLAData":[19,8,50,0,0,100],"RatingData":[95,95,100,0,0,0]],
["Name":"JAYALETCHUMI A\/P VENGADASALAM","SLAData":[33,14,100,0,0,0],"RatingData":[90,90,100,0,0,0]],
["Name":"MOHAMMAD FIRDHAUS BIN ISMAIL","SLAData":[0,0,0,0,0,0],"RatingData":[100,100,0,0,0,0]],
["Name":"YOGESWARAN A\/L PUSSAN","SLAData":[0,0,0,0,0,0],"RatingData":[0,0,0,0,0,0]],
["Name":"JAYAKUMAR PARAMASIVAM","SLAData":[0,0,0,0,0,100],"RatingData":[0,0,0,0,0,0]]
]
This is my current method
$Temp=[];
$output = array_slice($MonthlyData, 1, 1);
foreach ($MonthlyData as $data) {
if($data['Name']!='OVERALL')
$Temp[] = $data;
}
}
$Temp[] = $output;
$MonthlyData = $Temp;
There's multiple ways to do this. I chose a way that doesn't require changing the code you provided much. All this code does is add the elements to a front of a temprary array unless it is the one you want to be last. That one it puts on the end of the array.
$MonthlyData = [
["Name" => "AHMAD SUFFIAN BIN AHMAD LOTFI","SLAData" => [0,0,0,0,0,0],"RatingData" => [0,0,0,0,0,0]],
["Name" => "OVERALL","SLAData" => [19,8,50,0,0,100],"RatingData" => [95,95,100,0,0,0]],
["Name" => "JAYALETCHUMI A\/P VENGADASALAM","SLAData" => [33,14,100,0,0,0],"RatingData" => [90,90,100,0,0,0]],
["Name" => "MOHAMMAD FIRDHAUS BIN ISMAIL","SLAData" => [0,0,0,0,0,0],"RatingData" => [100,100,0,0,0,0]],
["Name" => "YOGESWARAN A\/L PUSSAN","SLAData" => [0,0,0,0,0,0],"RatingData" => [0,0,0,0,0,0]],
["Name" => "JAYAKUMAR PARAMASIVAM","SLAData" => [0,0,0,0,0,100],"RatingData" => [0,0,0,0,0,0]]
];
$Temp=[];
foreach ($MonthlyData as $data) {
if ($data['Name'] === 'OVERALL') {
$Temp[] = $data;
}
else {
array_unshift($Temp, $data);
}
}
$MonthlyData = $Temp;
Demo
If you want a more concise way to do it, use usort() to sort based on a specified criteria. In this case, always put an array last if it's "name" value is "OVERALL".
usort($MonthlyData, static function ($a, $b) {
return ($a['Name'] === 'OVERALL') ? 1 : -1;
});
Demo

PHP: Getting values from array1 with from nested arrays

I have two arrays. $array2 to check whether $array1 value exist in 'slug'.
$array1 = [
0 => "tenants",
1 => "modules",
];
$array2 = [
"child" => [
"prefix" => "tenants",
"slug" => "tenants",
"child" => [
[
"prefix" => "modules/{id}",
"slug" => "modules"
],
[
"prefix" => "fetch/{id}",
"slug" => "fetch"
],
],
],
];
My Code:
foreach ($array1 as $value) {
array_walk_recursive($array2['child'],
function ($item, $key) use (&$result, &$res, &$value) {
if ($key == "slug") {
if ($item == $value) {
$res[] = $item;
}
}
if ($key == 'prefix') {
$result[] = $item;
}
});
}
Here I used array_walk_recursive to check every if array1 is exist in slug. But i want to get also it's prefix.
In my code above It will not get the modules/{id} since it's not equal to slug.
Example:
$array1 = [
0 => "tenants",
1 => "modules",
];
Result:
array:3 [▼
1 => "tenants"
2 => "modules/{id}"
]
Code: https://3v4l.org/E18Uq
Happy coding.
You don't need to use array_walk_recursive just use custom function as:
function getFromChild($item, $asd) {
if (!is_array($item)) return [];
$res = [];
if ($item["slug"] == $asd) // if have slug check and add if needed
$res[] = $item['prefix'];
if (isset($item['child']))
foreach($item['child'] as $child) // for all child loop and add
$res = array_merge($res, getFromChild($child, $asd));
return $res;
}
Now you can call it with:
$res = [];
foreach ($array1 as $asd) {
$res = array_merge($res, getFromChild($array2, $asd));
}
Live example: 3v4l

Pluck id (integer) cast to string Laravel

While plucking from a database, I get id as strings.
$alphabets = new Alphabet();
return $alphabets->pluck('name', 'id');
Output
{
"1": "Apple",
"2": "Ball",
"3": "Cat"
}
Expected
{
1: "Apple",
2: "Ball",
3: "Cat"
}
But, when I reverse ID and name,
return $alphabets->pluck('id', 'name');
I get id as integer.
{
"Apple": 1,
"Ball": 2,
"Cat": 3
}
I'm not sure what's happening behind the scene. But how can I get ID in integer ? Actually, old flash session doesn't set value because of 1 vs "1" in Form Collective.
{!! Form::select('alphabet', $alphabets, null, ['class' => 'form-control', 'multiple' => true]) !!}
Try this code
$alphabets = new Alphabet();
return $alphabets->all()->pluck('name', 'id');
Alphabet.php
You should cast your columns like this.
protected $casts = [
'id' => 'integer',
'name' => 'string'
];
I think I found the answer here.
https://laracasts.com/discuss/channels/laravel/pluck-id-integer-cast-to-string
Here I found JSON only allows key names to be strings.
Using number as "index" (JSON)
{
"1": "Apple",
"2": "Ball",
"3": "Cat"
}
Actually, I want to achieve it for Form Collective. It was a bug and it's PR has been merged now.
https://github.com/LaravelCollective/html/pull/368#pullrequestreview-46820423
you also convert key into int
$alphabets = new Alphabet();
$alphaArr =$alphabets->pluck('name', 'id');
foreach($array as $key => $value) {
$newArray[(int) $key] = $value;
}
Usually, pluck() method gives you associative array of values
in string values.
So, try using select statements like this:
$data = Alphabet::select('id','name')->get()->toArray();
This will give you following result:
array:3 [▼
0 => array:2 [▼
"id" => 1
"name" => "Apple"
]
1 => array:2 [▼
"id" => 2
"name" => "Ball"
]
2 => array:2 [▼
"id" => 3
"name" => "Cat"
]
]
Now, using simple loop you can get your expected array.
$expected = array();
foreach($data as $d){
$expected[$d['name']] = $d['id'];
}
dd($expected);
Adding this line fix the old session issue of LaravelCollective/Html.
|| in_array((string) $value, $selected, true)
/**
* Determine if the value is selected.
*
* #param string $value
* #param string $selected
*
* #return null|string
*/
protected function getSelectedValue($value, $selected)
{
if (is_array($selected)) {
return in_array($value, $selected, true) || in_array((string) $value, $selected, true) ? 'selected' : null;
} elseif ($selected instanceof Collection) {
return $selected->contains($value) ? 'selected' : null;
}
return ((string) $value == (string) $selected) ? 'selected' : null;
}

Categories