How to obtain the following structure after the php json_encode.
It is possible?
{
"data": [
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800"
},
{
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750"
}
]
}
How must look arrays?
Although formally array keys can only be integer you could simple use:
array( 'data' =>
array(
array( 'name' => 'tiger nixon', 'position' => 'system architect', 'salary' => '$320,800' ),
array( 'name' => 'Garrett Winters', 'position' => 'Accountant', 'salary' => '$170,750' )
)
);
Related
I am having some issues with uploading attachment into deals in HubSpot.
Here is my code:
$note_details = array(
"engagement" => array(
"active" => true,
"type" => "NOTE",
"timestamp" => 5456563646
),
"associations" => array(
"dealIds" => array( '4875586081' ),
"attachments" => array(
"id" => array ("44830500634")
),
),
"metadata" => array(
"body" => "TESTING-222",
)
);
This code creates an engagement or Notes but the attachment is not there!
and here is the JSON structure that HubSpot uses:
{
"engagement": {
"active": true,
"ownerId": 1,
"type": "NOTE",
"timestamp": 1409172644778
},
"associations": {
"contactIds": [2],
"companyIds": [ ],
"dealIds": [ ],
"ownerIds": [ ],
"ticketIds":[ ]
},
"attachments": [
{
"id": 4241968539
}
],
"metadata": {
"body": "note body"
}
}
Any idea what am I missing?
So I'm trying to sort my search results to show the exact matches before all the partial matches. What I mean by this is if I have the documents with names:
Set 4/102
Set 44/102
Set 94/102
I'm searching on the term 4/102 and it returns all documents. This is fine, however, I want the Set 4/102 to show up first but it seemingly sorts them randomly. Is there a way to use script sorting or something like that to have the exact term match to show up first?
These are my mappings and settings:
$settingsParams = [
'index' => 'products',
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [
'substring_analyzer' => [
'tokenizer' => 'substring_tokenizer',
'filter' => [
'lowercase'
]
],
'fullword_analyzer' => [
'tokenizer' => 'whitespace',
'filter' => [
'lowercase'
]
],
],
'tokenizer' => [
'substring_tokenizer' => [
'type' => 'nGram',
'min_gram' => 3,
'max_gram' => 12,
'token_chars' => [
'letter',
'digit',
'symbol',
'custom'
],
'custom_token_chars' => '/'
]
]
],
'max_ngram_diff' => 20
]
]
];
$mappingParams = [
'index' => 'products',
'body' => [
'_source' => [
'enabled' => true
],
'properties' => [
'name' => [
'type' => 'text',
'fields' => [
'keyword' => [
'type' => 'keyword'
]
],
'analyzer' => 'substring_analyzer',
'search_analyzer' => 'fullword_analyzer'
],
'min_price' => [
'type' => 'double'
],
'saleprice' => [
'type' => 'double'
],
'list_price' => [
'type' => 'double'
],
'root_category_rank' => [
'type' => 'integer'
],
'interest_level' => [
'type' => 'integer'
],
'root_categoryid' => [
'type' => 'integer'
]
]
]
];
Adding a working example
Index Mapping:
{
"settings": {
"analysis": {
"analyzer": {
"substring_analyzer": {
"tokenizer": "substring_tokenizer"
},
"fullword_analyzer": {
"tokenizer": "whitespace"
}
},
"tokenizer": {
"substring_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 12,
"token_chars": [
"letter",
"digit",
"custom",
"symbol"
],
"custom_token_chars": "/"
}
}
},
"max_ngram_diff": 50
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "substring_analyzer",
"search_analyzer": "fullword_analyzer"
}
}
}
}
Search Query:
{
"query": {
"match": {
"name": "4/102"
}
}
}
Search Result:
The document "name": "4/102" is having a higher score as compared to other documents
"hits": [
{
"_index": "66232066",
"_type": "_doc",
"_id": "1",
"_score": 0.15275992,
"_source": {
"name": "4/102" // note this
}
},
{
"_index": "66232066",
"_type": "_doc",
"_id": "2",
"_score": 0.12562492,
"_source": {
"name": "44/102"
}
},
{
"_index": "66232066",
"_type": "_doc",
"_id": "3",
"_score": 0.12562492,
"_source": {
"name": "94/102"
}
}
]
This is how my mapping looks
$arr = [
'index' => 'test1',
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [
'name_analyzer' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'lowercase',
'asciifolding',
'word_delimiter'
]
]
]
]
],
"mappings" => [
"info" => [
"properties" => [
"Name" => [// this field is analyzed
"type" => "string",
"fields" => [
"raw" => [ //subfield of Name is not analyzed so that we can avoid a known issue of space saperated bucket generation
"type" => "string",
"index" => "not_analyzed"
]
]
],
"Address" => [
"type" => "string",
"index" => "analyzed",
"analyzer" => "name_analyzer"
]
]
]
]
]
];
And this is my query
$query['index'] = 'test1';
$query['type'] = 'info';
//without bool & should also it will work
$query['body'] = [
'query'=> [
'bool' => [
'should' => [
'query_string' => [
'fields' => ['Name'],
'query' => 'sa*',
'analyze_wildcard' => 'true'
]
]
]
],
'size'=> '0',
'aggregations' => [
'actor' => [
'terms' => [
'field' => 'Name.raw',
'size' => 10
]
]
]
];
My output is
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"actor": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Salma Hayak",
"doc_count": 1
},
{
"key": "Salman Khan",
"doc_count": 1
},
{
"key": "Salman Shaikh",
"doc_count": 1
}
]
}
}
}
What I want is since Salman Khan is the most searched actor as compare to Salma Hayak, having said that when user searched for "sa" they should see salman khan first rather than salma hayak.
Can anyone please help me on this?
this is my multidimensional array..i know how to encode array to json but not getting actual json expected json result
array (
1 =>
array (
'text' => 'Dashboard',
'spriteCssClass' => 'rootfolder',
'expanded' => 'true',
'id' => '1',
'item_name' => 'Dashboard',
'menu_type' => 'item',
'parent' => '0',
'items' =>
array (
9 =>
array (
'text' => 'Application',
'spriteCssClass' => 'html',
'id' => '9',
'item_name' => 'Application',
'menu_type' => 'header',
'parent' => '1',
'items' =>
array (
),
),
),
),
)
after encoding it into json i am getting the following result
for encodin i used json_encode($array);
{
"1": {
"text": "Dashboard",
"spriteCssClass": "rootfolder",
"expanded": "true",
"id": "1",
"item_name": "Dashboard",
"menu_type": "item",
"parent": "0",
"items": {
"9": {
"text": "Application",
"spriteCssClass": "html",
"id": "9",
"item_name": "Application",
"menu_type": "header",
"parent": "1",
"items": {}
}
}}}
i want the following encoded json
{
"text": "Dashboard",
"spriteCssClass": "rootfolder",
"expanded": "true",
"id": "1",
"item_name": "Dashboard",
"menu_type": "item",
"parent": "0",
"items": [
{
"text": "Application",
"spriteCssClass": "html",
"id": "9",
"item_name": "Application",
"menu_type": "header",
"parent": "1",
"items": {}
}]
}
i tried almost everything but not getting my expected json result
i want remove the array indexing from json like "1" { and also want to add "[" this after every items: column
It looks like you just want to json_encode($yourData[1]) instead of just json_encode($yourData)...
Your array is not 0 indexed, therefore json_encode assumes its an assoc array.
If you 0 index your array, you should get the expected result, or maybe even remove the index assignment completelely:
array (
array (
'text' => 'Dashboard',
'spriteCssClass' => 'rootfolder',
'expanded' => 'true',
'id' => '1',
'item_name' => 'Dashboard',
'menu_type' => 'item',
'parent' => '0',
'items' =>
array (
9 =>
array (
'text' => 'Application',
'spriteCssClass' => 'html',
'id' => '9',
'item_name' => 'Application',
'menu_type' => 'header',
'parent' => '1',
'items' =>
array (
),
),
),
),
)
EDIT***
to remove all numerical indexes / convert all "non-assoc" to normal use:
function normaliseArray($arr,$recurse=True) {
if (!is_array($arr))
return $arr;
if (count(array_filter(array_keys($arr), 'is_numeric')) == count($arr))
$arr = array_values($arr);
if ($recurse) {
foreach($arr as $k => $a) {
$arr[$k] = normaliseArray($a,$recurse);
}
}
return $arr;
}
json_encode(normaliseArray($array));
try that.
json_encode will encode it as is. The best you can do is force the array to start at 0 which would be the same as []:
$array = array_values($array);
$array[0]['items'] = array_values($array[0]['items']);
I am new to mongoDb and facing issue in using aggregate function. I am trying to get sum of the fields "expectations" and "overall" but it returns 0. I also want to take the total count of the comments which are not empty or null in the same query.
$out = $collection->aggregate
(
array(
array( '$match' => array( 'id' => 6200 )),
array ('$unwind' => '$reviews'),
array( '$group' => array( '_id' => '$id',
'exptotal' => array( '$sum' => array('reviews' => '$expectations') ),
'total' => array( '$sum' => array('reviews' => '$overall' ) ),
'count' => array( '$sum' => 1 )
)
)
)
);
Here is the json
{
"_id": "528c62406a542f7c6a6bf522",
"id": 6200,
"categories": [
{
"id": 6,
"name": "Artificial Intelligence"
},
{
"id": 5,
"name": "Statistics and Data Analysis"
}
],
"courseId": "COURSE_16",
"institute": {
"id": 5693,
"name": "YZ University"
},
"instructors": [
" A Morris"
],
"language": "en",
"reviews": [
{
"username": "kalis",
"expectations": 3,
"content": 2,
"overall": 3,
"comments": "This is really good course for improvement",
"datecreated": "2013-11-02T17:04:11.102Z"
},
{
"username": "julia",
"expectations": 4,
"content": 2,
"overall": 2,
"comments": "This improves my skill a lot",
"datecreated": "2013-11-03T17:04:11.102Z"
},
{
"username": "john",
"expectations": 2,
"content": 4,
"overall": 4,
"comments": "",
"datecreated": "2013-11-04T17:04:11.102Z"
}
],
"shortName": "ml",
"title": "Machine Learning"
}
This looks like it would work:
$out = $collection->aggregate(array(
array('$match' => array( 'id' => 6200 )),
array ('$unwind' => '$reviews'),
array('$unwind' => '$comments'),
array( '$group' => array( '_id' => '$id',
'commTotal' => array('$sum' => array('$cond'=>array(array('$eq'=>array('$comments',null),0,1)))),
'exptotal' => array( '$sum' => '$reviews.expectations'),
'total' => array( '$sum' => '$reviews.overall' ),
'count' => array( '$sum' => 1 )
))
));
The reason is that when you $unwind the data is still in its subdocument field it is just that the subdocument has become an object of a single review.
The documentation is a little misleading on this operator I'll give you that.