get all the values of property in array objects - php

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);

Related

How can loop & get values of unlimited parent-child & sub-child tree data in laravel

I have data like parent-child with unlimited levels of sub-child & sub-child data in the database which you can see below like a binary tree with nodes that have multiple children and these children can have unlimited sub-child.
Please give me a solution or suggestion how can show that. Thanks.
This is my database JSON data which I'm getting from the database...
Controller Code
$products = Product::where('project_id', $project_id )->whereNull('parent_id')->with('categories')->with('childProducts')->get();
Getting data from the above object this data want to save in the other way you can see below
{
"data": {
"products": [{
"id": 1,
"name": "ParentProduct",
"project_id": 1,
"parent_id": null,
"created_by": 1,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 6,
"product_id": 1,
"project_id": 1,
"name": "TopLevelCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 2,
"name": "LevelOneProduct",
"project_id": null,
"parent_id": 1,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 1,
"product_id": 2,
"project_id": 1,
"name": "LevelOneProductCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 3,
"name": "LevelOneProductChildFolder",
"project_id": null,
"parent_id": 2,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 2,
"product_id": 3,
"project_id": 1,
"name": "evelOneProductChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 4,
"name": "LevelOneProductSubChildFolder",
"project_id": null,
"parent_id": 3,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 3,
"product_id": 4,
"project_id": 1,
"name": "LevelOneProductSubChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": []
}]
}]
},
{
"id": 5,
"name": "LeveTwoProduct",
"project_id": null,
"parent_id": 1,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [],
"child_products": [{
"id": 6,
"name": "LeveTwoProductFolder",
"project_id": null,
"parent_id": 5,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 4,
"product_id": 6,
"project_id": 1,
"name": "LeveTwoProductFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 7,
"name": "LeveTwoProductChildFolder",
"project_id": null,
"parent_id": 6,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 5,
"product_id": 7,
"project_id": 1,
"name": "LeveTwoProductChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": []
}]
}]
}
]
}]
}
}
I want to take this data to another JSON data which I have to give you an example as mentioned below I want to loop the above JSON data and want to make their need JSON with this data as like Dear Problem is that How can loop above data and can give to the other JSON keys you can see below. I want all data with loop and want to get all values and want equal all values to the keys as below
foreach ($products as $product) {
foreach ($product->categories as $singleCaegory) {
$singleCaegoryData = [
"name" => $singleCaegory->name,
"request" => [
'method' => $singleCaegory->type,
'body' => [
"mode" => "raw",
'raw' => $singleCaegory->raw_data
],
'url' => [
"raw" => $singleCaegory->url
],
],
];
}
}
I think you understand my problem. that I want to loop above data which I have already in the database and fetching from that. I only want to loop all data and equal to the needed keys. Thanks.
for saving Id's you only need:-parent Id, child_Id_count(count of the only direct child), total_child_count(count all children below).that's it by using these data and created date you can generate a tree view.

PHP Group Json Objects

"forms": [
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 21,
"field_caption": "Gön.Hekim"
}
},
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 22,
"field_caption": "Kurum"
}
},
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 23,
"field_caption": "Endikasyon"
}
},
{
"id": 2,
"name": "edit_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 2,
"field_name_id": 24,
"field_caption": "Materyal"
}
}
]
I want to group forms object in same name/id.
How i want...
"forms": [
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"fields": [{
"field_name_id": 21,
"field_caption": "Gön.Hekim"
},{
"field_name_id": 22,
"field_caption": "Kurum"
},{
"field_name_id": 23,
"field_caption": "Endikasyon"
}]
},
{
"id": 2,
"name": "edit_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"fields": [{
"field_name_id": 24,
"field_caption": "Materyal"
}]
}
]
i searched in so and googled, but i could not find any method for my problem. There are answers to similar questions.
i did with foreach loop but i thought may be there is a method for this in php.
And also, if i have over an hundred objects in json, foreach can be slow.
Thanks in advice.
Try the following:
$arr = json_decode($str, true);
$arr2 = [];
$ids = [];
foreach($arr['forms'] as $form)
{
if(!in_array($form['id'], $ids))
{
$ids[] = $form['id'];
$arr2[$form['id']] = $form;
}
unset($arr2[$form['id']]['pivot']);
$arr2[$form['id']]['fields'][] = [$form['pivot']['field_name_id'], $form['pivot']['field_caption']];
}
$str2 = json_encode(['forms' => array_values($arr2)]);
echo $str2;
This uses a foreach loop to get unique forms and will create a new key fields to hold the field_name_id and field_caption and also removes the pivot key

PHP Array Conversion with removing Array Name

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>';

Merge two objects, while one is inside of an array

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;

Trying to get property of non-object in Laravel even when it is present

the data i am testing on is like this in feed variable
{
"id": 32,
"user_id": 1,
"target_id": 3,
"feedable_id": 7,
"feedable_type": "review",
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"feedable": {
"id": 7,
"user_id": 1,
"slug_id": 3,
"review": "y this product is so quite?",
"stars": 9,
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"slug": {
"id": 3,
"value": "Quite",
"views": 5,
"user_id": 1,
"category_id": 1,
"created_at": "2016-05-23 14:18:03",
"updated_at": "2016-05-24 12:47:29"
}
}
}
but i am not be able to access the value:quite in my view
{{ $feed->feedable->slug->value }}
Laravel throws
Trying to get property of non-object (View: ...
This is a json variable so you need to decode it first then access the value. Using json_decode you can decode the variable and get the desire value.
Check online.
$json = '{
"id": 32,
"user_id": 1,
"target_id": 3,
"feedable_id": 7,
"feedable_type": "review",
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"feedable": {
"id": 7,
"user_id": 1,
"slug_id": 3,
"review": "y this product is so quite?",
"stars": 9,
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"slug": {
"id": 3,
"value": "Quite",
"views": 5,
"user_id": 1,
"category_id": 1,
"created_at": "2016-05-23 14:18:03",
"updated_at": "2016-05-24 12:47:29"
}
}
}';
$feed = json_decode ($json);
echo $feed->feedable->slug->value; //Quite

Categories