Combine $request->request and $request->files arrays - php

Im posting a multipart with text and files and trying to pass data to form, but these data are separated, so I want to combine them.
$request->request->all()
$request->files->all()
$form = $this->createForm(ParkingType::class, new Parking());
$form->submit($INeedToPassTheCombinedArray);
if ($form->isValid()) {
return $form->getData();
}
Both arrays have the same structure.
For example:
$request->request->all()
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [{
"total": 4,
"capacity": 928,
"places": [{
"total": 123,
"name": "test",
"address": "test"
},
{
"total": 123,
"name": "test",
"address": "test"
}
]
}]
}
$request->files->all()
{
"parkings": [{
"generalInfo": "File.pdf",
"places": [{
"logo": "File1.png"
},
{
"logo": "File2.png"
}
]
}]
}
I want to combine then in one single array, getting this:
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [{
"total": 4,
"capacity": 928,
"generalInfo": "File.pdf",
"places": [{
"total": 123,
"name": "test",
"address": "test",
"logo": "File1.png"
},
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File2.png"
}
]
}]
}
I tried using array_merge, but the result is a single array which contain 2 arrays. It is not adding the data of one array in the respective position of the other array.
I want to know if there is some method for do this automatically and elegant.

Hope this will solve your problem
$data1 = json_decode($data,true);// first post array
$data2 = json_decode($file,true);//second file array
foreach($data1['parkings'] as $key=>&$val){ // Loop though one array
$val2 = $data2['parkings'][$key]; // Get the values from the other array
$val += $val2; // combine 'em
foreach($val['places'] as $k=>&$v){
$val3 = $val2['places'][$k]; // Get the values from the other array
$v += $val3; // combine 'em
}
}
echo json_encode($data1);
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [
{
"total": 4,
"capacity": 928,
"places": [
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File1.png"
},
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File2.png"
}
],
"generalInfo": "File.pdf"
}
]
}

Related

Skipping an object if one of the attributes is null in laravel

Task: you need to get a students object, with the conditions "group_id*" and "specialty_*id", where all these 3 tables are connected. It is also necessary to paginate only those objects of students for whom a group and specialty was found.
My problem: if the group or specialty data is not found, then the object is still added with the "group: null" attribute to pagination, thereby failing to display the requested number of elements on the page. How can this problem be solved?
The code:
$students = Student::with([
"group" =>
function($query) {
$query->where("id", \request("group_id"));
},
"group.speciality" =>
function($query) {
$query->where("id", \request("speciality_id"));
},
])->paginate(\request("page_size") ? : 10)->toArray();
return response()->json($students);
Preview: when I make a request with parameters
group_id = 2
speciality_id = 2
page_size = 2
The following object is returned:
{
"current_page": 1,
"data": [
{
"id": 1,
"receipt_date": "2010-11-02",
"user": {
"id": 1,
"login": "ykirillova#gmail.com",
"phone": "+7 (922) 472-9240",
"role": "user",
"passport": {
"series": 1762,
"number": 384282,
"date_of_issue": "1991-11-27",
"issued": "magni",
"division_code": 3,
"scan": "*photo link*",
"secondname": "Kilikova",
"firstname": "Olga",
"thirdname": "Anisimova",
"birthday": "1973-05-13",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
},
{
"id": 2,
"receipt_date": "1973-11-07",
"user": {
"id": 2,
"login": "marta.fedorov#dackov.net",
"phone": "+7 (922) 903-0339",
"role": "user",
"passport": {
"series": 8241,
"number": 419233,
"date_of_issue": "1980-06-05",
"issued": "quos",
"division_code": 33,
"scan": "*photo link*",
"secondname": "Efremov",
"firstname": "Boleslav",
"thirdname": "Kostin",
"birthday": "2009-04-03",
"sex": "W"
}
},
"group": null
}
],
"per_page": "2",
"total": 75
}
Whereas with group == null, only 2 objects with groups should be returned
{
"current_page": 1,
"data": [
{
"id": 1,
"receipt_date": "2010-11-02",
"user": {
"id": 1,
"login": "ykirillova#gmail.com",
"phone": "+7 (922) 472-9240",
"role": "user",
"passport": {
"series": 1762,
"number": 384282,
"date_of_issue": "1991-11-27",
"issued": "magni",
"division_code": 3,
"scan": "*photo link*",
"secondname": "Kilikova",
"firstname": "Olga",
"thirdname": "Anisimova",
"birthday": "1973-05-13",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
},
{
"id": 5,
"receipt_date": "2002-07-05",
"user": {
"id": 5,
"login": "tester#mail.ru",
"phone": "+7 (800) 555-3535",
"role": "user",
"passport": {
"series": 5521,
"number": 866521,
"date_of_issue": "1980-06-05",
"issued": "quos",
"division_code": 33,
"scan": "*photo link*",
"secondname": "Pavlov",
"firstname": "Denis",
"thirdname": "Artemev",
"birthday": "2009-04-03",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
}
],
"per_page": "2",
"total": 75
}
The data presented is irrelevant as it is randomly generated.
Removing data elements in a loop with group equal to null does not solve the problem, since pagination gets lost.

php array replace upto same depth

I'm building a binary tree where I have two arrays. First array has empty tree structure and second array has actual values. Below are the structure of both arrays.
First Array
"157": {
"id": "157",
"username": "username",
"children": [
{
"id": "-1",
"parent_id": "0",
"username": "NULL",
"position": "",
"children": [
{
"id": "-1",
"parent_id": "0",
"username": "NULL",
"position": "",
"children": [
{
"id": "-1",
"parent_id": "0",
"username": "NULL",
"position": "",
"children": []
}
]
}
]
}
]
}
}
Second Array
"157": {
"id": "157",
"position": 1,
"username": "test1",
"parent_id": null,
"children": [
{
"id": "158",
"position": 1,
"parent_id": "157",
"username": "test1",
"children": [
{
"id": "159",
"position": 1,
"parent_id": "158",
"username": "test2",
"children": [
{
"id": "160",
"position": 1,
"parent_id": "159",
"username": "test3",
"children": []
}
]
}
]
}
]
}
}
I'm using array_replace_recursive function to replace empty values with actual value. It is working fine when both the arrays have same depth. For example if first array has 3 depth and second array also has 3 depth then it is working fine, but if second array has more values then first array then it is breaking array tree structure. So now I want to replace values upto depth of first array, no matter how big is second array, just replace values upto the depth first array.
Is this really possible?
Thank you!

Rewrite json in loop

I have an array, which i wrote like a table for more reability.
[{
"id": "1",
"title": "boune",
"value": "3"
}, {
"id": "2",
"title": "check",
"value": "4"
}, {
"id": "3",
"title": "boune",
"value": "5"
}]
As the result of what i want to see is this json, where 'value' of identical 'titles' united inside [] brackets. I am very newbie in php. I undestand that i need for loop to sort query result, but can`t heck...Cant even put my mind to it...
[{
"id":"1",
"title": "boune",
"value": [3, 5]
}]
<?php
$json = '[{"id":"1","title":"boune","value":"3"},{"id":"2","title":"check","value":"4"},{"id":"3","title":"boune","value":"5"}]';
$json_data = json_decode($json,true);
function accumulateValuesByTitle($json_data){
$hash = [];
foreach($json_data as $each_record){
if(isset($hash[$each_record['title']])){
$hash[$each_record['title']]['value'][] = $each_record['value'];
if($hash[$each_record['title']]['id'] > $each_record['id']){
$hash[$each_record['title']]['id'] = $each_record['id'];
}
}else{
$hash[$each_record['title']] = $each_record;
$hash[$each_record['title']]['value'] = [$hash[$each_record['title']]['value']];
}
}
return array_values($hash);
}
$modified_data = accumulateValuesByTitle($json_data);
echo json_encode($modified_data);

Nested JSON Object with array in PHP

I want JSON object as follows in that personal, address and itm have sequence of json object.
{
"id": "1",
"state": "12",
"personal": [
{
"name": "abc",
"contact":"1111111"
"address": [
{
"line1": "abc",
"city": "abc",
"itm": [
{
"num": 1,
"itm_detatils": {
"itemname": "bag",
"rate": 1000,
"discount": 0,
}
}
],
"status": "Y"
}
]
}
]
}
But I am getting result as follows in that I want json array at address and itm_details.
{
"id": "1",
"state": "12",
"personal": [
{
"name": "abc",
"contact": "1111111",
"address": {
"line1": "abc",
"city": "abc",
"itm": {
"inum": "1",
"itm_detatils": {
"itemname": "bag",
"rate": 1000,
"discount": 0
}
},
"status": "Y"
}
}
]
}
My PHP Code is as follow:
In that I am creating simple array and after that array inside array but during encoding to json it's not showing sequence of json object.
$a=array();
$a["id"]="1";
$a["state"]="12";
$a["personal"]=array();
$a["personal"][]=array(
"name"=>"abc",
"contact"=>"1111111",
"address"=>array(
"line1"=>"abc",
"city"=>"abc",
"itm"=>array(
"inum"=>"1",
"itm_detatils"=>array(
"itemname"=>"bag",
"rate"=>1000,
"discount"=>0,
),
),
"status"=>"Y",
),
);
echo json_encode($a);
Thanks in advance.
Add one more array
//...
"address" => array(
array(
"line1"=>"abc",
"city"=>"abc",
// ...
),
)

PHP getting values from nested json

I have this json listed below. I was using json_decode to get some of the values. Such as getting the id value:
$decoded_array = json_decode($result, true);
foreach($decoded_array['issue'] as $issues ){
$value[] = $issues["id"];
This method is working for getting the id value, however, I want to get the emailAddress values for both Bob and John. I believe you can get a single value by doing this:
$value[] = $issues["fields"][people][0][emailAddress];
Is it possible to get both email addresses in an efficient manner?
Edited --------
How would you get data with an expanded dataset? Example:
{
"startAt": 0,
"issue": [
{
"id": "51526",
"fields": {
"people": [
{
"name": "bob",
"emailAddress": "bob#gmail.com",
"displayName": "Bob Smith",
},
{
"name": "john",
"emailAddress": "john#gmail.com",
"displayName": "John Smith",
}
],
"skill": {
"name": "artist",
"id": "1"
}
}
},
{
"id": "2005",
"fields": {
"people": [
{
"name": "jake",
"emailAddress": "jake#gmail.com",
"displayName": "Jake Smith",
},
{
"name": "frank",
"emailAddress": "frank#gmail.com",
"displayName": "Frank Smith",
}
],
"skill": {
"name": "writer",
"id": "2"
}
}
}
]
}
I only want to extract the email addresses from both "fields". Is there an easy way to loop through all the "fields" to get "emailAddress" data?
You need to delve deeper into the array.
foreach ($decoded_array['issue'][0]['fields']['people'] as $person) {
echo $person['emailAddress'];
}

Categories