I have this JSON array list and want to validate to ensure min_distance and max_distance in array 0 are ways less than array 1... and vice-versa
array:2 [
"name" => "Run"
"pricing" => array:3 [
0 => array:3 [
"min_distance" => "1.0000"
"max_distance" => "2.0000"
"assign" => "8.0000"
]
1 => array:3 [
"min_distance" => "3.0000"
"max_distance" => "4.0000"
"assign" => "2.0000"
]
2 => array:3 [
"min_distance" => "4.0000"
"max_distance" => "5.0000"
"assign" => "50.0000"
]
]
]
You can do like this:
if($arr['pricing'][0]['min_distance'] > $arr['pricing'][1]['min_distance'] || $arr['pricing'][0]['max_distance'] > $arr['pricing'][1]['max_distance']) {
return Redirect::back()->withErrors(['msg', 'The Message']);
}
and in your view:
#if($errors->any())
<h4>{{$errors->first()}}</h4>
#endif
Related
I'm dealing with a specifically shaped array in Laravel where items are grouped by keys that represent names of categories.
In below example I have elements from categories 1, 5 and 76. Each element has a key called order which is a nullable integer that should indicate the order within this category.
array:3 [
"category_1" => array:4 [
1 => array:2 [
"option_name" => "Some name 1"
"option_items" => array:1 [
0 => array:5 [
"id" => 1
"price" => 200
"time" => 0
"order" => 999
]
]
]
2 => array:2 [
"option_name" => "Some name 2"
"option_items" => array:1 [
0 => array:5 [
"id" => 2
"price" => 780
"time" => 5
"order" => null
]
]
]
3 => array:2 [
"option_name" => "Some name 3"
"option_items" => array:1 [
0 => array:5 [
"id" => 3
"price" => 400
"time" => 4
"order" => null
]
]
]
4 => array:2 [
"option_name" => "Some name 4"
"option_items" => array:1 [
0 => array:5 [
"id" => 4
"price" => 300
"time" => 2
"order" => 434
]
]
]
]
"category_5" => array:2 [
6 => array:2 [
"option_name" => "Some name 5"
"option_items" => array:1 [
0 => array:5 [
"id" => 6
"price" => 890
"time" => 3
"order" => null
]
]
]
7 => array:2 [
"option_name" => "Some name 6"
"option_items" => array:1 [
0 => array:5 [
"id" => 7
"price" => 1290
"time" => 5
"order" => null
]
]
]
]
"category_76" => array:2 [
10 => array:2 [
"option_name" => "Some name 7"
"option_items" => array:1 [
0 => array:5 [
"id" => 10
"price" => 320
"time" => 4
"order" => 33
]
]
]
11 => array:2 [
"option_name" => "Some name 8"
"option_items" => array:2 [
0 => array:5 [
"id" => 11
"price" => 600
"time" => 0
"order" => 500
]
1 => array:5 [
"id" => 12
"price" => 2000
"time" => 9
"order" => 500
]
]
]
]
]
I simply want to sort this entire array per category by order key ascending (with nulls at the end), so for example the order of elements in category_1 would be:
Some name 4 (order of 434)
Some name 1 (order of 999)
Some name 2 (order is nor present / null)
Some name 3 (same as above)
What did you try?
Obviously I've tried converting this plain PHP array to collection and calling sortBy with a closure via:
$sorted = collect($array)->sortBy(function ($elementsInCategory, $key) {
dd($elementsInCategory); // $key is category_1
})->toArray();
but I simply don't know what to return from the callback to make it sort the way I've described.
BTW. I don't want to complicate too much but please notice that option_items sub-array may sometimes have more than one item (key 11 from category_76) but this does not matter - elements in this sub array will always have the same order value. This inner array should not be sorted - can be left as is.
You can use Laravel collection macro for that
$Collection::macro('sortByOrder', function (string $column = 'order', bool $descending = false) {
/* #var $this Collection */
return $this->sortBy(function ($order) use ($column) {
return strtotime(((object)$order)->$column);
}, SORT_REGULAR, $descending);
});
And you can use it like
$categories->sortByOrder('order',false);
I have an array like below :
array:3 [
"2021-08-07" => array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
"2021-08-08" => array:3 [
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
]
What I want to do is simply to remote the parent 2021-08-08 items because I have the date inside the array already . Now, what I have tried so far is :
$result = array_map(function ($el){
return $el[0];
},$test);
dd($result);
But it gives me error of undefined index[0] . what I want this array to look like is like below :
array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
],
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
Won't just array_values() do?
array_values($test);
$test = array_values(yourArray);
$test = (object)$yourArray;
I'm transforming my data into an array structure. Now I have the meta which already unserialized and I just need to take a specific value in meta.
Code
$item->meta;
$itm['attrs'] = #unserialize($item->meta);
$serial = #unserialize($item->meta);
$itm['product']['attrs'] = $itm['attrs'];
$itm['product']['serial'] = $serial['serial'];
Result
"product" => array:2 [
"attrs" => array:1 [
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
Expected Result
"product" => array:2 [
"attrs" => array:1 [
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
"serial" => "12345wf"
]
I'm not sure how to get the value and pass to the object.
Problem in splitting array in PHP.
I need pradm_policy_risk_attr_details_motor_id => 20170825113749907.
but in array only. Below is my example how to I needed in array
$array = [pradm_policy_risk_attr_details_motor_id => 20170825113749907,
column_11 => BP-2-B1534,
column_14 => Mahindra];
How can get this?
This is my array
array:19 [
"pradm_policy_risk_attr_details_motor_id" => array:1 [
0 => "20170825113749907"
]
"column_11" => array:1 [
0 => "BP-2-B1534"
]
"column_14" => array:1 [
0 => "Mahindra"
]
"column_15" => array:1 [
0 => "Bolero-Camper 2WD (2523 cc)"
]
"column_61" => array:1 [
0 => ""
]
"column_92" => array:1 [
0 => "0.000000"
]
"column_28" => array:1 [
0 => "[SELECT]"
]
"column_29" => array:1 [
0 => "Closed"
]
"column_30" => array:1 [
0 => "0"
]
"column_32" => array:1 [
0 => "Owner Driver"
]
"column_33" => array:1 [
0 => ""
]
"column_35" => array:1 [
0 => "Excavator"
]
"column_36" => array:1 [
0 => ""
]
"column_69" => array:1 [
0 => ""
]
"column_70" => array:1 [
0 => ""
]
"column_24" => array:1 [
0 => ""
]
"column_16" => array:1 [
0 => "Select"
]
"column_121" => array:1 [
0 => ""
]
"column_122" => array:1 [
0 => ""
]
]
If you need to 'flatten' the whole array, try array_map. The way it works is you define a function, that receives a single array item, transforms it and returns transformed value, then you pass that function to array_map in order to perform said transformation on all elements of the array:
function flatten($item){
return $item[0];
}
$original_array = Array(...); // assign your original array here
$flattened_array = array_map('flatten', $original_array);
Or using anonymous function syntax:
$flattened_array = array_map(function($item){return $item[0]}, $original_array);
In my project with Symfony2, I'm using Doctrine createNativeQuery, and I would like to get an associative array.
This is my code
$rsm = new ResultSetMapping;
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('phone_one', 'phoneOne');
$rsm->addScalarResult('phone_two', 'phoneTwo');
I have this result:
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
It's posible this result?
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
]
Thanks a lot
If that is the result you want, why not create a OneToMany for Users to Phones? I would highly suggest you do that.