i need to combine two jsons in a job task, according to the person's location. I have a Json with people from location type 1, and another from location type 2.
I need to combine in a new array for each time they have the same location, and they are repeated and separate according to type. I tried to do it with a foreach inside the other, but with no success. Here is an example of JSON.
Json 1:
[
{
"id": 1,
"name": "Jacob",
"place": "Brazil",
"type": 1
},
{
"id": 2,
"name": "Izac",
"place": "Brazil",
"type": 1
},
{
"id": 3,
"name": "Anran",
"place": "Brazil",
"type": 1
},
{
"id": 4,
"name": "Irbur",
"place": "Brazil",
"type": 1
},
{
"id": 5,
"name": "Lusos",
"place": "Brazil",
"type": 1
},
{
"id": 6,
"name": "Gamio",
"place": "Brazil",
"type": 1
},
{
"id": 7,
"name": "Nubeil",
"place": "Brazil",
"type": 1
},
{
"id": 8,
"name": "Usgon",
"place": "Brazil",
"type": 1
},
{
"id": 15,
"name": "Fikis",
"place": "England",
"type": 1
}
]
Json 2:
[
{
"id": 9,
"name": "Gipin",
"place": "Brazil",
"type": 0
},
{
"id": 10,
"name": "Paoir",
"place": "Brazil",
"type": 0
},
{
"id": 11,
"cia": "Mutue",
"place": "Brazil",
"type": 0
},
{
"id": 12,
"name": "Ziefel",
"place": "England",
"type": 0
},
{
"id": 13,
"name": "Liedo",
"place": "England",
"type": 0
},
{
"id": 14,
"name": "Vicis",
"place": "England",
"type": 0
}
]
the result might look something like this:
{
"groups": [ // groups (same place)
{
"tipe1": [ // groups type 1: same place
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
},
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
}
]
"tipe2": [ // groups type 2: same place
{
"id": 11,
"name": "Mutue",
"place": "Brazil"
},
]
},
{
"tipe1": [
{
"id": 15,
"name": "Fikis",
"place": "England"
}
]
"tipe2": [
{
"id": 14,
"name": "Vicis",
"place": "England"
},
]
}
],
}
The rule for grouping is, two or more items of type 1 can be grouped with 1 of type 2, in reverse as well. It can be thought of as outward flights, and back flights.
Given an array, in which each index there are certain key value pairs, how do I select certain key value pairs and reject the others.
$channels = Channel::get()->toArray();
This will yield the following Array:
"channels": [
{
"id": 1,
"name": "Info Release",
"slug": "info-release",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-02 01:23:50",
"updated_at": "2018-12-05 07:54:41"
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-05 05:34:50",
"updated_at": "2018-12-05 07:54:32"
},
{
"id": 12,
"name": "haha",
"slug": "haha",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-29 23:27:16",
"updated_at": "2018-12-29 23:27:16"
}
],
What is the best way to turn that array into this:
"channels": [
{
"id": 1,
"name": "Information Release",
"slug": "information-release",
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
},
{
"id": 12,
"name": "haha",
"slug": "haha",
}
],
So that if I write
$channels[0]->id
it will spit out 1
Can You try this
$viewFileds = ['id', 'name', 'slug'];
$channels = Channel::get()
->map(function ($each) use ($viewFileds) {
return Arr::only($each, $viewFileds);
});
It will return the new Collection by Only the field enabled in the $viewFileds
Hope it helps
I have a list of (the result of a query in DB) like this:
[
{
"id": "1",
"parent_id": null,
"title": "مدادنوکی",
"url": "/medadnoki"
},
{
"id": "2",
"parent_id": null,
"title": "جامعه",
"url": "/commiunity"
},
{
"id": "5",
"parent_id": "1",
"title": "درباره ی مدادنوکی",
"url": "/about"
},
{
"id": "6",
"parent_id": "1",
"title": "درباره ی مدادنوکی",
"url": "/about"
},
{
"id": "7",
"parent_id": "2",
"title": "همکاران",
"url": "/co-worker"
},
{
"id": "8",
"parent_id": "2",
"title": "اساتید",
"url": "/masters"
}
]
But I want to create an object like this:
[
{
"title": "مدادنوکی",
"url": "/medadnoki",
"subs" : [
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
}
]
},
{
"title": "جامعه",
"url": "/soc",
"subs" : [
{
"title": "همکاران",
"url": "/co-work"
},
{
"title": "اساتید",
"url": "/masters"
}
]
}
]
I have handled this process with two foreach in php and it means I process data twice and it is not efficient and will be slow.
Is there any Idea to doing this with just one foreach?
using one foreach means faster than two foreach twice and It will show the result in big data
Assuming you have the results in an order where the parents appear in the results before any children they may have, this should work:
$nested = [];
foreach($results as $r) {
if($r['parent_id'] === null) {
$nested[$r['id']] = [
'title' => $r['title'],
'url' => $r['url'],
'subs' => []
];
continue;
}
$nested[$r['parent_id']]['subs'][] = [
'title' => $r['title'],
'url' => $r['url']
];
}
$nested = array_values($nested);
I am using php wrapper, try create an item, all is ok, item is creating, but I can't change status, tried different ways, but can't find right way.
Need change status to "Closed" - http://prntscr.com/fsrwc3
Codes not works:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
or
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
$get_item = PodioItem::get_basic($item->item_id);
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
After create item (after new PodioItem...), if I get fields after this code (just get $item->fields or PodioItem::get_basic...), I can see correct status, only in the code, only immediatly after create item, but if I get this item in the another code (just PodioItem::get_basic...) I will see default value, so code don't change status, looks like I just see some cache.
If I create item on the one script:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
Then, in the another script update field, it will be change:
$get_item = PodioItem::get_basic('639637317');
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
Update - debug information:
Get log by test code -
PodioItem::create($app_id, ['fields' => ['status'=>[13], 'category'=>[3], 'contract-type'=>[4]]]);
(simple variant for creating an item, have the same problems like new PodioItem... )
All fields have category type, but:
status - have inline show type - don't chnages
category - have dropdown show type - Is changing
contract-type - have dropdown show type - Is changing
Log:
2017-07-08 11:07:22 200 POST /item/app/12152727/
2017-07-08 11:07:22 Request body: {"fields":{"status":[8],"category":[3],"contract-type":[4]}}
2017-07-08 11:07:22 Reponse: {
"presence": {
"ref_type": "item",
"ref_id": 641331142,
"user_id": 4194774,
"signature": "c165b85090e6ad28e74ae4baf93ee56113f88bc9"
},
"app": {
"status": "active",
"sharefile_vault_url": null,
"name": "Projects",
"default_view_id": null,
"url_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"icon_id": 378,
"link_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"app_id": 12152727,
"current_revision": 141,
"item_name": "Project",
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url_label": "projects",
"config": {
"item_name": "Project",
"icon_id": 378,
"type": "standard",
"name": "Projects",
"icon": "378.png"
},
"space_id": 3466816,
"icon": "378.png"
},
"created_on": "2017-07-10 15:31:16",
"last_event_on": "2017-07-10 15:31:16",
"linked_account_data": null,
"sharefile_vault_folder_id": null,
"app_item_id_formatted": "3988",
"recurrence": null,
"title": "ACSC",
"participants": {},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"priority": 641331142.0,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"subscribed_count": 1,
"reminder": null,
"ref": null,
"revision": 0,
"app_item_id": 3988,
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/3988",
"item_id": 641331142,
"sharefile_vault_url": null,
"rights": ["subscribe", "grant", "add_conversation", "rate", "update", "delete", "add_file", "grant_view", "view", "comment", "add_task"],
"fields": [{
"status": "active",
"type": "category",
"field_id": 93352415,
"label": "Division",
"values": [{
"value": {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Division",
"visible": true,
"delta": 5,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "ACSE",
"id": 1,
"color": "DCEBD8"
}, {
"status": "active",
"text": "ACSB",
"id": 2,
"color": "F7F0C5"
}, {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}],
"display": "dropdown"
}
},
"external_id": "category"
}, {
"status": "active",
"type": "category",
"field_id": 148215928,
"label": "Contract Type",
"values": [{
"value": {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Contract Type",
"visible": true,
"delta": 7,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "PO \/ Purchase \/ T&M",
"id": 1,
"color": "FFD5C2"
}, {
"status": "active",
"text": "Original Contract",
"id": 2,
"color": "D2E4EB"
}, {
"status": "active",
"text": "Service Rider",
"id": 3,
"color": "DCEBD8"
}, {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}],
"display": "dropdown"
}
},
"external_id": "contract-type"
}, {
"status": "active",
"type": "category",
"field_id": 93034840,
"label": "Instal Status",
"values": [{
"value": {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Instal Status",
"visible": true,
"delta": 13,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "deleted",
"text": "To Be Reviewed",
"id": 2,
"color": "F7F0C5"
}, {
"status": "deleted",
"text": "ACS Accounting Review",
"id": 5,
"color": "FFD5C2"
}, {
"status": "deleted",
"text": "Ignite Setup",
"id": 10,
"color": "DCEBD8"
}, {
"status": "active",
"text": "To Be Scheduled",
"id": 8,
"color": "D2E4EB"
}, {
"status": "active",
"text": "In Progress",
"id": 6,
"color": "DCEBD8"
}, {
"status": "active",
"text": "Warranty",
"id": 11,
"color": "F7F0C5"
}, {
"status": "active",
"text": "Complete",
"id": 3,
"color": "D1F3EC"
}, {
"status": "deleted",
"text": "Closed",
"id": 7,
"color": "DDDDDD"
}, {
"status": "active",
"text": "Cancelled",
"id": 9,
"color": "DDDDDD"
}, {
"status": "deleted",
"text": "VA in Progress",
"id": 4,
"color": "E1D8ED"
}, {
"status": "deleted",
"text": "Submitted",
"id": 1,
"color": "F7F0C5"
}, {
"status": "active",
"text": "On Hold",
"id": 12,
"color": "F7D1D0"
}, {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}],
"display": "inline"
}
},
"external_id": "status"
}],
"initial_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"current_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"linked_account_id": null,
"push": {
"timestamp": 1499700676,
"expires_in": 21600,
"channel": "\/item\/641331142",
"signature": "b8a816ff367da6bc730071c875ca3fdca2d2c344"
},
"external_id": null
}
Have you tried http://podio.github.io/podio-php/fields/#category-field ?
Setting values
Set a single value by using the option_id. You can also
add a value with add_value()
$item = PodioItem::get_basic(123);
$field_id = 'category';
// Set value to a single option
$item->fields[$field_id]->values = 4; // option_id=4
// Add value to existing selection
$item->fields[$field_id]->add_value(4); // option_id=4
Use an array to set multiple values
$item = PodioItem::get_basic(123);
$field_id = 'category';
$item->fields[$field_id]->values = array(4,5,6); // option_ids: 4, 5 and 6
Creating item with value:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>array(13)]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
I am trying to extract a segment from the json file of Rome2Rio API with PHP but I cant get an output.
The json file from rome2rio:
{
"serveTime": 1,
"places": [
{ "kind": "town", "name": "Kozani", "longName": "Kozani, Greece", "pos": "40.29892,21.7972", "countryCode": "GR", "regionCode": "ESYE13" },
{ "kind": "city", "name": "Thessaloniki", "longName": "Thessaloniki, Greece", "pos": "40.64032,22.93527", "countryCode": "GR", "regionCode": "ESYE12" }
],
"airports": [],
"airlines": [],
"aircrafts": [],
"agencies": [{
"code": "KTEL",
"name": "KTEL",
"url": "http://www.ktelbus.com/?module=default\u0026pages_id=15\u0026lang=en",
"iconPath": "/logos/Trains/KTELgr.png",
"iconSize": "27,23",
"iconOffset": "0,0"
}
],
"routes": [
{ "name": "Bus", "distance": 121.04, "duration": 120, "totalTransferDuration": 0, "indicativePrice": { "price": 9, "currency": "EUR", "isFreeTransfer": 0 },
"stops": [
{ "name": "Kozani", "pos": "40.30032,21.79763", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" },
{ "name": "Thessaloniki", "pos": "40.6545,22.90233", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" }
]
The PHP code I wrote is:
$json_rome2rio = file_get_contents("http://free.rome2rio.com/api/1.2/json/Search?key=&oName=kozani&dName=thessaloniki");
$parsed_json_r = json_decode($json_rome2rio);
echo $parsed_json_r->agencies->name;
The agencies property contains an array of agencies (note the square brackets). To access the name as you're after, you can do the following:
echo $parsed_json_r->agencies[0]->name;
This assumes that at least one agency is returned and that the agency you are after is the first one if more than one is returned.