I met some difficulties when I was trying to get value from an array in my php blade.
It has rather clear structure (printed with dd function)
{{dd($attr)}}
array:4 [▼
"id" => "215"
"type" => "select"
"name" => "Status"
"value" => array:2 [▼
"pred" => array:3 [▼
0 => "Employed"
1 => "On vacation"
2 => "Dismissed"
]
"sel_val" => "0"
]
]
And when I want to get a value by key 'sel_val' or 'pred'
print_r($attr['value']['pred']);
it gives me Illegal string offset 'pred'
And it works nice in Controller. What should i do?
It gives that error because pred is also an array. you'll have to do $attr['value']['pred'][0] to get Employed, $attr['value']['pred'][1] to get On vacation, $attr['value']['pred'][2] to get Dismissed and $attr['value']['sel_val'] to get the value of sel_val which is 0 in this case. Hope this helps.
working fine when we send array in compact function in the controller
$record = array('id' => '215', 'type' => 'select', 'value' => array('pred' => array('0'=> 'Employed', '1' => 'On vacation', '2' => 'Dismissed'),'sel_val' => '0'));
return view('home', compact('record'));
Related
I have an array like this, after I use json_decode ($json = json_decode($items, true);), the result looks like this
array:1 [
61 => array:53 [
"id" => 2790
"name" => "ABC"
"created_at" => "2020-12-04 09:43:57.317"
"updated_at" => "2021-02-16 16:47:16.167"
"deleted_at" => null
"remark" => null
]
]
And Iwant to get the value from field name, how to do it in Laravel??
I try like this but, get error Undefined offset name
dd($array['name']);
The correct way to access the element you're trying to get to is $array[61]["name"];
$array[61] will give you:
array:53 [
"id" => 2790
"name" => "ABC"
"created_at" => "2020-12-04 09:43:57.317"
"updated_at" => "2021-02-16 16:47:16.167"
"deleted_at" => null
"remark" => null
]
Then you can use ["name"] to access the value you're after.
I have the following multidimensional array.
"PM" => array:6 [▼
0 => "Zeb"
1 => "Pen"
2 => "Zeb"
3 => "Eds"
4 => "Fsa"
5 => "Zeb"
]
"OS" => array:3 [▼
0 => "Min"
1 => "Kep"
2 => "Min"
]
"IT" => array:8 [▶]
]
And I would like to remove the duplicate values from the nested arrays. In this case, have only one value Zeb in PM and Min in OS. Please keep in mind that, I don't know in which array there are duplicates so I need a way to check all the nested arrays for duplicates.
Thank you.
$arr = [
"PM" => ["Zeb", "Pen", "Zeb", "Eds", "Fsa", "Zeb"],
"OS" => ["Min", "Kep", "Min"],
];
$cleanArray = collect($arr)->map(fn($subarr) => array_unique($subarr))->toArray();
will produce:
[
"PM" => ["Zeb", "Pen", "Eds", "Fsa"],
"OS" => ["Min", "Kep"]
]
I have a trouble validating an array members in validation for Laravel. For array itself it passes, but for elements themselves, it fails.
Open json in console, shows structure of data being sent. It's clearly a integer. Since it's associative array, not sure how it even finds it's value as show in error handling red rectangle on the bottom of the page.
Here is my validation logic:
$validation = Validator::make(
$request->all(),
[
'load_place' => 'required|max:255',
"unload_place" => 'required|max:255',
"comment" => 'required|max:255',
'time_in' => 'required',
'time_out' => 'required',
"vehicles" => 'required|array',
"vehicles.*" => "required|integer",
'operator_id' => 'required|integer',
'sec_id' => 'required|integer'
]
);
And here is how it's returned:
if($validation->fails()){
$response = array(
"message" => "Failed",
"errors" => $errors,
"test" => $request->vehicles[0]
);
return response()->json($response);
}
That request[0], is that number at the bottom.
Edit1:
Keys in that marked array, are set on frontend, they are not not predefined. They can be whatever.
Change "vehicles.*" => "required|integer", to "vehicles.*.vehicle_id" => "required|integer", and it should work.
Keys is set on frontend, they are not not predefined. That what i am trying to say.
Try to replace "vehicles.*" => "required|integer", with new Rule::forEach rule for nested array data - guess it should work.
"vehicles.*" => Rule::forEach(function($value, $attribute) {
return [
Rule::integer()->min(1) //min(1) as eg if it's for an id field which cannot be 0
];
}),
I have this collection
1 => {#27
+"id": 1
+"name": "Zel"
+"age": "43"
}
2 => {#28
+"id": 2
+"name": "Kol"
+"age": "56"
}
3 => {#29
+"id": 3
+"name": "Mil"
+"age": "32"
}
and I would like to return an array with the key values as a string like this:
[
'id',
'name',
'age',
]
Can someone help me with that, please?
use array_keys :
$keys = array_keys($collection->first());
Laravel collection has a keys() method you could simply use it like this:
$keys = $collection->keys();
$get = $keys->all();
It is all clearly written in the Laravel Documentation
EDIT
After looking at your edit, my first consideration would be that if your collection is consistent you could get the first one and subsequently get the keys from there on:
$keys = $collection->first();
$get = $keys->keys()->all();
Or simply put $collection->first()->keys()->all();
EDIT
Here is how i was able to reproduce your problem:
$collection = collect([
[
'id' => 1,
'name' => 'Zel',
'age' => 43
],
[
'id' => 2,
'name' => 'Kol',
'age' => 56
],
[
'id' => 3,
'name' => 'Mil',
'age' => 32
],
]);
$keys = collect($collection->first())->keys()->all();
Here is the Result I got:
array:3 [▼
0 => "id"
1 => "name"
2 => "age"
]
If it still returns a collection or an object based on your last comment, you could try any one of these:
$keys = $keys->toArray();
$keys = collect($keys)->toArray();
My array looks like this:
array:145 [▼
144 => array:2 [▼
0 => 1559739600000
1 => 39103.5828125
]
143 => array:2 [▼
0 => 1559739000000
1 => 39102.619270833
]
142 => array:2 [▼
0 => 1559738400000
1 => 39101.740234375
]
...
I need to change the 0s to time and 1s to data, so I can work with them within Laravel. If I try to reference them within Laravel like $0 I get T_Variable errors because of the rules that variables should not start with numbers. So I'm in a bind. So it ends up looking something like this:
array:145 [▼
144 => array:2 [▼
time => 1559739600000
data => 39103.5828125
]
143 => array:2 [▼
time => 1559739000000
data => 39102.619270833
]
142 => array:2 [▼
time => 1559738400000
data => 39101.740234375
]
...
You could do something like this using Laravel Collection.
$result = collect($array)->map(function ($item) {
return [
'time' => $item[0],
'data' => $item[1],
];
})->all();
I find this approach quite common in the Laravel community. For more info, pls check: https://laravel.com/docs/5.8/collections