Nested json string with variables in php - php

I will not lie to you guys: i hate nested Arrays.
So instead of my [working] code below
$query_array = Array(
"fields" => Array ("timestamp", "user.raw", "mailbox", "verb", "backend"),
"size" => 1,
"sort" => Array (Array ("#timestamp" => Array ("order" => $varOrder))),
"query" => Array (
"bool" => Array (
"must" => Array (
Array ("match" => Array ("verb" => "open")),
Array ("term" => Array ($varField => $varValue))
)
)
)
);
I want to use the [non working] code below
$query_json_string = '{
"fields" : [ "timestamp", "user.raw", "mailbox", "verb", "backend" ],
"size" : 1,
"sort" : [ { "#timestamp" : { "order" : $varOrder } } ],
"query" : {
"bool": {
"must": [
{ "match" : { "verb" : "open" } },
{ "term" : { $varField : $varValue } }
]
}
}
}';
So much easier to maintain...
But inside the single quotes the variables varOrder, varField and varValue are never expanded (i believe).
How can i use variables inside this nice and clean json string?

As I understand your question, you're simply not happy with php's array syntax. If that's the case, then don't use the old syntax. Instead use simple square brackets, which are available since version 5.4.
Doing so, your final code could be:
$query = [
"fields" => ["timestamp", "user.raw", "mailbox", "verb", "backend"],
"size" => 1,
"sort" => [["#timestamp" => ["order" => $varOrder]]],
"query" => [
"bool" => [
"must" => [
["match" => ["verb" => "open"]],
["term" => [$varField => $varValue]]
]
]
]
];
Which issn't far off your desired syntax. The only difference being => instead of :. But with the added benefit of not needing to declare objects with {}.

JSON is basically Javascript's version of a associative array.
$query_array = Array(
"fields" => Array ("timestamp", "user.raw", "mailbox", "verb", "backend"),
"size" => 1,
"sort" => Array (Array ("#timestamp" => Array ("order" => $varOrder))),
"query" => Array (
"bool" => Array (
"must" => Array (
Array ("match" => Array ("verb" => "open")),
Array ("term" => Array ($varField => $varValue))
)
)
)
);
$query_json_string = json_encode($query_array);
But if you have a problem with that, heredoc syntax should work for you..
$query_json_string = <<<JSON
{
"fields" : [ "timestamp", "user.raw", "mailbox", "verb", "backend" ],
"size" : 1,
"sort" : [ { "#timestamp" : { "order" : $varOrder } } ],
"query" : {
"bool": {
"must": [
{ "match" : { "verb" : "open" } },
{ "term" : { $varField : $varValue } }
]
}
}
}
JSON;
// if you wanted PHP array,
$query_array = json_decode($query_json_string, true);

You can switch the from single to double quotes and escape your double qoutes
$query_json_string = "{
\"fields\" : [ \"timestamp\", \"user.raw\", \"mailbox\", \"verb\", \"backend\" ],
\"size\" : 1,
\"sort\" : [ { \"#timestamp\" : { \"order\" : $varOrder } } ],
\"query\" : {
\"bool\": {
\"must\": [
{ \"match\" : { \"verb\" : \"open\" } },
{ \"term\" : { $varField : $varValue } }
]
}
}
}";

Related

Change key indexing format for nth level of nested array in php

I have nth level of nested array with string naming key , I want to convert that in indexing key only for array of item key.
i tried it to convert but that conversation only possible for limited level of nested array rather than nth level .
Input array:
$input_array= [
"NOCPL-All -working" => [
"name" => "NOCPL-All -working",
"item" => [
"apis for web" => [
"name" => "apis for web",
"item" => [
"0" => [
"name" => "update user branch maps"
]
]
],
"update user web" => [
"name" => "update user web",
"item" => [
"0" => [
"name" => "update user"
],
"1" => [
"name" => "add user"
]
]
]
]
]
];
I tried below code to convert indexing of 'item' nested array for limited level
function cleanArrayKeys($arr) {
foreach($arr as $k=>$arr1) {
if(isset($arr[$k]['item'])) {
$arr[$k]['item'] = array_values($arr[$k]['item']);
foreach($arr[$k]['item'] as $k1=>$arr2) {
if(isset($arr[$k]['item'][$k1]['item'])) {
$arr[$k]['item'][$k1]['item'] = array_values($arr[$k]['item'][$k1]['item']);
foreach($arr[$k]['item'][$k1]['item'] as $k3=>$arr3) {
if(isset($arr[$k]['item'][$k1]['item'][$k3]['item'])) {
$arr[$k]['item'][$k1]['item'][$k3]['item'] = array_values($arr[$k]['item'][$k1]['item'][$k3]['item']);
foreach($arr[$k]['item'][$k1]['item'][$k3]['item'] as $k4=>$arr4) {
if(isset($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'])) {
$arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'] = array_values($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item']);
foreach($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'] as $k5=>$arr5) {
if(isset($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'][$k5]['item'])) {
$arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'][$k5]['item'] = array_values($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'][$k5]['item']);
}
}
}
}
}
}
}
}
}
}
return $arr;
}
print_r(cleanArrayKeys($input_array));
?>
Expected Output :
[
"NOCPL-All -working" => [
"name" => "NOCPL-All -working",
"item" => [
"0" => [
"name" => "apis for web",
"item" => [
"0" => [
"name" => "update user branch maps"
],
"1" => [
"name" => "add user branch maps"
]
]
],
"1" => [
"name" => "update user web",
"item" => [
"0" => [
"name" => "update user"
]
]
]
]
]
];
Try using a recursion:
function aValues(&$arr) {
if (array_key_exists('item', $arr)) {
$arr['item'] = array_values($arr['item']);
}
foreach ($arr['item'] as &$el) {
if (array_key_exists('item', $el)) {
aValues($el);
}
}
}
aValues($input_array);
print_r($input_array);

Retrieve data from the name of a directory in my database

I use php and laravel. I have a column named MsgBody. In this column, there is an array as follows.
"MsgBody": [
{
"MsgType": "TIMCustomElem",
"MsgContent": {
"Desc": "",
"Ext": "",
"Sound": ""
}
}
]
It seems that your data are JSON. You can decode this using json_decode.
You should be able to access what you want with something like:
$column = [
"MsgBody" => [
[
"MsgType" => "TIMCustomElem",
"MsgContent" => [
"Desc" => "",
"Data" => "{\"conversationDesc\":\"abc\"}",
"Ext" => "",
"Sound" => ""
]
]
]
];
$firstMessageContent = $column["MsgBody"][0]["MsgContent"];
$decodedData = json_decode($firstMessageContent["Data"], true);
$conversationDesc = $decodedData["conversationDesc"];
assert($conversationDesc === "abc");

PHP wrap array elements in separate arrays

I have the following array:
$arr = [
"elem-1" => [ "title" => "1", "desc" = > "" ],
"elem-2" => [ "title" => "2", "desc" = > "" ],
"elem-3" => [ "title" => "3", "desc" = > "" ],
"elem-4" => [ "title" => "4", "desc" = > "" ],
]
First I need to change the value from [ "title" => "1", "desc" = > "" ] to 1 (title's value).
I did this using array_walk:
array_walk($arr, function(&$value, $key) {
$value = $value["title"];
});
This will replace my value correctly. Our current array now is:
$arr = [
"elem-1" => "1",
"elem-2" => "2",
"elem-3" => "3",
"elem-4" => "4",
]
Now, I need to transform each element of this array into its own subarray. I have no idea on how to do this without a for loop. This is the desired result:
$arr = [
[ "elem-1" => "1" ],
[ "elem-2" => "2" ],
[ "elem-3" => "3" ],
[ "elem-4" => "4" ],
]
You can change your array_walk callback to produce that array.
array_walk($arr, function(&$value, $key) {
$value = [$key => $value["title"]];
});
Run the transformed array through array_values if you need to get rid of the string keys.
$arr = array_values($arr);
To offer an alternative solution you could achieve all of this with array_map
<?php
$arr = [
"elem-1" => [ "title" => "1", "desc" => "" ],
"elem-2" => [ "title" => "2", "desc" => "" ],
"elem-3" => [ "title" => "3", "desc" => "" ],
"elem-4" => [ "title" => "4", "desc" => "" ],
];
function convertToArray($key,$elem){
return [$key => $elem['title']];
}
$arr = array_map("convertToArray", array_keys($arr), $arr);
echo '<pre>';
print_r($arr);
echo '</pre>';
?>
outputs
Array
(
[0] => Array
(
[elem-1] => 1
)
[1] => Array
(
[elem-2] => 2
)
[2] => Array
(
[elem-3] => 3
)
[3] => Array
(
[elem-4] => 4
)
)
It doesn't make much sense to use array_walk() and modify by reference because the final result needs to have completely new keys on both levels. In other words, the output structure is completely different from the input and there are no salvageable/mutable parts. Mopping up the modified array with array_values() only adds to the time complexity cost.
array_map() has to bear a cost to time complexity too because array_keys() must be passed in as an additional parameter.
If you want to use array_walk(), use use() to modify the result array. This will allow you to enjoy the lowest possible time complexity.
More concise than array_walk() and cleaner to read, I would probably use a classic foreach() in my own project.
Codes: (Demo)
$result = [];
array_walk(
$arr,
function($row, $key) use(&$result) {
$result[] = [$key => $row['title']];
}
);
Or:
$result = [];
foreach ($arr as $key => $row) {
$result[] = [$key => $row['title']];
}
var_export($result);
You need to use array_map like
$new_arr= array_map(function($key,$val){
return [$key => $val['title']];},array_keys($arr),$arr);

Encoding data into JSON object in php

I have to send the JSON in the following format:
{
"suomottoRegVO" : {
"rgdtls" : {
"aplty" : APLSM
},
"docls" : [
{
"id" : 123,
"ty" : 101,
},
{
"id" : 123,
"ty" : 101,
}
],
"todtls":{
"tonm":"Gaurav Kumar",
"todg":"Tax Official",
"pl":"Bangalore",
"dt":" 12-04-2015 23:05:23"
}
}
}
I am using the following code to encode it into JSON and since docls is an array, I am using a loop to put two values into it.
$json_string = array(
'suomottoRegVO' => array (
'rgdtls'=> array(
'aplty'=>'APLSM',
),
'todtls' => array(
'tonm' => 'Gaurav Kumar',
'todg' => 'Tax Official',
'pl' => 'Bangalore',
'dt' => ' 12-04-2015 23:05:23',
)
)
);
for ($i=0; $i<2; $i++) {
$docls[] = array (
'id' => '123',
'ty' => '101'
);
}
$json_string['docls']=$docls;
json_encode($json_string);
When I print the JSON, it becomes:
{
"suomottoRegVO" : {
"rgdtls" : {
"aplty" : APLSM
},
"todtls":{
"tonm":"Gaurav Kumar",
"todg":"Tax Official",
"pl":"Bangalore",
"dt":" 12-04-2015 23:05:23"
}
}
"docls" : [
{
"id" : 123,
"ty" : 101,
},
{
"id" : 123,
"ty" : 101,
}
],
}
The "docls" should be inside "suomottoRegVO" but is not. Please anyone guide me to solve this.
Try this...
You need to paas your array under $json_string['suomottoRegVO'] so try this way...
$json_string['suomottoRegVO']['docls']=$docls;
json_encode($json_string);

mongodb avg in aggregation of array element

I have the following collection structure
{
"_id": {
"d_timestamp": NumberLong(1429949699),
"d_isostamp": ISODate("2015-04-25T08:14:59.0Z")
},
"XBT-USD-cpx-okc": [
{
"buySpread": -1.80081
}
I run the following aggregation
$spreadName ='XBT-USD-stp-nex';
$pipe = array(
array(
'$match' => array(
'_id.d_isostamp' => array(
'$gt' => $start, '$lt' => $end
)
)
),
array(
'$project' => array(
'sellSpread' =>'$'.$spreadName.'.sellSpread',
)
),
array(
'$group' => array(
'_id' => array(
'isodate' => array(
'$minute' => '$_id.d_isostamp'
)
),
'rsell_spread' => array(
'$avg' => '$sellSpread'
),
)
),
);
$out = $collection->aggregate($pipe ,$options);
and I get as a result the value 0 for rsell_spread whereas if I run a $max for instance instead of an $avg in the $group , I get an accurate value for rsell_spread , w/ the following structure
{
"_id": {
"isodate": ISODate("2015-04-25T08:00:58.0Z")
},
"rsell_spread": [
-4.49996▼
]
}
So I have two questions :
1/ How come does the $avg function does not work?
2/ How can I can a result not in an array when I use $max for example (just a regular number)?
The $avg group accumulator operator does work, it's only that in your case it is being applied to an element in an array and thus gives the "incorrect" result.
When you use the $max group accumulator operator, it returns the the highest value that results from applying an expression to each document in a group of documents, thus in your example it returned the maximum array.
To demonstrate this, consider adding a few sample documents to a test collection in mongoshell:
db.test.insert([
{
"_id" : {
"d_timestamp" : NumberLong(1429949699),
"d_isostamp" : ISODate("2015-04-25T08:14:59.000Z")
},
"XBT-USD-stp-nex" : [
{
"sellSpread" : -1.80081
}
]
},
{
"_id" : {
"d_timestamp" : NumberLong(1429949710),
"d_isostamp" : ISODate("2015-04-25T08:15:10.000Z")
},
"XBT-USD-stp-nex" : [
{
"sellSpread" : -1.80079
}
]
},
{
"_id" : {
"d_timestamp" : NumberLong(1429949720),
"d_isostamp" : ISODate("2015-04-25T08:15:20.000Z")
},
"XBT-USD-stp-nex" : [
{
"sellSpread" : -1.80083
}
]
},
{
"_id" : {
"d_timestamp" : NumberLong(1429949730),
"d_isostamp" : ISODate("2015-04-25T08:15:30.000Z")
},
"XBT-USD-stp-nex" : [
{
"sellSpread" : -1.80087
}
]
}
])
Now, replicating the same operation above in mongoshell:
var spreadName = "XBT-USD-stp-nex",
start = new Date(2015, 3, 25),
end = new Date(2015, 3, 26);
db.test.aggregate([
{
"$match": {
"_id.d_isostamp": { "$gte": start, "$lte": end }
}
},
{
"$project": {
"sellSpread": "$"+spreadName+".sellSpread"
}
}/*,<--- deliberately omitted the $unwind stage from the pipeline to replicate the current pipeline
{
"$unwind": "$sellSpread"
}*/,
{
"$group": {
"_id": {
"isodate": { "$minute": "$_id.d_isostamp"}
},
"rsell_spread": {
"$avg": "$sellSpread"
}
}
}
])
Output:
/* 0 */
{
"result" : [
{
"_id" : {
"isodate" : 15
},
"rsell_spread" : 0
},
{
"_id" : {
"isodate" : 14
},
"rsell_spread" : 0
}
],
"ok" : 1
}
The solution is to include an $unwind operator pipeline stage after the $project step, this will deconstruct the XBT-USD-stp-nex array field from the input documents and outputs a document for each element. Each output document replaces the array with an element value. This will then make it possible for the $avg group accumulator operator to work.
Including this will give the aggregation result:
/* 0 */
{
"result" : [
{
"_id" : {
"isodate" : 15
},
"rsell_spread" : -1.80083
},
{
"_id" : {
"isodate" : 14
},
"rsell_spread" : -1.80081
}
],
"ok" : 1
}
So your final working aggregation in PHP should be:
$spreadName ='XBT-USD-stp-nex';
$pipe = array(
array(
'$match' => array(
'_id.d_isostamp' => array(
'$gt' => $start, '$lt' => $end
)
)
),
array(
'$project' => array(
'sellSpread' =>'$'.$spreadName.'.sellSpread',
)
),
array('$unwind' => '$sellSpread'),
array(
'$group' => array(
'_id' => array(
'isodate' => array(
'$minute' => '$_id.d_isostamp'
)
),
'rsell_spread' => array(
'$avg' => '$sellSpread'
),
)
),
);
$out = $collection->aggregate($pipe ,$options);

Categories