I have this code:
$data = $collection->aggregate(
array(
'$group'=> array(
'_id' => $fn,
'massi' => array(
'$max' => $dnameth_value
)
)
)
);
I execute this query and I want to obtain only MAX Value, and use this in variable.
I tried this code :
$data=$collection->aggregate(array( '$group'=> array('_id'=>$fn,'massi'=>array('$max'=>$dnameth_value))));
var_dump( $data['result'] );
$func = function($value) {
return $value->massi;};
$massi = array_map($func, $data['result']);
var_dump($massi);
Output in the image attach
When you execute the query, the return value of the aggregate() helper method is an array with two elements: ok with a value of double(1) as well as a result element containing an array of all the documents that made it through the whole pipeline.
For instance, if you want to group all the documents by the field my_key and you want to obtain the maximum dnameth_value from that group, and you execute this aggregation operation
<?php
$m = new MongoClient;
$collection = $m->test->collection;
$fn = '$my_key';
$dnameth_value = '$dnameth_value'
$data = $collection->aggregate(
array(
'$group'=> array(
'_id' => $fn,
'massi' => array(
'$max' => $dnameth_value
)
)
)
);
var_dump( $data['result'] );
?>
you should expect the result array from $data['result'] to come in the following form (for example):
…
array (
'_id' => 'value',
'massi' => double(13),
),
array (
'_id' => 'other_value',
'massi' => double(9),
),
array (
'_id' => 'another_value',
'massi' => double(4),
),
…
Note: Because the aggregation framework returns all of its results as one document over the network, the full result is limited to 16MB. There are also memory limits internally, so it is always wise to restrict the data coming through the pipeline with an operator as soon as you can.
So, with your request, you basically would want to map all the massi values from the array into another array variable using array_map():
<?php
$m = new MongoClient;
$collection = $m->test->collection;
$fn = '$my_key';
$dnameth_value = '$dnameth_value'
$data = $collection->aggregate(
array(
'$group'=> array(
'_id' => $fn,
'massi' => array(
'$max' => $dnameth_value
)
)
)
);
var_dump( $data['result'] );
$func = function($value) {
return $value['massi'];
};
$massi = array_map($func, $data['result']);
var_dump($massi);
?>
Now, suppose you are grouping all the documents in the collection then specify an _id value of null to calculate accumulated values for all the input documents as a whole. The resulting array will have one element which you can access by its index:
<?php
$m = new MongoClient;
$collection = $m->test->collection;
$fn = 'null';
$dnameth_value = '$dnameth_value'
$data = $collection->aggregate(
array(
'$group'=> array(
'_id' => $fn,
'massi' => array(
'$max' => $dnameth_value
)
)
)
);
var_dump( $data['result'] );
$massi = $data['result'][0]['massi'];
var_dump($massi);
?>
Related
Here's my code
function ...($id){
$data = array(
'name' => $this->input->post('name'),
'id' => $id
);
$data2 = array(
'inv_id' = $data['id']
);
}
I couldn't get the value of the id from the first array($data).
Is it possible to get the value of the key id from the first array($data) to the second array
while assigning array elements you must use '=>' operator'
like below: -
$data2 = array(
'inv_id' => $data['id']
);
Please use separator '=>' for the associative arrays.
Example:
$data2 = array(
'inv_id' => $data['id']
);
function ...($id){
//changes in the line below.
$inp_arr = ['name' => $this->input->post('name'),'id' => $id];
$some_other_arr = array(
'inv_id' = $inp_arr['id']
);
}
Edit 1:
//it is =>
'inv_id' => $inp_arr['id'] //pointed out in one of the answer.
I am trying to run an update on my documents, I'm using upsert true but its still overwriting?
$col = "A" . $user->agencyID;
$db = $m->rules;
$collection = $db->$col;
$validValue = $_POST['validValue'];
$id = $_POST['ruleID'];
$document = array(
'tags' => array(
$validValue
)
);
$collection->update(
array(
'_id' => new MongoId($id)
),
array('$set' => $document),
array('upsert'=>true)
);
$validValue is like - Foo Bar
The first value goes in fine but when I try adding a different value it overwrites the first one?
I managed to figure out the problem, I needed $addToSet and also needed to take the array() from arround my $validValue
Actually, use $addToSet which will not push a value into the array if it already exists. This code is untested, please change to fit your needs.
$col = "A" . $user->agencyID;
$db = $m->rules;
$collection = $db->$col;
$validValue = $_POST['validValue'];
$id = $_POST['ruleID'];
$document = array(
'tags' => array(
$validValue
)
);
$collection->update(
array(
'_id' => new MongoId($id)
),
array('$addToSet' => array('tags' => $document))
);
I am struggling to get values added into the array at specific points.
What I am looking for is to create and empty array, take the resultset from the database and depending on whether they are global or local create the PHP array listed below. The problem is that no matter what I try I can't get the array to look like below.
Either values are being replaced, or arrays being replaced or values being added into wrong places
This is my empty array and I am trying to loop each resultset into the array and then how many sold in the day.
$test = array();
foreach($globalDatabase as $value)
{
$test['global'][] = array('name'=>$value->type, 'data' => array($value->day,$value->quantity) );
}
foreach($localDatabase as $value)
{
$test['local'][] = array('name'=>$value->type, 'data' => array($value->day,$value->quantity) );
}
This is what I need it to look like so that it ends up looking like this, which is the output format I need it to be in so it is compatible with highcharts
$test = array(
'global'=>array(
array(
'name'=>'CARS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BIKES',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BOATS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
),
'local'=>array(
array(
'name'=>'CARS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BIKES',
'data' => array(
array('mon',1),
array('tue',1)
)
),
)
);
I take it that your data rows pulled from the table are 'flat', meaning each row has only a single value each for 'type', 'day', and 'quantity', with no nesting.
Going on that assumption, you may just need to group your data by common attributes similar to the following:
$databases = array('global'=>$globalDatabase, 'local'=>$localDatabase);
$workData = array('global'=>array(), 'local'=>array());
foreach($databases as $dbType=>$workDb)
{
foreach ($workDb as $value) {
if(!array_key_exists($value->type, $workData[$dbType])) {
$workData[$dbType][$value->type] = array();
}
$workData[$dbType][$value->type][] = array($value->day, $value->quantity);
}
}
$test = array();
foreach ($workData as $type=>$workRow) {
foreach ($workRow as $dataKey=>$dataRow) {
$test[$type][] = array('name'=>$dataKey, 'data' => $dataRow );
}
}
I need the pipeline to match documents where field 'modelName' is equal to 'movies' or 'tv_shows'. I tried the code below but it matches only 'tv_shows' and ignores 'movies'.
$match = array('$match' => array('modelName' => 'movies', 'modelName' => 'tv_shows'));
Whole script:
<?php
$connection = new MongoClient;
$collection = $connection -> selectDB("getglue") -> selectCollection("gg");
MongoCursor::$timeout = -1;
$match = array('$match' => array('modelName' => 'movies', 'modelName' => 'tv_shows'));
$group = array('$group' => array('_id' => '$title', 'total' => array('$sum' => 1)));
$sort = array('$sort' => array('total' => -1));
$limit = array('$limit' => 7);
$pipeline = array($match, $group, $sort, $limit);
$out = $collection -> aggregate($pipeline);
echo json_encode($out, JSON_PRETTY_PRINT);
?>
Make use of the $or operator:
$match = array('$match' =>
array('$or' => array(array("modelName" => "movies"),
array("modelName" => "tv_shows"))
)
);
An array in PHP is actually an ordered map. A map can have only one value for any key, and the last added value will override any previous values for the same key. So, in the below, tv_shows which is the last added value for the key - modelName will be associated as the key's only value. And that is why you get the results only for modelname of tv_shows.
'$match' => array('modelName' => 'movies', 'modelName' => 'tv_shows')
I'm having real problems trying to figure this one out.
I have a PHP array which looks like this:
$info = array();
$info[0] = array(
'car' => 'Audi',
'previous_car' => 'BMW'
);
$info[1] = array(
'car' => 'Audi',
'previous_car' => 'Seat'
);
$info[2] = array(
'car' => 'Audi',
'previous_carg' => 'BMW'
);
$info[3] = array(
'car' => 'BMW',
'previous_car' => 'BMW'
);
$info[4] = array(
'car' => 'Ford',
'previous_car' => 'Seat'
);
I need to do some sorting on this, so the result looks like this:
Array (
car [
'Audi' => 3,
'BMW' => 1,
'Ford' => 1
],
previous_car [
'BMW' => 3,
'Seat' => 2
]
);
I need to count distinct occurrences of a value in the same key, but the search is made upon couple of arrays. I was trying to use array_value_count(), but I doesn't work well on multidimensional arrays.
I am trying to avoid the looping, since it can be overkill if the array is large.
I will be very grateful for all the help.
If you're running PHP 5.5, you can use:
$newArray = array(
'car' => array_count_values(array_column($info, 'car')),
'previous_car' => array_count_values(array_column($info, 'previous_car'))
);
var_dump($newArray);
For versions of PHP prior to 5.5
$newArray = array(
'car' => array_count_values(
array_map(
function($value) {
return $value['car'];
},
$info
)
),
'previous_car' => array_count_values(
array_map(
function($value) {
return $value['previous_car'];
},
$info
)
)
);
var_dump($newArray);
In a more object orientated way you can solve it as follows
$values = new ArrayObject();
$iterator = new RecursiveArrayIterator($info);
iterator_apply($iterator, 'countDistinct', array($iterator, $values));
function countDistinct($iterator, $values) {
while ( $iterator -> valid() ) {
if ( $iterator -> hasChildren() ) {
countDistinct($iterator -> getChildren(), $values);
} else {
if (!$values->offsetExists($iterator->key())) {
$values->offsetSet($iterator->key(), new ArrayObject());
}
if (!$values->offsetGet($iterator->key())->offsetExists($iterator->current())) {
$values->offsetGet($iterator->key())
->offsetSet($iterator->current(), 1);
} else {
$values->offsetGet($iterator->key())
->offsetSet($iterator->current(),
$values->offsetGet($iterator->key())->offsetGet($iterator->current()) + 1);
}
}
$iterator -> next();
}
}
Sure, with this example you do not avoid the loop. But with the ArrayObject and the RecursiveArrayIterator you will have some memory and performance advantages.
The result of this will exactly match your expected result, which you can easyliy iterate with the getIterator() function of the ArrayObject.
You can write a function that will sort your data but for now check this out:
http://www.php.net/manual/en/function.array-multisort.php
Here is what might help you:
$returnArray = array('car' => NULL, 'previous_car' => NULL);
foreach($info as $newInfo) {
$returnArray['car'][] = $newInfo['car'];
$returnArray['previous_car'][] = $newInfo['previous_car'];
}
$ret['car'] = array_count_values($returnArray['car']);
$ret['previous_car'] = array_count_values($returnArray['previous_car']);
var_dump($ret);
This returns:
array (size=2)
'car' =>
array (size=3)
'Audi' => int 3
'BMW' => int 1
'Ford' => int 1
'previous_car' =>
array (size=2)
'BMW' => int 3
'Seat' => int 2