i have an array like below :
array:3 [
0 => array:1 [
1 => array:2 [
"type" => "validation"
"message" => "this item not valid"
]
]
1 => array:1 [
1 => array:2 [
"type" => "duplicate"
"message" => "This item is duplicate"
]
]
3 => array:1 [
2 => array:2 [
"type" => "duplicate"
"message" => "This item is duplicate"
]
]
]
i want to skip the array index and just return the nested items of array like below :
{
"1": {
"type": "validation",
"message: "this item not valid"
},
"1": {
"type": "duplicate",
"message: "this item not valid"
},
"2": {
"type": "validation",
"message: "this item not valid"
},
}
i want its type to be array so i can return it in laravel ValidationException::withMessages
thanks
CodePudding user response:
there is issue in you formatting if you will format your php array like this
[
[
0 => [
"type" => "validation",
"message" => "this item not valid"
]
],
[
1 => [
"type" => "duplicate",
"message" => "This item is duplicate"
]
],
[
2 => [
"type" => "duplicate",
"message" => "This item is duplicate"
]
]
]
Then your json result will be
[
{
"0":
{
"type": "validation",
"message": "this item not valid"
}
},
{
"1":
{
"type": "duplicate",
"message": "This item is duplicate"
}
},
{
"2":
{
"type": "duplicate",
"message": "This item is duplicate"
}
}]
CodePudding user response:
Try this code it might help you. $a is your unformatted array.
$a = array_values($a);
for ($i = 0; $i < count($a); $i ) {
$a[$i] = array_values($a[$i])[0];
}
return $a;