PHP Array Conversion with removing Array Name - php

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

Related

api json response not coming in array form

First of all I am new to laravel.
This is my controller
$itemsList=Items::all()->where('shop_id',$request->shop_id);
return response()->json(['data'=>$itemsList]);
This is the response
{
"data": {
"10": {
"id": 11,
"shop_id": 1,
"title": "Test",
"price_nd": 12,
"price_wd": 10,
"updated_at": "2019-11-14 00:00:00",
"created_at": "2019-11-14 00:00:00"
},
"11": {
"id": 12,
"shop_id": 1,
"title": "Test",
"price_nd": 12,
"price_wd": 10,
"updated_at": "2019-11-14 00:00:00",
"created_at": "2019-11-14 00:00:00"
},
"14": {
"id": 15,
"shop_id": 1,
"title": "Test",
"price_nd": 12,
"price_wd": 10,
"updated_at": "2019-11-14 00:00:00",
"created_at": "2019-11-14 00:00:00"
}
}
}
The problem is I want to return an array list of the items
I tried many approached but I don't know what is the problem or what I am missing
Try. Use get() method .
get() and all() both get same output but
all() method not useful for use where condition.
get() method can useful to use other conditions
$itemsList=Items::where('shop_id',$request->shop_id)->get();
return response()->json(['data'=>$itemsList]);
Try this code..
$itemsList=Items::where('shop_id',$request->shop_id)->get();
dd($itemsList); // add this step in your code
return response(['data' => $itemsList]);
You this approach.
return response(['data' => $itemsList]);

Delete unwanted data from api result

I have a project in laravel which has API. I'm asking the API for posts(I call them recommendation).
Now my api response look like this -
{
"success": true,
"data": {
"current_page": 1,
"data": [
{
"id": 3,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
}
...
...
...
All is good but I don't wanna likes_count to be in the result. It is a relation method. I get from it all I need. It is if the user liked this post. And it is is_liked in the result . but likes_count automatically added to the response .
if($item->likesCount->contains($user->id)){
$item['is_liked']='true';
}
I tried delete it with
foreach ($recommendations as $item) {
unset($item['likes_count']);
}
But it doesn't do it.
I think the problem is how you are referencing the object data structure. You are attempting to unset() something that doesn't exist so no error is thrown but the likes_count isn't getting removed either.
Here's your code example (fixed and modified for demonstration):
<?php
$apiResult = <<<eod
{
"success": true,
"data": {
"current_page": 1,
"data": [
{
"id": 3,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres",
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
},
{
"id": 4,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres",
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
}
]
}
}
eod;
$result = json_decode($apiResult);
$data = $result->data->data;
var_dump($data);
// This should remove the likes_count array from the $result structure.
foreach ($data as &$item) {
unset($item->likes_count);
}
var_dump($data);
A quick way would be to make that 'attribute' (relation) hidden:
$recommendations->makeHidden('likes_count');
Though I am not sure how you are building your response.

get all the values of property in array objects

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

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