I've been reading around and cannot find a solution that works for the requirement I have. I need to dynamically add values to the 'add' part of this array depending on conditions. I know that you cannot put any if statements inside the array itself.
The correct syntax (from the documentation) is:
$subResult = $gateway->subscription()->create([
'paymentMethodToken' => 'the_token',
'planId' => 'thePlanId',
'addOns' => [
'add' => [
[
'inheritedFromId' => 'addon1',
'amount' => '10'
],
[
'inheritedFromId' => 'addon2',
'amount' => '10'
]
]
]
]);
From what I had read on a similar question on SO, I tried the following (where $addon1 and $addon2 would be the conditions set earlier in the code)
$addon1 = true;
$addon2 = true;
$subResult = $gateway->subscription()->create([
'paymentMethodToken' => 'the_token',
'planId' => 'thePlanId',
'addOns' => [
'add' => [
($addon1 ? array(
[
'inheritedFromId' => 'productAddon1Id',
'amount' => '10'
]) : false),
($addon2 ? array(
[
'inheritedFromId' => 'productAddon2Id',
'amount' => '10'
]) : false)
]
]
]);
But I get back a Warning: XMLWriter::startElement(): Invalid Element Name so I suspect that it does not like the structure and the code fails with a fatal error (interestingly, if I only set the first $addon to true it still comes up with the warning, but does actually work. With two it fails).
Is there another way to do this or did I get the syntax wrong?
I cannot hardcode all the possibilities due to the amount of possible product combinations.
Would appreciate and help. Thank you.
Don't try to do everything at once.
$add = [];
if( $addon1)
$add[] = ['inheritedFromId'=>.......];
if( $addon2)
.....
$subResult = $gateway->subscription()->create([
'paymentMethodToken' => 'the_token',
'planId' => 'thePlanId',
'addOns' => [
'add' => $add
]
]);
you can put if statements in array declarations, it's called ternary operations:
$myArray['key'] = ($foo == 'bar' ? 1 : 2);
this is the basis of how to use.
You're using the array() syntax together with the short array syntax ([]). See the manual. This means that e.g. your first element in add would be an array within an array. Perhaps that's why the XML error is occurring? Better would be:
'add' => [
($addon1 ?
[
'inheritedFromId' => 'productAddon1Id',
'amount' => '10'
] : null),
($addon2 ?
[
'inheritedFromId' => 'productAddon2Id',
'amount' => '10'
] : null)
]
]
Your syntax is fine. you are creating array with this structure
array (size=3)
'paymentMethodToken' => string 'the_token'
(length=9)
'planId' => string 'thePlanId'
(length=9)
'addOns' =>
array (size=1)
'add' =>
array (size=2)
0 =>
array (size=1)
...
1 =>
array (size=1)
...
My quess is that $gateway->subscription()->create() is does not like this structure of your array. Maybe the 'false' as value or the numeric keys. Check what does it expect and try again with new structure of the array.
Related
I write code with some array that have different structure, but I must extract the data to do something else. How can I manager these array?
The array's structure are as follow:
$a = [
'pos1' => 'somedata',
'pos2' => ['data2', 'data3'],
'pos3' => '';
];
$b = [
[
'pos1' => ['data1', 'data2', ['nest1', 'nest2']],
'pos2' => ['data1', 'data2', 'data3'],
],
['data1', 'data2'],
'data4',
];
The array's Index can be a key or a position, and the value of the corresponding index may be a array with the same structure. More tough problem is that the subarray can be nesting, and the time of the nesting has different length.
Fortunately, every array has it's owe fixed structure.
I want to convert the these array to the format as follow. When the index is a value, change it to the keyword; and if the index is a keyword, nothing changed.
$a = [
'pos1' => 'somedata',
'pos2' => [
'pos2_1' => 'data2',
'pos2_2' => 'data3'
],
'pos3' => '';
];
$b = [
'pos1' => [
'pos1_1' => [
'pos1_1_1' => 'data1',
'pos1_1_2' => 'data2',
'pos1_1_3' => [
'pos1_1_3_1' => 'nest1',
'pos1_1_3_2' => 'nest2',
],
],
'pos1_2' => [
'pos1_2_1' => 'data1',
'pos1_2_2' => 'data2',
'pos1_2_3' => 'data3',
],
],
'pos2' => ['data1', 'data2'],
'pos3' => 'data4',
];
My first solution is for every array, write the function to convert the format(the keyword will specify in function). But it is a huge task and diffcult to manage.
The second solution is write a common function, with two argument: the source array and the configuration that specify the keyword to correspondent value index. For example:
$a = [0, ['pos10' => 1]];
$conf = [
// It means that when the value index is 0, it will change it into 'pos1'
'pos1' => 0,
'pos2' => 1,
];
The common funciton will generate the result of:
$result = [
'pos1' => 0,
'pos2' => ['pos10' => 1],
]
But this solution will lead to a problem: the config is diffcult to understand and design, and other people will spend a lot of time to understand the format after conversion.
Is there are some better solution to manage these array that other people can easy to use these array?
Thanks.
I'm doing "inserts" by $push in an array inside my document, and inside it there is a field date that I want to sort it when I use the find(), but not sorting the "_id". How do I sort by date (ordens.dtOrdem)?
<?php
$mongo = (new MongoDB\Client('mongodb://localhost:27017'))->Carteira;
$collection = $mongo->ativo;
/*First way that I've tried*/
$result1 = $collection->find([].['ordens' => ['sort' => ['dtOrdem' => -1]]]);
/*Second way that I've tried*/
$result2 = $collection->find([],['ordens' => ['each' => ['ordens' => ['sort' => ['dtOrdem' = -1]]]]]);
?>
The field 'ordens.dtOrdem' is no been sorted in descending.
You cannot sort an array during a find(). You have two options:
You can specify a $sort operation during your $push to ensure that your elements are added to the array in sorted order.
You can using aggregation to sort the array on retrieval.
Solutions might look something like the following (some modification may be necessary):
// Sorting on push.
$collection->updateOne(
[...],
[ '$push' => [
'ordens' => [
// You MUST include the $each operator here or this will not work.
'$each' => [
[ 'dtOrdem' => 5 ]
],
// Where the magic happens, sorts in descending order.
'$sort'=> [ 'dtOrdem' => -1 ]
]
]]
);
// Sorting on retrieval.
$collection->aggregate([
[ '$unwind' => '$ordens' ],
[ '$sort' => [
'$ordens.dtOrdem' => -1
]],
[ '$group' => [
'_id' => '$_id',
'ordens' => [
'$push' => '$ordens'
]
]]
]);
Here is my aggregation code for mongodb collection :
$mongo_db_name = "db_".$tenant_id;
$con = new MongoClient($this->config->item('mongo_instance'));
$db = $con->$mongo_db_name;
$collection = $db->$module;
$options = array('cursor' => array("batchSize" => 4 ));
$pipeline=array(
array('$match'=>array('deleted'=>'0')),
array('$group'=>array('_id' => array('id'=>'$id', 'accountnm' => '$account_name', 'legal___corporate_name_cstm' => '$legal___corporate_name_cstm', 'funded_by_cstm' => '$funded_by_cstm', 'approval_amount_cstm' => '$approval_amount_cstm', 'payback_amount_cstm' => '$payback_amount_cstm', 'factor_rate_cstm' => '$factor_rate_cstm', 'daily_ach_amount__1_cstm' => '$daily_ach_amount__1_cstm', 'total_commission_owed_cstm' => '$total_commission_owed_cstm', 'total_commission_paid_cstm' => '$total_commission_paid_cstm', 'upfront_amount_due_cstm' => '$upfront_amount_due_cstm', 'upfront_amount_due_date_cstm' => '$upfront_amount_due_date_cstm', 'date_modified' => '$date_modified'))),
);
$data = $collection->aggregate($pipeline,$options);
var_dump($data);
I am getting this error with the above code
array (size=4) 'ok' => float 0 'errmsg' => string 'Each element of
the 'pipeline' array must be an object' (length=54) 'code' => int 14
'codeName' => string 'TypeMismatch' (length=12)
If I try to encode the pipeline using json_encode the result is 'null'.
If I use find(array('deleted'=>'0')); it returns all documents as expected.
Can anyone help me about where I am going wrong or whatmust I do to resolve this ?
aggregation pipeline takes objects in array so your pipeline should be like
[
{$match:{}};
{$group:{}};
{$somepipelinestageOperator:{}}
]
but if you have pipeline with array as tages and not object you will get above error(each element of pipeline must be an array..).
[
[$match:{}],
[$group:{}],
[$somepipelinestageOperator:{}]
]
in your code there are array for match and group so its failing.
$pipeline=array(
array('$match'=>array('deleted'=>'0')),
array('$group'=>array('_id' => array('id'=>'$id', 'accountnm' => '$account_name', 'legal___corporate_name_cstm' => '$legal___corporate_name_cstm', 'funded_by_cstm' => '$funded_by_cstm', 'approval_amount_cstm' => '$approval_amount_cstm', 'payback_amount_cstm' => '$payback_amount_cstm', 'factor_rate_cstm' => '$factor_rate_cstm', 'daily_ach_amount__1_cstm' => '$daily_ach_amount__1_cstm', 'total_commission_owed_cstm' => '$total_commission_owed_cstm', 'total_commission_paid_cstm' => '$total_commission_paid_cstm', 'upfront_amount_due_cstm' => '$upfront_amount_due_cstm', 'upfront_amount_due_date_cstm' => '$upfront_amount_due_date_cstm', 'date_modified' => '$date_modified'))),
);
you can print your pipeline and see that your pipeline stages are having array and not object.
I have an array based MySql database.
This is the array.
[
0 => [
'id' => '1997'
'lokasi_terakhir' => 'YA4121'
]
1 => [
'id' => '1998'
'lokasi_terakhir' => 'PL2115'
]
2 => [
'id' => '1999'
'lokasi_terakhir' => 'PL4111'
]
]
How can I get the element lokasi_terakhir that grouped by the first character ? What the best way ?
This is the goal :
[
"Y" => 1,
"P" => 2
]
Please advise
Here are two refined methods. Which one you choose will come down to your personal preference (you won't find better methods).
In the first, I am iterating the array, declaring the first character of the lokasi_terakhir value as the key in the $result declaration. If the key doesn't yet exist in the output array then it must be declared / set to 1. After it has been instantiated, it can then be incremented -- I am using "pre-incrementation".
The second method first maps a new array using the first character of the lokasi_terakhir value from each subarray, then counts each occurrence of each letter.
(Demonstrations Link)
Method #1: (foreach)
foreach($array as $item){
if(!isset($result[$item['lokasi_terakhir'][0]])){
$result[$item['lokasi_terakhir'][0]]=1; // instantiate
}else{
++$result[$item['lokasi_terakhir'][0]]; // increment
}
}
var_export($result);
Method #2: (functional)
var_export(array_count_values(array_map(function($a){return $a['lokasi_terakhir'][0];},$array)));
// generate array of single-character elements, then count occurrences
Output: (from either)
array (
'Y' => 1,
'P' => 2,
)
You can group those items like this:
$array = [
0 => [
'id' => '1997',
'lokasi_terakhir' => 'YA4121'
],
1 => [
'id' => '1998',
'lokasi_terakhir' => 'PL2115'
],
2 => [
'id' => '1999',
'lokasi_terakhir' => 'PL4111'
]
];
$result = array();
foreach($array as $item) {
$char = substr($item['lokasi_terakhir'], 0, 1);
if(!isset($result[$char])) {
$result[$char] = array();
}
$result[$char][] = $item;
}
<?php
$array=[
0 => [
'id' => '1997',
'lokasi_terakhir' => 'YA4121'
],
1 => [
'id' => '1998',
'lokasi_terakhir' => 'PL2115'
],
2 => [
'id' => '1999',
'lokasi_terakhir' => 'PL4111'
]
];
foreach($array as $row){
$newArray[]=$row['lokasi_terakhir'][0];
}
print_r(array_flip(array_unique($newArray)));
this code gets the first letter of the fields lokasi_terakhir , get the unique values to avoid duplicates and just flips the array to get the outcome you want.
The output is this :
Array ( [Y] => 0 [P] => 1 )
Im retrieving data from a mysql database like following Array:
$data = [
0 => [
'id' => 1,
'Benutzer' => 'foo',
'Passwort' => '123456',
'Adresse' => [
'Strasse' => 'bla', 'Ort' => 'blubb'
],
'Kommentare' => [
0 => ['Titel' => 'bar', 'Text' => 'This is great dude!'],
1 => ['Titel' => 'baz', 'Text' => 'Wow, awesome!']
]
],
]
Data like this shall be stored in a mongo database and therefore i want to replace the keynames with translated strings that come from a config- or languagefile ('Benutzer' -> 'username').
Do i really have to iterate over the array and replace the keys or is the a better way to achieve that?
If you don't want to iterate over the array then you can change the column name in the query itself using select() function.
Considering your model name is Client then your query will be:
Client::select('Benutzer as username', '...') // you can use `trnas()` function here also
->get()