Add a row to an array - php

I'm trying to create a function, in order to create an array.
I'm able to create this array :
$result = [ 'data' =>
[0 =>
[
'months' => $months[0],
'years' => $years[0],
'date' => $date[0],
'hr' => $hr[0],
'isms' => $isms[0],
'product' => $product[0],
'exploitation' => $exploitation[0]
],
],
];
And my prob is that i would like to create a function(PHP) in order to add some rows in this array. I would like to have this kind of result :
$result = [ 'data' =>
[0 =>
[
'months' => $months[0],
'years' => $years[0],
'date' => $date[0],
'hr' => $hr[0],
'isms' => $isms[0],
'product' => $product[0],
'exploitation' => $exploitation[0]
],
[
'months' => $months[0],
'years' => $years[0],
'date' => $date[0],
'hr' => $hr[0],
'isms' => $isms[0],
'product' => $product[0],
'exploitation' => $exploitation[0]
]
]
];
But everytime i try to push, it creates a "1=>...."
What can I do ? Could you help me ?

You need to do like below:-
$result['data'][] = [
'months' => $months[0],
'years' => $years[0],
'date' => $date[0],
'hr' => $hr[0],
'isms' => $isms[0],
'product' => $product[0],
'exploitation' => $exploitation[0]
];
Output:-https://eval.in/990308

Your first array should be like this -
$result = [ 'data' =>
[0 =>
[0 =>
[
'months' => $months[0],
'years' => $years[0],
'date' => $date[0],
'hr' => $hr[0],
'isms' => $isms[0],
'product' => $product[0],
'exploitation' => $exploitation[0]
],
],
],
];
Now use below code to add new row to your array -
$row = [
'months' => $months[0],
'years' => $years[0],
'date' => $date[0],
'hr' => $hr[0],
'isms' => $isms[0],
'product' => $product[0],
'exploitation' => $exploitation[0]
];
$result['data'][0][] = $row;
print_r($result);

Related

Filter and re-format a given multi-dimensional array (php)

I have been given a dataset by my tutor and asked to carry out the following:
// make a function
// get skus of which the figure of the average monthly sales unit is equal or greater than 350
// array in the following format:
[
'id' => 11102,
'sku' => 'TEST_3',
'alternates' => [
'asin' => [
'code' => '50',
'value' => 'JL1235',
],
'barcode' => [
'code' => '10',
'value' => 'JS160694',
],
],
'commodityCode' => '9989898889',
'price' => [
'value' => 145.99,
],
'instructions' => [
'picking' => [
'code' => 'PICK',
'value' => 'I\'VE JUST HAD AN UNHAPPY LOVE AFFAIR, SO I DON\'T SEE WHY ANYBODY ELSE SHOULD HAVE A GOOD TIME.',
],
],
'quantity' => [
'inner' => 100,
'masterCarton' => 200,
'pallet' => 300,
],
'averageMonthlyUnitSales' => 100,
'created_at' => '2022-03-13 04:14:12'
],
Example dataset is here:
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
I can perform the first part of the assignment (filter for averageMonthlyUnitSales>350) by carrying out something like:
$filtered_array= array_filter($data, function($item){
return ($item['averageMonthlyUnitSales']>350);
});
But I am not too sure how I can go about getting a new array in the required format?
The only way I can see to simply do both is as follows.
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
$new = [];
foreach ($data as $line){
if ( $line['averageMonthlyUnitSales'] > 350 ){
// reformat the array
$new = [
'id' => $line['id'],
'sku' => $line['sku'],
'alternates' => ['asin' => ['code'=>$line['asin_code'], 'value'=>$line['asin_value'] ],
'barcode' => ['code'=> $line['barcode'], 'value' => $line['barcode_value']]
],
'commodityCode' => $line['commodityCode'],
'price' => [ 'value' => $line['price'] ],
'instructions' => ['picking' => ['code' => $line['picking_instruction_code'], 'value'=> $line['picking_instruction_value']]
],
'quantity' =>['inner' =>$line['qty_inner'], 'masterCarton' =>$line['qty_masterCarton'], 'pallet'=>$line['qty_pallet']],
'averageMonthlyUnitSales'=>$line['averageMonthlyUnitSales'],
'created_at'=>$line['averageMonthlyUnitSales']
];
}
}
print_r($new);
RESULT
PHP 8.1.3
Array
(
[id] => 9947
[sku] => test_1
[alternates] => Array
(
[asin] => Array
(
[code] => 50
[value] =>
)
[barcode] => Array
(
[code] => 10
[value] => js160694
)
)
[commodityCode] => 9989898889
[price] => Array
(
[value] => 120.50
)
[instructions] => Array
(
[picking] => Array
(
[code] => PICK
[value] =>
)
)
[quantity] => Array
(
[inner] => 120
[masterCarton] => 200
[pallet] => 1200
)
[averageMonthlyUnitSales] => 750
[created_at] => 750
)

How to display array data in Yii2 GridView using ActiveDataprovider

I have an array of data very similar to this
$array = [
'FUNDSCTR' => '10000001'
'RCMMTITEM' => 'R400001'
'YEAR' => '2018'
'CONSUMA' => 5898257
'CONSUME' => 30140
'AVAIL' => 5868117 ]
and use in ArrayDataProvider
$dataProvider = new ArrayDataProvider(['allModels' => $array,]);
code in gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'label' => 'Year',
'attribute'=>'YEAR',
],
[
'label' => 'Fund',
'attribute'=>'FUNDSCTR',
],
[
'label' => 'Item',
'attribute'=>'RCMMTITEM',
],
[
'label' => 'Consumeable',
'attribute'=>'CONSUMA',
],
[
'label' => 'Consumed',
'attribute'=>'CONSUME',
],
[
'attribute'=>'Available',
'value'=> 'AVAIL',
],
]);
?>
</div>
resluts
print_r($dataProvider); ~ $arrays return
yii\data\ArrayDataProvider Object ( [key] => [allModels] => Array ( [RFUNDSCTR] => 10000001 [RCMMTITEM] => R400013 [RYEAR] => 2018 [CONSUMA] => 5898257 [CONSUME] => 30140 [AVAIL] => 5868117 )
I need to show in gridView data like this.
To work properly, an array which you pass to ArrayDataProvider should be two-dimensional, like this:
$array = [
[
'FUNDSCTR' => '10000001',
'RCMMTITEM' => 'R400001',
'YEAR' => '2018',
'CONSUMA' => 5898257,
'CONSUME' => 30140,
'AVAIL' => 5868117,
],
[
'FUNDSCTR' => '10000001',
'RCMMTITEM' => 'R400001',
'YEAR' => '2018',
'CONSUMA' => 5898257,
'CONSUME' => 30140,
'AVAIL' => 5868117,
],
[
'FUNDSCTR' => '10000001',
'RCMMTITEM' => 'R400001',
'YEAR' => '2018',
'CONSUMA' => 5898257,
'CONSUME' => 30140,
'AVAIL' => 5868117,
]
];

php Two-dimensional arrays are sorted and grouped according to the value of the key

I have a two-dimensional array that needs to be grouped according to assemble_id ,I can't get the result I want by traversing. Below is the original array:
$list = [
0 =>[
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
1 =>[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
],
2 =>[
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
3 =>[
'nickname' => 'Jelly',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
]
];
What i need is:
[
[
'assemble_id' => 'c1d0zUbmP',
'assemble_group' => [
[
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
[
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
]
]
],
[
'assemble_id' => 'ED6OJX4VV',
'assemble_group' => [
[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
],
[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
]
]
]
];
I need to group according to the same assemble_id and put is_group=1 in the first of each group.
hery is my code:
function formatData($list)
{
$data = [];
foreach ($list as $k=>$v) {
$list[$k]['created_at'] = formatDate($v['created_at'],'Y-m-d H:i');
$list[$k]['phone'] = setMobile($v['phone']);
$data[$k]['assemble_id'] = $v['assemble_id'];
$data[$v['assemble_id']][] = $list[$k];
}
return $data;
}
This did not get the results I wanted.
Assign temporary associative keys, then remove them when finished looping.
Code: (Demo)
foreach ($list as $row) {
$result[$row['assemble_id']]['assemble_id'] = $row['assemble_id'];
$result[$row['assemble_id']]['assemble_group'][] = [
'nickname' => $row['nickname'],
'phone' => $row['phone'], // add your function here
'is_group' => $row['is_group'],
'created_at' => $row['created_at'], // add your function here
'assemble_id' => $row['assemble_id'],
'status' => $row['status']
];
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'assemble_id' => 'c1d0zUbmP',
'assemble_group' =>
array (
0 =>
array (
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1,
),
1 =>
array (
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1,
),
),
),
1 =>
array (
'assemble_id' => 'ED6OJX4VV',
'assemble_group' =>
array (
0 =>
array (
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1,
),
1 =>
array (
'nickname' => 'Jelly',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1,
),
),
),
)

MongoDB php add tz offset to aggregate query and average output

I know that mongoDB (php-driver) saves the date as ISODate in Milliseconds UTC, so i have to add a offset for my timezone (GMT +2:00) to the aggregate query.
$dateStart = new MongoDB\BSON\UTCDateTime(strtotime("now 00:00:00") * 1000);
$dateEnd = new MongoDB\BSON\UTCDateTime(time() * 1000);
$queryHours = [
['$match' => ['date' => ['$gte' => $dateStart, '$lte' => $dateEnd]]],
['$project' => ['date' => ['$add' => ['$date', 2*60*60000]]]],
['$group' => ['_id' => ['$hour' => '$date'], 'average' => ['$avg' => '$zapi.apiRes.curlHeader.total_time']]],
['$sort' => ['_id' => 1]],
];
The $hour result seems to be correct, but i get no "average" result and i doesn't see my failure since days, staring at the code. :(
Sample Document:
{
"_id" : ObjectId("58e4e3ca135d9e038242b248"),
"date" : ISODate("2017-04-05T12:32:10Z"),
[...]
"zapi" : {
"apiRes" : {
"apiCall" : {
[...]
},
"curlHeader" : {
"url" : "http://api:/api/foo",
"http_code" : 204,
"total_time" : 0.02906399999999998,
[...]
},
}
},
}
I know that mongoDB working on this feature nativ: https://jira.mongodb.org/browse/SERVER-28611.
Thanks and best regards,
lun4tic
You can replace your $project stage with $addFields if you are on 3.4 version.
$queryHours =
[
['$match' => ['date' => ['$gte' => $dateStart, '$lte' => $dateEnd]]],
['$addFields' => ['date' => ['$add' => ['$date', 2*60*60000]]]],
['$group' => ['_id' => ['$hour' => '$date'], 'average' => ['$avg' => '$zapi.apiRes.curlHeader.total_time']]],
['$sort' => ['_id' => 1]],
];
Or
You can simply move the timezone arithmetic from $project stage into $group stage.
['$group' => ['_id' => ['$hour' => [ '$add' => ['$date', 2*60*60000] ] ], 'average' => ['$avg' => '$zapi.apiRes.curlHeader.total_time']]],
Your error should be the project phase. You have to add 'zapi.apiRes.curlHeader.total_time' => 1 so try this:
$queryHours = [
['$match' => ['date' => ['$gte' => $dateStart, '$lte' => $dateEnd]]],
['$project' => ['date' => ['$add' => ['$date', 2*60*60000]], 'zapi.apiRes.curlHeader.total_time' => 1]],
['$group' => ['_id' => ['$hour' => '$date'], 'average' => ['$avg' => '$zapi.apiRes.curlHeader.total_time']]],
['$sort' => ['_id' => 1]],
];

MongoDB or operator for range

This is the range Im using :
$Range1=array('$or'=>array(
array( '$gte' => floatval($O_NLA), '$lte' => $N_NLA ),
array( '$gte' => floatval($N_SLA), '$lte' => $O_SLA ))
);
$Range2=array('$or'=>array(
array( '$gte' => floatval($N_SLO), '$lte' => $O_SLO ),
array( '$gte' => floatval($O_NLO), '$lte' => $N_NLO ))
);
A part of my query :
$finder=$reposit->findBy(
array(
'field1' => $Range1,
'field2' => $Range2,
'display'=>1
))->toArray();
"message":"localhost:27017: invalid operator: $or"..... (it's long though)
How can I use $or operator with doctrine without query builder?
findBy([
'$or' => [
['field1' => ['$gte' => floatval($O_NLA), '$lte' => $N_NLA]],
['field1' => ['$gte' => floatval($N_SLA), '$lte' => $O_SLA]],
['field2' => ['$gte' => floatval($N_SLO), '$lte' => $O_SLO]],
['field2' => ['$gte' => floatval($O_NLO), '$lte' => $N_NLO]]
]
, 'display' => 1
])
Or actually:
findBy([
'$and' => [
['$or' => [
['field1' => ['$gte' => floatval($O_NLA), '$lte' => $N_NLA]],
['field1' => ['$gte' => floatval($N_SLA), '$lte' => $O_SLA]],
]],
['$or' => [
['field2' => ['$gte' => floatval($N_SLO), '$lte' => $O_SLO]],
['field2' => ['$gte' => floatval($O_NLO), '$lte' => $N_NLO]]
]]
]
, 'display' => 1
])
To be more precise according to your question.
Since the $or works upon condition which then contains a find() like array.

Categories