convert array to array of object Laravel - arrays

my api receive single array of object from client side like following:
[
{
"datetime": "2022-07-19 12:04:03",
"receiptNumber" : "4010",
"isRefund": 0,
"amount" : 1025
},
]
and whem try to validate request at api side checked success and process to database success my issue happen when try to sent multi object inside array like following :
[
{
"datetime": "2022-07-19 12:04:03",
"receiptNumber" : "4010",
"isRefund": 0,
"amount" : 1025
},
{
"datetime": "2022-07-19 12:04:03",
"receiptNumber" : "4009",
"isRefund": 0,
"amount" : 1025
}
]
when i am try to validate first object inside array using foreach like following :
$array = $request->all();
foreach($array as $sdsa){
$this->validate($sdsa, [
'datetime' => 'required|date_format:Y-m-d H:i:s',
'amount' => 'required|numeric',
'isRefund' => 'required|integer|between:0,1',
'receiptNumber' => 'required|string',
]);
}
in this case api return error :
"message": "Argument 1 passed to App\\Http\\Controllers\\Controller::validate() must be an instance of Illuminate\\Http\\Request, array given,
so that i am asking how can convert this format:
array:4 [
"datetime" => "2022-07-19 12:04:03"
"receiptNumber" => "4010"
"isRefund" => 0
"amount" => 1025
]
to
{
array:4 [
"datetime" => "2022-07-19 12:04:03"
"receiptNumber" => "4010"
"isRefund" => 0
"amount" => 1025
]
}

You can use 'json_encode()'.
Example;
$myArray = array("datetime" => "2022-07-19 12:04:03", "receiptNumber" => "4010", "isRefund" => 0, "amount" => 1025);
Output;
[
"datetime" => "2022-07-19 12:04:03",
"receiptNumber" => "4010",
"isRefund" => 0,
"amount" => 1025,
]
Than if you use json_encode like this;
$myArray = json_encode($myArray);
Output;
{
[
"datetime" => "2022-07-19 12:04:03",
"receiptNumber" => "4010",
"isRefund" => 0,
"amount" => 1025,
]
}

You need to use
Validator::make($sdsa, [
'datetime' => 'required|date_format:Y-m-d H:i:s',
'amount' => 'required|numeric',
'isRefund' => 'required|integer|between:0,1',
'receiptNumber' => 'required|string',
])->validate()

Related

Using validated method with array in Laravel

This is the rquest body:
{
"arr": [
{
"a": 3,
"b": 3
},
{
"a": 3,
"b": 3
}
]
}
and with this validations rules:
public function rules()
{
return [
// 'arr' => 'array',
'arr.*.a' => 'required'
];
}
The result of dd($request->validated()); is what I want, I don't need b to be included
"arr" => array:2 [
0 => array:1 [
"a" => 3
]
1 => array:1 [
"a" => 3
]
]
But when I want to validate the array itself like this:
public function rules()
{
return [
'arr' => 'array',
'arr.*.a' => 'required'
];
}
b will be added to the output
"arr" => array:2 [
0 => array:1 [
"a" => 3,
"b" => 3
]
1 => array:1 [
"a" => 3,
"b" => 3
]
]
How can I prevent adding b to the dd($request->validated()); while validating the array itself?

Perl: How to iterate over arrays in multiple objects and extract values from webservice

I request a web service and get a JSON response:
{
"timestamp" : "2019-06-11T08:04:35Z",
"version" : "0.5",
"document" : [
{
"href" : "http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239",
"item" : [
{
"href" : "http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239",
"label" : "40 623 a",
"id" : "http://uri.gbv.de/document/opac-de-7:epn:3421084610",
"available" : [
{
"service" : "presentation"
}
],
"unavailable" : [
{
"service" : "loan"
},
{
"service" : "interloan"
}
]
},
{
"href" : "http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239",
"label" : "40 623 b",
"id" : "http://uri.gbv.de/document/opac-de-7:epn:342108467X",
"available" : [
{
"service" : "presentation"
}
],
"unavailable" : [
{
"service" : "loan"
},
{
"service" : "interloan"
}
]
}
],
"id" : "http://uri.gbv.de/document/opac-de-7:ppn:1629107239"
}
],
"institution" : {
"href" : "http://www.sub.uni-goettingen.de",
"content" : "Niedersächsische Staats- und Universitätsbibliothek Göttingen",
"id" : "http://uri.gbv.de/organization/isil/DE-7"
}
}
Then I parse the JSON with Perls Dumper::Data module:
my $data = decode_json($resultJson);
print Dumper($data);
It looks like:
$VAR1 = {
'document' => [
{
'item' => [
{
'available' => [
{
'service' => 'presentation'
}
],
'id' => 'http://uri.gbv.de/document/opac-de-7:epn:3421084610',
'href' => 'http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239',
'label' => '40 623 a',
'unavailable' => [
{
'service' => 'loan'
},
{
'service' => 'interloan'
}
]
},
{
'available' => [
{
'service' => 'presentation'
}
],
'unavailable' => [
{
'service' => 'loan'
},
{
'service' => 'interloan'
}
],
'id' => 'http://uri.gbv.de/document/opac-de-7:epn:342108467X',
'label' => '40 623 b',
'href' => 'http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239'
}
],
'id' => 'http://uri.gbv.de/document/opac-de-7:ppn:1629107239',
'href' => 'http://opac.sub.uni-goettingen.de/DB=1/PPNSET?PPN=1629107239'
}
],
'institution' => {
'content' => "Nieders\x{e4}chsische Staats- und Universit\x{e4}tsbibliothek G\x{f6}ttingen",
'id' => 'http://uri.gbv.de/organization/isil/DE-7',
'href' => 'http://www.sub.uni-goettingen.de'
},
'timestamp' => '2019-06-11T08:04:35Z',
'version' => '0.5'
};
I would like to display the availability of each item, but I'm having a hard time iterating through the arrays inside the objects. My actual code looks like:
my $availability = $data->{document}[0]->{item}[0]->{available};
foreach my $key (#{$availability}) {
if (($key->{'service'}) eq "loan") {
print $rueckgabe = "Loan: available.\n";
} elsif (($key->{'service'}) eq "presentation") {
print $rueckgabe = "Presentation: available.\n";
} elsif (($key->{'service'}) eq "interloan") {
print $rueckgabe = "ILL: available.\n";
}
}
My expected result would be:
Item 1: Loan: available.
Item 2: Loan: available.
Iterate over the items, for each item, iterate over the availabilities.
for my $i (0 .. $#{ $data->{document}[0]{item} }) {
print 'Item ', $i + 1, ': ';
for my $availability ($data->{document}[0]{item}[$i]{available}) {
for my $key (#$availability) {
print { interloan => 'ILL' }->{ $key->{service} } || ucfirst $key->{service},
": available\n";
}
}
}

How can I use array_replace inside foreach loop?

colums_arr:
array:4 [▼
0 => "id"
1 => "name"
2 => "productgroup"
3 => "category"
]
fields:
{#9767 ▼
+"id": array:9 [▶]
+"name": array:8 [▼
"fieldName" => "name"
]
+"productgroup": array:19 [▼
"fieldName" => "productgroup"
"mappedBy" => null
]
+"category": array:19 [▼
"fieldName" => "category"
"mappedBy" => null
]
}
What I want to do is, whenever in fields mappedBy exists for the element, I want to add name to the value. So as a result columns_arr should look like this:
array:4 [▼
0 => "id"
1 => "name"
2 => "productgroup.name"
3 => "category.name"
]
This is my approach:
foreach ($fields as $field) {
$MappedBy = isset($field['mappedBy']);
if($MappedBy != true){
$class_field = $field['fieldName'];
$key = array_search($field['fieldName'],$input);
$replace=array($key=>$class_field.".name");
$columns_arr = (array_replace($input,$replace));
}
}
The problem is, that my result is now:
array:4 [▼
0 => "id"
1 => "name"
2 => "productgroup"
3 => "category.name"
]
Why is name not added to productgroup?
I couldn't really answer your question, it's a bit unclear what you're doing and where your values are coming from etc. so I tried to rebuild your arrays and this is what I got without using array_search:
<?php
$columns = array(0 => "id", 1 => "name", 2 => "productgroup", 3 => "category");
$fields = array("id" => 9, "name" => array("fieldName" => "name"), "productgroup" => array("fieldName" => "productgroup", "mappedBy" => null), "category" => array("fieldname" => "category", "mappedBy" => null));
foreach ($fields as $key => $field) {
if($key !== "id" && $key !== "name"){
if(!isset($field['mappedBy'])){
foreach($columns as $ckey => $column){
if($column === $key){
$columns[$ckey] = $column.".name";
}
}
}
}
}
var_dump($columns);
var_dump outputs:
array(4) { [0]=> string(2) "id" [1]=> string(4) "name" [2]=> string(17) "productgroup.name" [3]=> string(13) "category.name" }
foreach ($fields as $key => $field) {
$MappedBy = isset($field['mappedBy']);
if($MappedBy != true){
$columns_arr[$key] = $field['fieldName'].".name";
}
}
You need to overwrite the correct key of your array like this.

how to select elements in array from multiple elements in another array

I have an array A that looks like this:
A = [ { "id" => "1234", "name" => "audi", "isCool" => false },
{ "id" => "5678", "name" => "acura", "isCool" => false },
{ "id" => "9101112", "name" => "bentley", "isCool" => true },
{ "id" => "13141516", "name" => "rollsroyce", "isCool" => true },
{ "id" => "17181920", "name" => "toyota", "isCool" => true } ]
and I have an array B that looks like this:
B = ["1234", "13141516”]
I am trying to select only elements from array A that match array A's ids with Array Bs elements.
So the returned results I would like is:
C = [ { "id" => "1234", "name" => "audi", "isCool" => false },
{ "id" => "13141516", "name" => "rollsroyce", "isCool" => true } ]
Is there an easy way to go about this?
I have currently tried this but obviously not a good idea:
a.select {|x| x['id'] == B.first || B.last}
But obviously this is not dynamic, because what if I had 3 or 4 elements in array B.
A.select { |x| B.include?(x['id']) }

How do I access request arrays within arrays

I have the following request;
Collection {#278
#items: array:3 [
0 => array:2 [
0 => array:8 [
"id" => 631
"name" => "OIL, FILTER O.E.M."
"partno" => "235-00"
"oemnumber" => "16099 003"
"stock" => 0
"price" => "30"
"qty" => 1
"total" => 30
]
1 => array:8 [
"id" => 23
"name" => "SPEEDOMETER"
"partno" => "122-"
"oemnumber" => "25005 1013"
"stock" => 0
"price" => "276"
"qty" => 1
"total" => 276
]
]
1 => array:2 [
0 => array:2 [
"description" => "Oil change"
"hours" => "1"
]
1 => array:2 [
"description" => "Tune up"
"hours" => "2"
]
]
2 => array:15 [
"id" => 1
"custId" => 9046
"bikeId" => 5238
"trans" => "yes"
"transDetails" => "call cab"
"policies" => "Yes"
"locker" => "1"
"lockerContents" => "stuff"
"estimate" => "Yes"
"oldparts" => "Yes"
"status" => "Pending"
"created_by" => null
"created_at" => "2016-05-19 14:40:59"
"updated_by" => null
"updated_at" => "2016-06-08 09:06:58"
]
]
}
I am getting this through;
$collection = collect($request->all());
How should I go about accessing the attributes in these arrays? I have tried pluck with no joy. I suspect I could do a loop over them but with no array_expression for the arrays do I need to use the index?

Resources