I need to merge 2 objects using PHP (Laravel) one of them is in an array, the other is a plain object, but also contains a nested object, is there any simple way to do it?
Edit: The result should be a single object
[
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"state_id": 1,
"type_id": 7,
"created_at": "2016-11-18 11:39:19",
"updated_at": "2016-11-18 11:39:19",
"deleted_at": null
}
]
and
{
"id": 1,
"parent_id": null,
"name": "Chrysler",
"state_id": 1,
"type_id": 1,
"created_at": "2016-11-18 11:39:10",
"updated_at": "2016-11-18 11:39:10",
"deleted_at": null,
"pivot": {
"vehicle_id": 1,
"brand_id": 1
}
}
Desired output should be:
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"name": "Chrysler",
}
It has to be something similar, otherwise it would break a lot of JavaScript code connected to the first object.
<?php
//Merge the two as an array
$merged = array_merge($array,(array)$object);
//As an object
$merged = (object)$merged;
Related
on Laravel I have an answer for this from a query:
{
"id": 19,
"skin_id": 13,
"skin_payment_provider_id": 16,
"payment_method_id": 24,
"rank": 1,
"is_visible": 1,
"is_enabled": 1,
"created_at": "2020-07-16T10:09:51.000000Z",
"updated_at": "2020-07-16T10:09:51.000000Z",
"deleted_at": null,
"laravel_through_key": 13,
"payment_provider": {
"id": 5,
"parent_id": null,
"name": "Paypal",
"slug": "paypal",
"thumb": null,
"is_enabled": true,
"options": null,
"percentage": null,
"rank": 1,
"created_at": "2020-07-16T08:47:49.000000Z",
"updated_at": "2020-07-16T08:47:49.000000Z",
"deleted_at": null,
"laravel_through_key": 16
}
}
I can access the properties without problems such as:
$result->payment_method_id or $result->skin_id etc but I can't access the property $result->payment_provider!
if I call a return $result I see the answer I put above but if I call $result->payment_provider I have an empty answer.
I tried to call through Arr::get() but it is always the same...
What am I missing?
Thanks
Not sure how you convert the json to an array..
i guess your payment provider is converted as an array an not an object
$result->payment_provider['id']; or $result['payment_provider']['id']
Dumping it like dd($result)
or print_r($result)
Will help you understand whats in your var.
I have three related tables:
Table Coretable which is referenced by Table extensiontable_itc and extensiontable_sysops.
I now want to get all the records from coretable which are referenced by extensiontable_itc and extensiontable_sysops, and to do so, I run this code:
$join = coretable::with('extensiontable_itc','extensiontable_sysops')->get();
and this kind of works, I get the following result:
[{
"id": 1,
"Internal_key": "TESTKEY_1",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 1,
"coretable_id": 1,
"description": "EXTENSION_iTC_1",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 1,
"coretable_id": 1,
"description": "EXTENSION_SYSOPS_1"
}
}, {
"id": 2,
"Internal_key": "TESTKEY_2",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 4,
"coretable_id": 2,
"description": "EXTENSION_ITC_2",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 2,
"coretable_id": 2,
"description": "EXTENSION_SYSOPS_2"
}
}, {
"id": 3,
"Internal_key": "TESTKEY_3",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 5,
"coretable_id": 3,
"description": "EXTENSION_ITC_3",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 3,
"coretable_id": 3,
"description": "EXTENSION_SYSOPS_3"
}
}, {
"id": 4,
"Internal_key": "TESTKEY_4",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 6,
"coretable_id": 4,
"description": "EXTENSION_ITC_4",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 4,
"coretable_id": 4,
"description": "EXTENSION_SYSOPS_4"
}
}, {
"id": 5,
"Internal_key": "TESTKEY_5",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 7,
"coretable_id": 5,
"description": "EXTENSION_ITC_5",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 5,
"coretable_id": 5,
"description": "EXTENSION_SYSOPS_5"
}
}]
As you can see, the records referencing the respective record on coretable are put into an array, which itself becomes an element of the overall array containing the data from coretable.
Now, I want to get ALL this data EXCEPT for:
The ids from coretable
The created_at/updated_at
the foreign keys/coretable_ids
I already worked with the collection method "pluck()", but it doesn't seem to fit the purpose of accessing this multi-level array with all these duplicates in the indexes.
Is there any convenient way to do this? Or do I have to do it manually, using loops?
https://laravel.com/docs/5.8/eloquent-serialization#hiding-attributes-from-json
There are two ways to hide fields. The first one is to put needed fields to $hidden array inside model class, and all these fields will not be included in the model instance. The second one is to use makehidden function. This allows you to hide fields dynamically, depend on some conditions as an example.
i have an array full of objects , in each object there is properties i wanna collect a specific property in all the object that i have and assign them to a variable.
this is the array
[
{
"id": 23,
"user_id": 2,
"friend_id": 2,
"created_at": "2018-05-23 21:00:07",
"updated_at": "2018-05-23 21:00:07"
},
{
"id": 31,
"user_id": 2,
"friend_id": 1,
"created_at": "2018-05-23 21:00:07",
"updated_at": "2018-05-23 21:00:07"
},
{
"id": 32,
"user_id": 2,
"friend_id": 4,
"created_at": "2018-05-23 21:00:07",
"updated_at": "2018-05-23 21:00:07"
}
]
i wanna get the value of friend_id
whats the best practices to do so?
thanks.
That looks like a json string, so you would need to decode it first:
$friends = json_decode($json_string, true);
The you can extract the ids with array column as suggested in comments, or a foreach loop if you are using php 5.4 or below:
$friend_ids = array_column($friends, 'friend_id');
//OR
$friend_ids=array();
foreach($friends as $friend)
$friend_ids[] = $friend['friend_id'];
You can use array map. What this will do is assign all the friend id's into a new array. I'm passing an anonymous function that returns just the friend id out of the object. For more info on array_map: http://php.net/manual/en/function.array-map.php
<?php
$json = '[
{
"id": 23,
"user_id": 2,
"friend_id": 2,
"created_at": "2018-05-23 21:00:07",
"updated_at": "2018-05-23 21:00:07"
},
{
"id": 31,
"user_id": 2,
"friend_id": 1,
"created_at": "2018-05-23 21:00:07",
"updated_at": "2018-05-23 21:00:07"
},
{
"id": 32,
"user_id": 2,
"friend_id": 4,
"created_at": "2018-05-23 21:00:07",
"updated_at": "2018-05-23 21:00:07"
}
]';
$jsonObject = json_decode($json);
$newArray = array_map(function($a) {
return $a->friend_id;
}, $jsonObject);
print_r($newArray);
Hello i want to remove "news_list" from php json array.
{
"news_list": [
{
"id": 2,
"group_id": 1,
"news_title": "fbb",
"news_description": "gfhgfh",
"status": "Y",
"created_at": "2017-05-11 16:04:26",
"updated_at": "2017-05-11 16:04:26"
},
{
"id": 3,
"group_id": 1,
"news_title": "ewrdf",
"news_description": "dsfsdfdsfsdffffffffffffff",
"status": "Y",
"created_at": "2017-05-12 10:59:01",
"updated_at": "2017-05-12 10:59:01"
}
]
}
Desired Output :
[
{
"id": 2,
"group_id": 1,
"news_title": "fbb",
"news_description": "gfhgfh",
"status": "Y",
"created_at": "2017-05-11 16:04:26",
"updated_at": "2017-05-11 16:04:26"
},
{
"id": 3,
"group_id": 1,
"news_title": "ewrdf",
"news_description": "dsfsdfdsfsdffffffffffffff",
"status": "Y",
"created_at": "2017-05-12 10:59:01",
"updated_at": "2017-05-12 10:59:01"
}
]
is there ant in-built PHP function for same ?.if there i s any in built function then please convey me.
There is nothing like direct function to fetch any key data of json. You need to convert your json to array or object using json_decode and then can perform or fetch what you looking for.
$res = json_decode($data); // return object
print_r($res->news_list);
Or
$res = json_decode($data, true); // return array
print_r($res['news_list']);
Try this...
$oldDataArray = json_decode('{"news_list":[{"id":2,"group_id":1,"news_title":"fbb","news_description":"gfhgfh","status":"Y","created_at":"2017-05-11 16:04:26","updated_at":"2017-05-11 16:04:26"},{"id":3,"group_id":1,"news_title":"ewrdf","news_description":"dsfsdfdsfsdffffffffffffff","status":"Y","created_at":"2017-05-12 10:59:01","updated_at":"2017-05-12 10:59:01"}]}');
$newDataArray = $oldDataArray->news_list;
echo '<pre>';
print_r($newDataArray);
echo '</pre>';
I have a problem with my collection
$tasks = Bear::all();
$val = new Collection([]);
foreach($tasks as $tas){
$val ->push($tas->id);
}
$tasks1 = BearPic::whereIn('bear_id',$val)->get();
$val2 = new Collection([]);
foreach($tasks1 as $tas){
$val ->push($tas->bear_id);
}
my echo on val one is
[10,11,12,10,10,11,11,12,12]
And on tasks1 is
[{
"id": 1,
"bear_id": 10,
"picnic_id": 1,
"created_at": null,
"updated_at": null
}, {
"id": 2,
"bear_id": 10,
"picnic_id": 2,
"created_at": null,
"updated_at": null
}, {
"id": 3,
"bear_id": 11,
"picnic_id": 1,
"created_at": null,
"updated_at": null
}, {
"id": 4,
"bear_id": 11,
"picnic_id": 2,
"created_at": null,
"updated_at": null
}, {
"id": 5,
"bear_id": 12,
"picnic_id": 1,
"created_at": null,
"updated_at": null
}, {
"id": 6,
"bear_id": 12,
"picnic_id": 2,
"created_at": null,
"updated_at": null
}]
the second collection returns empty
what might be the problem? I am rather new to laravel this is a tutorial
The second collection val2 is empty simply because you are pushing the values to val i.e. the first collection instead of val2.
Also, if you insist on not using joins and working on Collections, pluck is indeed a quicker solution.
$tasks = Bear::all();
$val = $tasks->pluck('id');
$tasks1 = BearPic::whereIn('bear_id', $val)->get();
$val2 = $tasks2->pluck('bear_id');
If I understand correctly, you wanted to put all of id values of $tasks1 collection to $val2, am I correct? If so then you could simply do like this:
$tasks1 = BearPic::whereIn('bear_id',$val)->get();
$val2 = $tasks1->pluck('id');