I have the following :
$data = [
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 31.25, treatment : 'Pressure' },
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 31.25, treatment : 'Gas' },
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 33.12, treatment : 'Temp' },
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 25.87, treatment : 'Current' },
{model_num : "ABC", revision : "AB", "testRecipeID":86, value : 26.63, treatment : 'Pressure' },
{model_num : "ABC", revision : "AB", "testRecipeID":86, value : 26.00, treatment : 'Gas' },
{model_num : "ABC", revision : "AB", "testRecipeID":86, value : 23.75, treatment : 'Temp' }
];
and i would like to end up with something like this:
var data=[{model_num : "ABC", revision : "AA", "testRecipeID":85, "Pressure":31.25, "Gas":31.25, "Temp": 33.12,"Current":25.87 },{model_num : "ABC", revision : "AB", "testRecipeID":86, "Gas":26.00,"Temp":23.75}]
I know how to do this in JS but not on PHP and it turns out I need to do it in my php so that I can process the large amounts of data that I have. I have this based on another question that I found but It doesn't work it returns a 0
$table = array();
$round_names = array();
$total = array();
foreach ($data as $score)
{
$round_names[] = $score->treatment;
$table[$score->testRecipeID][$score->treatment] = $score->value;
$total[$score->testRecipeID] += $score->value;
print_r($round_names);
}
$round_names = array_unique($round_names);
foreach ($table as $player => $rounds)
{
echo "$player\t";
foreach ($round_names as $round)
echo "$rounds[$round]\t";
echo "$total[$player]\n";
}
any help will be greatly appreciated!
this is how i do it in JS
var result = [];
data.forEach(function(e) {
var a = e.model_num + '|' + e.revision+ '|'e.recipeID;
if(!this[a]) {
this[a] = {model_num: e.model_num, revision: e.revision, recipeID: e.recipeID}
result.push(this[a]);
}
this[a][e.treatment] = e.value;
}, {});
If you need the structure, you can try with JSON Encode:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
Which outputs:
{"a":1,"b":2,"c":3,"d":4,"e":5}
If you need it to be an array, use:
echo json_encode(array($arr));
This is your JS function in PHP
<?php
$data = '[
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 31.25, "treatment" : "Pressure" },
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 31.25, "treatment" : "Gas" },
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 33.12, "treatment" : "Temp" },
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 25.87, "treatment" : "Current" },
{"model_num" : "ABC", "revision" : "AB", "testRecipeID":86, "value" : 26.63, "treatment" : "Pressure" },
{"model_num" : "ABC", "revision" : "AB", "testRecipeID":86, "value" : 26.00, "treatment" : "Gas" },
{"model_num" : "ABC", "revision" : "AB", "testRecipeID":86, "value" : 23.75, "treatment" : "Temp" }
]';
$data = json_decode($data);
$result = [];
foreach ($data as $row) {
$a = $row->model_num . '|' . $row->revision . '|' . $row->testRecipeID;
if (! array_key_exists($a, $result)) {
$result[$a] = [
'model_num' => $row->model_num,
'revision' => $row->revision,
'testRecipeID' => $row->testRecipeID
];
}
}
Because neither of the previous answers managed to provide the desired output, I am compelled to answer this pivot question.
Create a temporary string from the 3 identifying column values.
Use that string as the first level key while grouping related row data.
The null coalescing assignment operator spares you needing to call isset() to check if the group has been encountered before. If it is the first encounter, save the three core elements to the row.
Unconditionally push the dynamically keyed value into the group's row.
5 When finished iterating clear away the temporary keys with array_values().
Code: (Demo)
$data = [
['model_num' => 'ABC', 'revision' => 'AA', 'testRecipeID' => 85, 'value' => 31.25, 'treatment' => 'Pressure'],
['model_num' => 'ABC', 'revision' => 'AA', 'testRecipeID' => 85, 'value' => 31.25, 'treatment' => 'Gas'],
['model_num' => 'ABC', 'revision' => 'AA', 'testRecipeID' => 85, 'value' => 33.12, 'treatment' => 'Temp'],
['model_num' => 'ABC', 'revision' => 'AA', 'testRecipeID' => 85, 'value' => 25.87, 'treatment' => 'Current'],
['model_num' => 'ABC', 'revision' => 'AB', 'testRecipeID' => 86, 'value' => 26.63, 'treatment' => 'Pressure'],
['model_num' => 'ABC', 'revision' => 'AB', 'testRecipeID' => 86, 'value' => 26.0, 'treatment' => 'Gas'],
['model_num' => 'ABC', 'revision' => 'AB', 'testRecipeID' => 86, 'value' => 23.75, 'treatment' => 'Temp']
];
$result = [];
foreach ($data as $row) {
$compositeKey = "{$row['model_num']}-{$row['revision']}-{$row['testRecipeID']}";
$result[$compositeKey] ??= [
'model_num' => $row['model_num'],
'revision' => $row['revision'],
'testRecipeID' => $row['testRecipeID']
];
$result[$compositeKey][$row['treatment']] = $row['value'];
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'model_num' => 'ABC',
'revision' => 'AA',
'testRecipeID' => 85,
'Pressure' => 31.25,
'Gas' => 31.25,
'Temp' => 33.12,
'Current' => 25.87,
),
1 =>
array (
'model_num' => 'ABC',
'revision' => 'AB',
'testRecipeID' => 86,
'Pressure' => 26.63,
'Gas' => 26.0,
'Temp' => 23.75,
),
)
Related
This is my array of objects in php:
Array( [0]=>
Array (
[added_time] => 2018-12-09 14:00:00+00
[id] => 123
[places] =>
[{
"place_id" : 1,
"total" : 5,
"empty" : 0,
"weight" : 68000,
"persons" :
[{
"person_id" : 2,
"person_name" : "ABC",
"total":100
},
{
"person_id" : 3
"person_name" : "DEF",
"total":200
}]
},
"place_id" : 4,
"total" : 10,
"empty" : 0,
"weight" : 54000,
"persons" :
[{
"person_id" : 2,
"person_name" : "ABC"
"total":100
},
{
"person_id" : 6
"person_name" : "GHI",
}
]
],
),
Array (
[added_time] => 2018-12-09 15:00:00+00
[id] => 456
[places] =>
[{
"place_id" : 1,
"total" : 5,
"empty" : 0,
"weight" : 68000,
"persons" :
[{
"person_id" : 2,
"person_name" : "ABC",
"total":200
},
{
"person_id" : 3
"person_name" : "DEF",
"total":300
}]
}]
)
)
I am trying to get sum on the base of person_id like group by person_id.
My desired result should be:
Array (
[added_time] => 2018-12-09 14:00:00+00
[id] => 123
persons:array(
Array([0]=>
[person_id] : 2,
[person_name] : "ABC",
[total]:200
),
Array([1]=>
[person_id] : 3,
[person_name] : "DEF",
[total]:300
),
Array([2]=>
[person_id] : 6,
[person_name] : "GHI",
[total]:500
)
),
[added_time] => 2018-12-09 15:00:00+00
[id] => 123
persons:array(
Array([0]=>
[person_id] : 2,
[person_name] : "ABC",
[total]:200
),
Array([1]=>
[person_id] : 3,
[person_name] : "DEF",
[total]:300
)
)
)
How can I achieve this result. I can use foreach but I don't want to use it because of three iterations.
Is there any other method to achieve this in php and without any performance issue?
You can have that by using PHP function as array-merge, array-column, array-reduce.
Let divided that to 3 step:
1.Sum total for all person given list of persons. Define the the following reduce function:
function reduce($carry, $item)
{
if (!in_array($item["person_name"], array_column($carry, "person_name")))
$carry[] = $item;
else {
foreach($carry as &$person) {
if ($item["person_name"] == $person["person_name"])
$person["total"] += $item["total"];
}
}
return $carry;
}
2.For every element in your array create you output using the reduce from step 1:
function reduceElement($arr) {
$res = array("added_time" => $arr["time"], "id" => $arr["id"]);
$persons = array();
foreach($arr["places"] as $place) {
$persons = array_merge($persons, $place["persons"]);
}
$res["persons"] = array_reduce($persons, "reduce", array());
return $res;
}
3.Combine it all:
$elemA= array("id"=>1, "time"=>"2018", "places" => array(array("persons" => array(array("person_name" => "ABC", "total" => 100), array("person_name" => "DEF", "total" => 200))), array("persons" => array(array("person_name" => "ABC", "total" => 100), array("person_name" => "GHI", "total" => 100)))));
$elemB = array("id"=>2, "time"=>"2017", "places" => array(array("persons" => array(array("person_name" => "ABC", "total" => 200), array("person_name" => "DEF", "total" => 300)))));
$arr = array($elemA, $elemB);
$res = array();
foreach($arr as $obj)
$res[] = reduceElement($obj);
print_r($res);
This will give you the require out put
I'm generating data arrays and want to add 0/null to a specific month, where no data is present, but been struggling with it for few days.
I'm generating the final dataset array like this
foreach ($patients as $key => $item) {
$labels[] = $item['month'];
usort($labels, "compare_months");
$labels = array_unique($labels);
$chartData[ $item['brand_name'] ]['data'][] =
$item['patientsCount'];
$chartData[ $item['brand_name'] ]['brand_data'] = [
"name" => $item['brand_name'],
"color" => $item['color']
];
}
foreach ($chartData as $item) {
$pointRadius++;
$dataSet[] = [
'label' => $item['brand_data']['name'],
'data' => $item['data'],
'pointRadius' => $pointRadius,
'fill' => false,
'borderWidth' => 1,
'backgroundColor' => "#" . $item['brand_data']['color'],
'borderColor' => "#" . $item['brand_data']['color'],
];
}
$finalData[] = [
'labels' => $labels,
'datasets' => $dataSet
];
Which gives me this json response
[
{
"labels":[
"April",
"May",
"June",
"July"
],
"datasets":[
{
"label":"Medicine 1",
"data":[
1
],
"pointRadius":1,
"fill":false,
"borderWidth":1,
"backgroundColor":"#ea5f2d",
"borderColor":"#ea5f2d"
},
{
"label":"Medicine 2",
"data":[
1,
1,
1
],
"pointRadius":2,
"fill":false,
"borderWidth":1,
"backgroundColor":"#ffb400",
"borderColor":"#ffb400"
},
{
"label":"Medicine 3",
"data":[
1,
1,
2
],
"pointRadius":3,
"fill":false,
"borderWidth":1,
"backgroundColor":"#ff7777",
"borderColor":"#ff7777"
},
{
"label":"Medicine 4",
"data":[
1,
1,
2
],
"pointRadius":4,
"fill":false,
"borderWidth":1,
"backgroundColor":"#64a36f",
"borderColor":"#64a36f"
},
{
"label":"Medicine 5",
"data":[
2
],
"pointRadius":5,
"fill":false,
"borderWidth":1,
"backgroundColor":"#e7e6fc",
"borderColor":"#e7e6fc"
}
]
}
]
As you can see, for some items, the data object only contains 1 value which is wrong, because I need to add zeros to months, where there is no data.
Can this be done in MySQL or is it better to use PHP and how?
EDIT 1
Here is the array I am currently getting.
Array
(
[MEDICINE 1] => Array
(
[April] => 1
)
[MEDICINE 2] => Array
(
[April] => 1
[July] => 2
)
[MEDICINE 3] => Array
(
[April] => 1
[May] => 1
[July] => 2
)
[MEDICINE 4] => Array
(
[May] => 1
[July] => 3
)
[MEDICINE 5] => Array
(
[June] => 2
)
[MEDICINE 6] => Array
(
[July] => 1
)
)
As you can see some medicines have data for only one month and I want to add these months to the medicine array with 0 value, if there is no data.
Add this line after each foreach: if (count($item['data']) < 2) continue;. Because you need skip if length of data is smaller than 2.
foreach ($patients as $key => $item) {
if (count($item['data']) < 2) continue;
$labels[] = $item['month'];
usort($labels, "compare_months");
$labels = array_unique($labels);
$chartData[ $item['brand_name'] ]['data'][] =
$item['patientsCount'];
$chartData[ $item['brand_name'] ]['brand_data'] = [
"name" => $item['brand_name'],
"color" => $item['color']
];
}
foreach ($chartData as $item) {
$pointRadius++;
if (count($item['data']) < 2) continue;
$dataSet[] = [
'label' => $item['brand_data']['name'],
'data' => $item['data'],
'pointRadius' => $pointRadius,
'fill' => false,
'borderWidth' => 1,
'backgroundColor' => "#" . $item['brand_data']['color'],
'borderColor' => "#" . $item['brand_data']['color'],
];
}
$finalData[] = [
'labels' => $labels,
'datasets' => $dataSet
];
I am working on building simple recommendation engine using PHP and MongoDB. I am trying to find the average rating of each item but I did not figure it out yet.
my collection is:
{
"_id" : 1,
"item" : "efg",
"rating" : 5.0,
"date" : ISODate("2014-01-01T08:00:00.000Z")
}
{
"_id" : 2,
"item" : "efg",
"rating" : 2.0,
"date" : ISODate("2014-01-01T08:00:00.000Z")
}
{
"_id" : 3,
"item" : "abc",
"rating" : 5.0,
"date" : ISODate("2014-01-01T08:00:00.000Z")
}
{
"_id" : 4,
"item" : "abc",
"rating" : 4.0,
"date" : ISODate("2014-01-01T08:00:00.000Z")
}
{
"_id" : 5,
"item" : "xyz",
"rating" : 3.0,
"date" : ISODate("2014-01-01T08:00:00.000Z")
}
This is the code I have tried recently:
$out = $sales->aggregate(
array(
'$group' => array(
'_id' => '$item',
'avgrating' => array('$avg' => '$rating')
)
)
);
var_dump($out);
Please try executing following code snippet
$out = $sales->aggregate(
array(
'$group' => array(
'_id' => array('item' => '$item'),
'avgrating' => array('$avg' => '$rating')
)
)
);
var_dump($out);
I got it working using this code:
$pipeline = array(
array(
'$group' => array(
'_id' => '$itemId',
'avgrating' => array('$avg' => '$rating'),
),
)
);
$out = $sales1->aggregate($pipeline);
var_dump($out)
this is the json that my code produces
{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555}
}
and this is the code
<?php
$c = array('a' => 11111, 'b' => 222222, 'c' => 33333, 'd' => 444454, 'e' => 55555555 );
$arr = array('aaa' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 , 'fff'=>$c);
echo json_encode($arr);
?>
but I want to have some structure like this
{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555},
"last":[
{
"id": 8817,
"loc": "NEW YORK CITY"
},
{
"id": 2873,
"loc": "UNITED STATES"
},
{
"id": 1501,
"loc": "NEW YORK STATE"
}
]
}
I am new in json and php and I need this fast so I do not have time to read about this json structure... So please if someone know how to add this last element please provide some php code.
Thanks,
Take the "json-encoded" string and pass it to json_decode()
assign the return value to a variable
pass that variable to var_export() to get a "php-encoded" string representation of the data.
e.g.
<?php
$json = '{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555},
"last":[
{
"id": 8817,
"loc": "NEW YORK CITY"
},
{
"id": 2873,
"loc": "UNITED STATES"
},
{
"id": 1501,
"loc": "NEW YORK STATE"
}
]
}';
$php = json_decode($json, true);
echo var_export($php);
prints
array (
'aaa' => 1,
'b' => 2,
'c' => 3,
'd' => 4,
'e' => 5,
'fff' =>
array (
'a' => 11111,
'b' => 222222,
'c' => 33333,
'd' => 444454,
'e' => 55555555,
),
'last' =>
array (
0 =>
array (
'id' => 8817,
'loc' => 'NEW YORK CITY',
),
1 =>
array (
'id' => 2873,
'loc' => 'UNITED STATES',
),
2 =>
array (
'id' => 1501,
'loc' => 'NEW YORK STATE',
),
),
)
I'm really struggling to parse a JSON string with PHP. The link below contains the JSON that I'm trying to parse: http://finance.google.com/finance/info?client=ig&q=NASDAQ:MSFT,NASDAQ:GOOG,NASDAQ:AAPL
Can anyone help, I've tried everything but I'm still new to this.
Thanks.
if there are no // in start of string use
$json_data = '[ { "id": "358464" ,"t" : "MSFT" ,"e" : "NASDAQ" ,"l" : "25.80" ,"l_cur" : "25.80" ,"s": "1" ,"ltt":"4:00PM EDT" ,"lt" : "Sep 2, 4:00PM EDT" ,"c" : "-0.41" ,"cp" : "-1.56" ,"ccol" : "chr" ,"el": "25.44" ,"el_cur": "25.44" ,"elt" : "Sep 6, 4:35AM EDT" ,"ec" : "-0.36" ,"ecp" : "-1.40" ,"eccol" : "chr" ,"div" : "0.16" ,"yld" : "2.48" } ,{ "id": "694653" ,"t" : "GOOG" ,"e" : "NASDAQ" ,"l" : "524.84" ,"l_cur" : "524.84" ,"s": "0" ,"ltt":"4:00PM EDT" ,"lt" : "Sep 2, 4:00PM EDT" ,"c" : "-7.66" ,"cp" : "-1.44" ,"ccol" : "chr" } ,{ "id": "22144" ,"t" : "AAPL" ,"e" : "NASDAQ" ,"l" : "374.05" ,"l_cur" : "374.05" ,"s": "1" ,"ltt":"4:00PM EDT" ,"lt" : "Sep 2, 4:00PM EDT" ,"c" : "-6.98" ,"cp" : "-1.83" ,"ccol" : "chr" ,"el": "371.45" ,"el_cur": "371.45" ,"elt" : "Sep 6, 6:02AM EDT" ,"ec" : "-2.60" ,"ecp" : "-0.70" ,"eccol" : "chr" ,"div" : "" ,"yld" : "" } ]';
$json_object = json_decode($json_data);
with // use
$json_data = '......';
$json_data = trim(substr($json_data,2));
$json_object = json_decode($json_data);
result
array (
0 =>
stdClass::__set_state(array(
'id' => '358464',
't' => 'MSFT',
'e' => 'NASDAQ',
'l' => '25.80',
'l_cur' => '25.80',
's' => '1',
'ltt' => '4:00PM EDT',
'lt' => 'Sep 2, 4:00PM EDT',
'c' => '-0.41',
'cp' => '-1.56',
'ccol' => 'chr',
'el' => '25.44',
'el_cur' => '25.44',
'elt' => 'Sep 6, 4:35AM EDT',
'ec' => '-0.36',
'ecp' => '-1.40',
'eccol' => 'chr',
'div' => '0.16',
'yld' => '2.48',
)),
1 =>
stdClass::__set_state(array(
'id' => '694653',
't' => 'GOOG',
'e' => 'NASDAQ',
'l' => '524.84',
'l_cur' => '524.84',
's' => '0',
'ltt' => '4:00PM EDT',
'lt' => 'Sep 2, 4:00PM EDT',
'c' => '-7.66',
'cp' => '-1.44',
'ccol' => 'chr',
)),
2 =>
stdClass::__set_state(array(
'id' => '22144',
't' => 'AAPL',
'e' => 'NASDAQ',
'l' => '374.05',
'l_cur' => '374.05',
's' => '1',
'ltt' => '4:00PM EDT',
'lt' => 'Sep 2, 4:00PM EDT',
'c' => '-6.98',
'cp' => '-1.83',
'ccol' => 'chr',
'el' => '371.45',
'el_cur' => '371.45',
'elt' => 'Sep 6, 6:02AM EDT',
'ec' => '-2.60',
'ecp' => '-0.70',
'eccol' => 'chr',
'div' => '',
'yld' => '',
)),
)
json_decode documentation: http://php.net/json_decode
In your source they are commenting out the JSON. It's pretty weird that they do it, but anyway the // at the start is screwing it up.
$url = 'http://finance.google.com/finance/info?client=ig&q=NASDAQ:MSFT,NASDAQ:GOOG,NASDAQ:AAPL';
$json = preg_replace('#^\s+//#', '', file_get_contents($url) );
$decoded = json_decode( $json );
var_dump( $decoded );