Put 2 objects inside the object - php

I'm writing some queries and receiving the $help_articles:
[
{
"id": 10,
"title": "Creative Agency Creation",
"sub_title": "Learn how to create creative agency and setup information and users",
"featured_image": image.jpg",
"content": "[{some content}]",
},
{
"id": 12,
"title": "Create New User",
"sub_title": "Instruction to create new user",
"featured_image": "56095569_step_3.jpg",
"content": "[{some_content}]",
}
]
Another query is returning $related_articles
[
{
"title": "Creative Agency Creation",
"slug": "creative-agency-creation"
},
{
"title": "Create New User",
"slug": "create-new-user"
},
]
I need 2 put this 2 objects in 1 like this:
{
"all_articles": [
{
"id": 10,
"title": "Creative Agency Creation",
"sub_title": "Learn how to create creative agency and setup information and users",
"featured_image": image.jpg",
"content": "[{some content}]",
},
{
"id": 12,
"title": "Create New User",
"sub_title": "Instruction to create new user",
"featured_image": "56095569_step_3.jpg",
"content": "[{some_content}]",
}
],
"related_articles": [
{
"title": "Creative Agency Creation",
"slug": "creative-agency-creation"
},
{
"title": "Create New User",
"slug": "create-new-user"
},
]
}
I'm making the following:
$final = new \stdClass();
$final=$help_articles;
$final['related_articles'] = $related_articles;
return response()->json($final);
But I didn't get the required result.
Please advice, what I'm doing wrong and how to fix this

#Cbroe, thanks a lot, it's helped.
The solution was simple:
$final = [
'all_articles' => $help_articles,
'related_articles' => $related_articles
];
return response()->json($final);

What about compact()? It was like
$all_articles = []; //some array
$related_articles = []: //some data
return response()->json(compact(['all_articles', 'related_articles']));

Try this it will work in your case.
$final = new stdClass();
$final->all_articles = $all_articles;
$final->related_articles = $related_articles;
return response()->json($final);

Related

Elastic Search Indexing in php

I have a codeigniter application where I try to set up elasticsearch, I have my json which looks like this:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "features",
"_type": "liste",
"_id": "test",
"_score": 1.0,
"_source": {
"mnuActiveSection": "securite.features",
"rowsFeatures": [
{
"id": "50",
"idCategory": "5",
"Name": "01- Data",
"FeatureCategory": "Serie",
"NombrePrivileges": "9",
"ListePrivileges": "Admin"
},
{
"id": "51",
"idCategory": "5",
"Name": "02- Data 2",
"FeatureCategory": "Documentary",
"NombrePrivileges": "3",
"ListePrivileges": "Direction"
},
{
"id": "52",
"idCategory": "5",
"Name": "03- Data 3",
"FeatureCategory": "Films",
"NombrePrivileges": "7",
"ListePrivileges": "Super Admin"
}
]
}
}
]
}
}
I would like to display the list of features in my view, and to search the documents via elastic search. unfortunately I can not display the data json.
This is not what I will like, because I have the impression that I have all my data grouped in a single object array.
To be done I try to use a "foreach" but unfortunately I do not get what I'm looking for. I am stuck at this stage.
I would like for each data of my table to have a different _id for example to better visualize them.
public function liste()
{
$data["mnuActiveSection"] = "securite.features";
$Finder = new FeaturesFinder();
$data["rowsFeatures"] = $Finder->FindAll();
$this->load->library('elasticsearch');
foreach($data["rowsFeatures"] as $d){
$this->elasticsearch->index = 'features';
$this->elasticsearch->create();
$this->elasticsearch->add($type ='liste',$id = 'test',$data);
var_dump($d);
}
}
If anyone could help me find a track I'm interested.
thank you very much
I think what you're trying to do is this:
$i = 0;
foreach($data["rowsFeatures"] as $d){
$i++;
$this->elasticsearch->index = 'features';
$this->elasticsearch->create();
$this->elasticsearch->add($type ='liste',$id = $i,$data);
var_dump($d);
}

Parsing a JSON file with multiple levels in a MySQL database

I've some trouble with parsing a JSON file into a MySQL database. It's an export of some Facebookstats.
Because I've multiple export of multiple pages, it's important that I've the corresponding ID in the database.
The JSONfile (or cURL from Facebook) looks like this:
{
"data": [
{
"name": "impressions",
"period": "week",
"values": [
{
"value": 123456789,
"end_time": "2016-01-01T08:00:00+0000"
},
{
"value": 12345678,
"end_time": "2016-01-02T08:00:00+0000"
},
{
"value": 1234567,
"end_time": "2016-01-03T08:00:00+0000"
},
{
"value": 123456,
"end_time": "2016-01-04T08:00:00+0000"
},
{
"value": 12345,
"end_time": "2016-01-05T08:00:00+0000"
}
],
"title": "Weekly Impressions",
"description": "The number of impressions seen of any content associated with your Page. (Total Count)",
"id": "101010101010\/insights\/page_impressions\/week"
}
],
"paging": {
"previous": "1",
"next": "2"
}
}
I would, ideally, parse this data into a MySQL database that looks like this:
id value end_time
101010101010 123456789 2016-01-01T08:00:00+0000
101010101010 12345678 2016-01-02T08:00:00+0000
101010101010 1234567 2016-01-03T08:00:00+0000
101010101010 123456 2016-01-04T08:00:00+0000
101010101010 12345 2016-01-05T08:00:00+0000
I hope someone had some ideas :-)
Use json_decode(). Example:
$jsonString = '{
"data": [
{
"name": "impressions",
"period": "week",
"values": [
{
"value": 123456789,
"end_time": "2016-01-01T08:00:00+0000"
},
{
"value": 12345678,
"end_time": "2016-01-02T08:00:00+0000"
},
{
"value": 1234567,
"end_time": "2016-01-03T08:00:00+0000"
},
{
"value": 123456,
"end_time": "2016-01-04T08:00:00+0000"
},
{
"value": 12345,
"end_time": "2016-01-05T08:00:00+0000"
}
],
"title": "Weekly Impressions",
"description": "The number of impressions seen of any content associated with your Page. (Total Count)",
"id": "101010101010\/insights\/page_impressions\/week"
}
],
"paging": {
"previous": "1",
"next": "2"
}
}';
Then decode it to an associative array:
$assocData = json_decode($jsonString, true); //Setting second optional parameter to true makes it return an associative array.
Then access it however you want:
$data = $assocData['data'];

Elasticsearch Updating / Deleting Nested

I've gone through a few examples and documentations and kind find a solution update a nested object in the this result set.
I can add one (if one does not exist)
I can append to it (if one does exist)
Can't figure out how to delete a selected entry.
Is there a method I can use (using the php client) to add an entry if it does not exist / update an entry if it does exist / delete the second entry.
I'm inheriting this problem and am new to Elastic search.
Thanks.
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "products",
"_type": "categories",
"_id": "AUpRjtKZfXI7LIe9OpNx",
"_score": 1,
"_source": {
"name": "Primary",
"description": "Primary Category",
"slug": "Primary",
"created": "2014-12-16 00:25:22",
"parent": [
{
"name": "First One",
"description": "Test",
"id": "ae74ea4e2e865ed3fd60c18a06e69c65",
"slug": "first-one"
},
{
"name": "Second One",
"description": "Testing Again",
"id": "c8dbe5143c8dfd6957fa33e6cea7a0a8",
"slug": "second-one"
}
]
}
}
]
}
}
Do you want to do all three in the same operation?
Deleting the second nested object is achieved through a script which removes the second element:
PUT /products
{
"mappings": {
"categories": {
"properties": {
"parent": {
"type": "nested",
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"id": { "type": "string", "index": "not_analyzed" },
"slug": { "type": "string" }
}
}
}
}
}
}
PUT /products/categories/1
{
"name": "Primary",
"description": "Primary Category",
"slug": "Primary",
"created": "2014-12-16 00:25:22",
"parent": [
{
"name": "First One",
"description": "Test",
"id": "ae74ea4e2e865ed3fd60c18a06e69c65",
"slug": "first-one"
},
{
"name": "Second One",
"description": "Testing Again",
"id": "c8dbe5143c8dfd6957fa33e6cea7a0a8",
"slug": "second-one"
}
]
}
POST /products/categories/1/_update
{
"script" : "ctx._source.parent.remove(1)",
"lang": "groovy"
}
GET /products/categories/1
So in PHP code (using the official PHP client), the update would look like:
$params = [
'index' => 'products',
'type' => 'categories',
'id' => 1,
'body' => [
'script' => 'ctx._source.parent.remove(1)',
'lang' => 'groovy'
]
];
$result = $client->update($params);

PHP JSON add array

i need some assistance with properly generating JSON. I almost have it, but I need to have each category and its related item in its own array. I have multiple categories each with many items under them. My JSON looks good, i just need the categories as an array along with its items. Any help is greatly appreciated!
Here is my code:
$channel ['items']['post']['categories'][$category_name]['details'][] = array(
'title' => $title,
'price' => $price,
'date_time' => $date_time,
'description' => $description,
);
}
$channels = array($channel);
$json = json_encode($channel);
header('Content-type: application/json');
echo $json;
My JSON looks like this currently:
{
"items": { /// THIS IS MEANT TO ORGANIZE UNDER ITEMS
"post": { ///THIS IS PROBABLY UNNECESSARY, I JUST HAVEN'T REMOVED IT.
"categories": {
"Baby": { ///I HAVE MANY MANY CATEGORIES
"details": [ ///I HAVE MANY MANY ITEMS UNDER EACH CATEGORY
{
"title": "trying with category id again",
"price": "3344.55",
"date_time": "2013-11-11 17:33:49",
"description": "Descriptor sb ",
"category_id": "3",
},
]
But I want it to look like this:
{
"items": {
"categories": [{ /// NEED THE BRACKET HERE
"Baby": {
"details": [
{
"title": "trying with category id again",
"price": "3344.55",
"date_time": "2013-11-11 17:33:49",
"description": "Descriptor sb ",
},
{
"title": "what the",
"price": "44444.66",
"date_time": "2013-11-18 20:15:58",
"description": "Blah blah",
},
]
},
"Baby Stuff": {
"details": [
{
"title": "putting in title",
"price": "3000.99",
"date_time": "2013-11-11 17:42:15",
"description": "Blah blah blah",
},
{
"title": "adding another listing",
"price": "400000.99",
"date_time": "2013-11-17 22:37:02",
"description": "Blah blah blah",
},
]
},
"More Baby Stuff": {
"details": [
{
"title": "does this work",
"price": "4000.77",
"date_time": "2013-11-18 19:59:49",
"description": "Description ",
},
{
Add another [] after ['categories'] and that should do the trick
$channel ['items']['post']['categories'][][$category_name]['details'][] = array( // ...
$channel ['items']['post']['categories'][]['Baby']['details'][] = array(
'title' => 'trying with category id again',
'price' => 3344.55,
'date_time' => '2013-11-11 17:33:49',
'description' => 'Descriptor sb',
);
$channels = array($channel);
$json = json_encode($channel);
echo $json;
Output
{
"items": {
"post": {
"categories": [
{
"Baby": {
"details": [
{
"title": "trying with category id again",
"price": 3344.55,
"date_time": "2013-11-11 17:33:49",
"description": "Descriptor sb"
}
]
}
}
]
}
}
}
See it working here on ideone
Based on comments and edits to OP, perhaps you should be looking at a data structure like this:
{
"categories":
[
{
"category_name": "Baby",
... [Other category metadata],
"category_items":
[
{
"item_name": "Item Name",
... [Other item information]
},
{ [next item] },
...
]
},
{ [ next category ] },
...
]
}
This means one might build this object in similar manner to the following (assuming for example you are building this from DB query):
$channel = new stdClass();
$channel->categories = array();
LOOP for categories as $category
$category = new stdClass();
$category->category_name = 'some value';
$category->some_other_category_metadata = 'some other value';
$category->category_items = array();
INNER LOOP on items in category as $item
$item = new stdClass();
$item->item_name = 'item name';
$item->some_other_item_data = 'data';
$category->category_items[] = $item;
END INNER LOOP
$channel->categories[] = $category;
END OUTER LOOP
json_encode($channel);

Posting to Facebook results randomly in Status messages or Posts

This app is posting to the user's timeline, using PHP and Facebook's PHP API implementation. This is working right now, for over several weeks already.
The following PHP code is being used to post a message:
if($hasPhoto === TRUE)
{
// Post to FB with picture
$facebook->setFileUploadSupport(true);
$result = $facebook->api("/me/photos", "post", array(
'message' => $message,
'place' => $place,
'source' => '#' . $photo
));
}
else
{
// Post to FB without picture
$result = $facebook->api("/me/feed", "post", array(
'message' => $message,
'place' => $place
));
}
This is working properly, except there are two formattings of the $result:
array ('id' => '103240856515XXX', )
array ('id' => '100004900175XXX_103239809849XXX', )
The Graph API documentation tells us the following:
A post from Facebook Platform: https://graph.facebook.com/19292868552_10150189643478553
A status message on the Facebook Page: https://graph.facebook.com/10150224661566729
This means the used PHP code generates posts ór status messages. I don´t see any relation between the textmessages, photos, dates, or authors. It seems to happen randomly.
The following shows the relationship from our data between the format of the id and whether a photo is attached.
select count(*) from fbposts where facebookpostid like '%\_%' and hasphoto = 1; -- 90
select count(*) from fbposts where facebookpostid like '%\_%' and hasphoto = 0; -- 87
select count(*) from fbposts where facebookpostid not like '%\_%' and hasphoto = 1; -- 47
select count(*) from fbposts where facebookpostid not like '%\_%' and hasphoto = 0; -- 54
Why does this behaviour happen? How to force a Post? The reason why this is relevant is because Post does have a Privacy property which I would like to query.
Update:
Querying a status message 545778052106XXX, given by the FB API, with a photo, gives me:
{
"id": "545778052106XXX",
"from": {
"name": "Jeffrey Krist",
"id": "100000226354XXX"
},
"name": "My message!",
"picture": "http://photos-f.ak.fbcdn.net/hphotos-ak-ash3/522827_545778052106XXX_1151562XXX_s.jpg",
"source": "http://sphotos-f.ak.fbcdn.net/hphotos-ak-ash3/s720x720/522827_545778052106XXX_1151562XXX_n.jpg",
"height": 720,
"width": 720,
"images": [
{
"height": 2048,
"width": 2048,
"source": "http://sphotos-f.ak.fbcdn.net/hphotos-ak-ash3/s2048x2048/522827_545778052106XXX_1151562XXX_n.jpg"
}, .. lots more
],
"link": "https://www.facebook.com/photo.php?fbid=545778052106402&set=p.545778052106XXX&type=1",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPXXX.gif",
"place": {
"id": "182665821805XXX",
"name": "A company name",
"location": {
"street": "My street 13", ..
}
},
"created_time": "2012-11-01T08:35:20+0000",
"updated_time": "2012-11-01T08:35:20+0000",
"comments": ...
"likes": ...
}
Querying a post message using a id from the FB API gives me:
{
"id": "100003331805XXX_299609210160XXX",
"from": {
"name": "Some name",
"id": "100003331805XXX"
},
"message": "My message",
"picture": "http://photos-e.ak.fbcdn.net/hphotos-ak-ash3/560724_299609200160XXX_789651XXX_s.jpg",
"link": "https://www.facebook.com/photo.php?fbid=299609200160XXX&set=a.285494101571XXX.69331.100003331805XXX&type=1&relevant_count=1",
"name": "Photo album name",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPXXX.gif",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/100003331805XXX/posts/299609210160XXX"
},
{
"name": "Like",
"link": "https://www.facebook.com/100003331805XXX/posts/299609210160XXX"
}
],
"privacy": {
"value": "ALL_FRIENDS", ...
},
"place": {
"id": "174171872642XXX", ...
},
"type": "photo",
"status_type": "added_photos",
"object_id": "299609200160XXX",
"application": {
"name": "My app", ...
},
"created_time": "2012-12-21T22:33:59+0000",
"updated_time": "2012-12-21T23:30:39+0000",
"likes": ...
"comments": ...
}
Querying a composed id, _ ('100000226354XXX_545778052106XXX'), which is message with a photo, gives me:
{
"error": {
"message": "Unsupported get request.",
"type": "GraphMethodException",
"code": 100
}
}
There is no need to force a post when each status message is a post object.
Status message object 10100948019328597
Post object userid_10100948019328597
Status message object
{
"id": "10100948019328597",
"from": {
"name": "phwd",
"id": "13608786"
},
"message": "Happy Thanksgiving you cool Canadians!",
"updated_time": "2012-10-08T23:17:27+0000",
"likes": {
"data": [
],
"paging": {
"next":
}
}
}
Post object
{
"id": "13608786_10100948019328597",
"from": {
"name": "phwd",
"id": "13608786"
},
"message": "Happy Thanksgiving you cool Canadians!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/13608786/posts/10100948019328597"
},
{
"name": "Like",
"link": "http://www.facebook.com/13608786/posts/10100948019328597"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2012-10-08T23:17:28+0000",
"updated_time": "2012-10-08T23:17:28+0000",
"likes": {
"data": [
]
},
"comments": {
"count": 0
}
}
The best way to check would be to compare /me/statuses vs /me/posts

Categories