I need a certain array format like this:
$data = [
1 => ['order' => 3],
2 => ['order' => 2],
3 => ['order' => 1]
];
So when I do:
$ids = $items->transform(function ($item) {
return [$item->id => ['order' => rand(1,10)]];
})->all();
Gives:
array:3 [
0 => array:1 [
1 => array:1 [
"order" => 8
]
]
1 => array:1 [
2 => array:1 [
"order" => 3
]
]
2 => array:1 [
3 => array:1 [
"order" => 10
]
]
]
How can I transform the array in a specific way I need above? TIA
you can use mapWithKeys, docs is here: https://laravel.com/docs/5.5/collections#method-mapwithkeys
$keyed = $items->mapWithKeys(function ($item) {
return [$item->id => ['order' => rand(1,10)]];
});
$ids = $keyed->all();
Related
i'm kinda new with Laravel Framework. And now confused with data type like array and object. I got a data like this :
"data[0].detailItem" => array:2 [
0 => array:3 [
"itemNo" => "100011"
"unitPrice" => 0
"quantity" => 6
]
1 => array:3 [
"itemNo" => "102972"
"unitPrice" => 0
"quantity" => 3
]
]
is there a way I change it into :
"data[0].detailItem" => [
{
"itemNo" => "100011"
"unitPrice" => 0
"quantity" => 6
},
{
"itemNo" => "102972"
"unitPrice" => 0
"quantity" => 3
}
]
Edit :
Heres the code
public function apiStore(Request $request){
$detailReceiveItem = DB::table('t_receive_item_detail')->where('receive_item_id','=',$request->idReceiveItem)->get();
$detailItem = [];
foreach($detailReceiveItem as $key=>$receiveItem){
$temp = [
'itemNo' => $receiveItem->item_code,
'unitPrice' => 0,
'quantity' => $receiveItem->qty,
];
array_push($detailItem,$temp);
}
$input = [
'data[0].vendorNo' => $request->vendorNo,
'data[0].receiveNumber' => $request->receiveNumber,
'data[0].transDate' => date('d/m/Y',strtotime($request->transDate)),
'data[0].detailItem' => (object)$detailItem
];
$insertReceiveItem = Http::withHeaders([
'Authorization' => 'Bearer '.session()->get('access_token'),
'X-Session-ID' => session()->get('X-Session-ID'),
])->asForm()->post(session()->get('host').'api url here',$input);
return response()->json(['status'=>1, 'url'=>'/api-accurate/receive-item','message'=>'Receive Item Created Successfully!']);
}
That's my code. I need to convert $detailItem into a object.
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 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
$response['inserted_data'] =
array:3 [
0 => array:16 [
"name" => "Khv"
"emails" => array:1 [
0 => array:1 [
"email" => "demo#yahoo.com"
]
]
]
1 => array:18 [
"name" => "Aesha"
"emails" => array:1 [
0 => array:1 [
"email" => "test2#gmail.com"
]
]
]
How to get email values demo#yahoo.com and test2#gmail.com using pluck - php laravel and then from that emails need to get _id from database and update that records.
Try the below code :
Assuming your data look likes below array i.e $arrays.
$arrays=[
["name" => "Khv","emails" =>
[0 => ["email" => "demo1#yahoo.com"]
]
],
["name" => "asy", "emails" =>
[0 => ["email" => "demo2#yahoo.com"]
]
]
];
$emails=collect();
foreach ($arrays as $array) {
$emails->push($array['emails'][0]['email']);
}
dd($emails);
Note : You can't use pluck on array, it can be only used on collection.
You get the value from the pluck like below way.
User::all()->pluck('email');
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);