I'm having problem with find query in php-monogodb. find() without conditions works fine but when using condition inside find() method it won't give any results. How to solve this issue?
$connection = new Mongo();
$db = $connection->selectDB('db1');
$collection = $db->selectCollection('customers');
$cursor = $collection->find(array('CUSTOMER_ID' => $id));
$num_docs = $cursor->count();
if($num_docs > 0)
{
foreach($cursor as $obj)
{
echo 'Customer-Id: '.$obj['CUSTOMER_ID']."\n";
echo 'Customer Name: '.$obj['CUST_FIRST_NAME']." ".$obj['CUST_LAST_NAME']."\n";
echo 'Customer Email: '.$obj['CUST_EMAIL']."\n";
echo "\n\n\n";
}
}
After var_dump($cursor->explain()); it prints:
array (size=3)
'queryPlanner' =>
array (size=6)
'plannerVersion' => int 1
'namespace' => string 'db1.customers' (length=13)
'indexFilterSet' => boolean false
'parsedQuery' =>
array (size=1)
'CUSTOMER_ID' =>
array (size=1)
...
'winningPlan' =>
array (size=3)
'stage' => string 'COLLSCAN' (length=8)
'filter' =>
array (size=1)
...
'direction' => string 'forward' (length=7)
'rejectedPlans' =>
array (size=0)
empty
'executionStats' =>
array (size=7)
'executionSuccess' => boolean true
'nReturned' => int 0
'executionTimeMillis' => int 1
'totalKeysExamined' => int 0
'totalDocsExamined' => int 325
'executionStages' =>
array (size=14)
'stage' => string 'COLLSCAN' (length=8)
'filter' =>
array (size=1)
...
'nReturned' => int 0
'executionTimeMillisEstimate' => int 0
'works' => int 327
'advanced' => int 0
'needTime' => int 326
'needYield' => int 0
'saveState' => int 2
'restoreState' => int 2
'isEOF' => int 1
'invalidates' => int 0
'direction' => string 'forward' (length=7)
'docsExamined' => int 325
'allPlansExecution' =>
array (size=0)
empty
'serverInfo' =>
array (size=4)
'host' => string 'deadpool' (length=8)
'port' => int 27017
'version' => string '3.2.4' (length=5)
'gitVersion' => string 'e2ee9ffcf9f5a94fad76802e28cc978718bb7a30' (length=40)
Can you explain what is going wrong?
$cursor = $collection->find(array('CUSTOMER_ID' => $id)); where are you getting the id value from.
and try not using id as sometimes it has issues finding the value esp if $id is used elsewhere.
Related
I need to replace the keys of my array with keys within the same array in PHP.
My array looks like this at the moment :
array (size=7)
0 =>
array (size=2)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
1 =>
array (size=2)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
2 =>
array (size=2)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
3 =>
array (size=2)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
4 =>
array (size=2)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
5 =>
array (size=2)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
6 =>
array (size=2)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
I want it to look like this :
array (size=7)
978 =>
array (size=1)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
979 =>
array (size=1)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
980 =>
array (size=1)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
981 =>
array (size=1)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
983 =>
array (size=1)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
984 =>
array (size=1)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
991 =>
array (size=1)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
Is there any function in PHP that could help without looping into the whole array ?
Thank you !
Just use array_column() with the third parameter as the column you want as the index...
$indexedArray = array_column($input,null,"id");
As the title says I get array looking something like this:
array (size=376)
0 =>
array (size=3)
'source' => int 0
'target' => int 47
'officers' =>
array (size=1)
0 => string 'PARKER, Thomas, Sir' (length=19)
1 =>
array (size=3)
'source' => int 0
'target' => int 104
'officers' =>
array (size=1)
0 => string 'STEVENS, Anne' (length=13)
2 =>
array (size=3)
'source' => int 0
'target' => int 187
'officers' =>
array (size=1)
0 => string 'PARKER, Thomas, Sir' (length=19)
3 =>
array (size=3)
'source' => int 0
'target' => int 229
'officers' =>
array (size=1)
0 => string 'GROTE, Byron' (length=12)
4 =>
array (size=3)
'source' => int 0
'target' => int 244
'officers' =>
array (size=1)
0 => string 'GROTE, Byron' (length=12)
5 =>
array (size=3)
'source' => int 1
'target' => int 60
'officers' =>
array (size=1)
0 => string 'BASON, John' (length=11)
6 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'HAUSER, Wolfhart, Dr' (length=20)
If source and target are the same I want to create 1 value in this array for example
Insteed of this:
5 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'BASON, John' (length=11)
6 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'HAUSER, Wolfhart, Dr' (length=20)
I would want to get
5 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'BASON, John' (length=11)
1 => string 'HAUSER, Wolfhart, Dr' (length=20)
My idea to achieve this:
for ($i = 0; $i < count($edges); $i++) {
for ($j = $i + 1; $j < count($edges); $j++) {
if($edges[$i]['source']==$edges[$j]['source']&&$edges[$i]['target']==$edges[$j]['target']){
foreach( $edges[$j]['officers'] as $officer){
array_push($edges[$i]['officers'], $officer);
}
array_splice($edges, $j,1);
}
}
}
It seems to be merging but only 2 values and it isn't deleting old values so:
I got something like :
66 =>
array (size=3)
'source' => int 24
'target' => int 103
'officers' =>
array (size=2)
0 => string 'GILAURI, Irakli' (length=15)
1 => string 'JANIN, Neil' (length=11)
67 =>
array (size=3)
'source' => int 24
'target' => int 103
'officers' =>
array (size=1)
0 => string 'MORRISON, David' (length=15)
So it merget Gilauri and Janin but didn't add Morrison, David to officers array and also Gilauri, Irakli and Janin, Neil have separate row in this array which shouldn't be added.
Note that array_splice rekeys the array each time it is executed. From the documentation:
Note that numeric keys in input are not preserved.
I.e. array_splice($array, 1, 1) on $array = [0 => 1, 1 => 2, 2 => 3] will result in [0 => 1, 1 => 3].
Thus when you are iterating with a for loop, your index will point one entry further than before for each removed array element.
To remove by index without rekeying, use unset($edges[$j]) instead of the array_splice().
Alternatively use a foreach loop (by reference to operate on the actual loop and not on a copy):
foreach ($edges as $i => &$edge) {
for ($j = $i + 1; $j < count($edges); $j++) {
if ($edge['source'] == $edges[$j]['source'] && $edge['target'] == $edges[$j]['target']) {
foreach ($edges[$j]['officers'] as $officer) {
array_push($edge['officers'], $officer);
}
array_splice($edges, $j, 1);
}
}
}
But I recommend the unset() alternative as it is much faster (array_splice always recreates the array completely).
This is similar to other problems where you can solve this by using a key from the sub-arrays as a key in your merged result. It's just a little different because you need to use more than one key to determine which records should be merged.
I would suggest doing it by just concatenating the two keys.
foreach ($your_array as $item) {
$k = "$item[source].$item[target]";
if (isset($merged[$k])) {
$merged[$k]['officers'] = array_merge($merged[$key]['officers'], $item['officers']);
} else {
$merged[$k] = $item;
}
}
And if you don't like the composite keys, you can do array_values($merged) afterward to get back to plain numeric keys.
I have the following arrays:
array (size=3)
0 =>
array (size=3)
'id' => int 18
'class' => string 'VIP' (length=3)
'fee' => float 20
1 =>
array (size=3)
'id' => int 19
'class' => string 'VVIP' (length=4)
'fee' => float 50
2 =>
array (size=3)
'id' => int 20
'class' => string 'STANDARD' (length=8)
'fee' => float 5
array (size=3)
0 =>
array (size=3)
'id' => int 19
'class' => string 'VVIP' (length=4)
'fee' => int 50
1 =>
array (size=3)
'id' => int 18
'class' => string 'VIP' (length=3)
'fee' => int 20
2 =>
array (size=3)
'id' => int 20
'class' => string 'STANDARD' (length=8)
'fee' => int 5
Now i am trying to sort them both using array_sort in ascending order using the id.:
$array_1 = array_sort($array_1, 'id', SORT_ASC);
$array_2 = array_sort($array_2, 'id', SORT_ASC);
However i keep getting the following error:
Argument 2 passed to array_sort() must be callable, string given
The second parameter of array_sort should be a closure, not a string:
$array_1 = array_sort( $array_1, function($value){
return $value['id'];
});
I have the following repository:
class CustomerRepository extends EntityRepository
{
public function searchCustomer($criteria)
{
$q = $this->createQueryBuilder('c');
$q->join('TeamERPCustomerBundle:Company', 'o',
'WITH', 'c.company = o.id');
if (isset($criteria) and $criteria!=""){
$q->orWhere(sprintf("c.customer_name like '%s'", '%'.$criteria.'%'));
(...)
}
$q = $q->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
return $q;
}
}
I call it from the controller as follows:
$result = $this->getDoctrine()->getManager()
->getRepository('TeamERPCustomerBundle:Customer')
->searchCustomer($customerInfo);
It returns an array of elements as follows:
array (size=20)
0 =>
array (size=9)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
1 =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
2 =>
array (size=9)
'id' => int 2
'customer_name' => string 'Kay' (length=3)
'address' => null
'postal_address' => null
'city_town_village' => null
'e_mail' => null
'land_line' => null
'cell_phone' => null
'fax' => null
3 =>
array (size=2)
'id' => int 3
'company_name' => string 'DTC' (length=3)
(...)
It gives one element of the array per object, but what I want it the classic mysql join; as result an array of elements of the same type. Something like mergin elements 1 and 2 together.
The other problem with this query is that is does not work like that classic join ether, it gives all the customers, but only the companies that are not repeated.
Can anyone help me to get a uniform array of elements?
Edit: I managed to find a solution:
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT c, i
FROM TeamERPCustomerBundle:Customer c
JOIN c.company i');
//$query->setParameter('id', '1'); With this is can add as many parameter as I need
return $query->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
This returned something like this:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
As you might have already appreciated this array is different from what I was getting before. so now I just have to go through the array and change it to what ever I want. for instance:
foreach ($result as $key => $value){
$result[$key]['company'] = $value['company']['company_name'];
}
Now we are talking. This Is kind of what I wanted:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' => string 'Debswana' (length=8)
I have just implemented https://github.com/thujohn/twitter-l4 for Laravel 4, it basically pulls in an array
of tweets from a search of a hashtag.
It seems like its working, I think there are some things that are not working though.
I get a blank screen with no errors which means a positive sign.
Here is my code.
PHP:
$tweets = Twitter::getSearch(array('q' => 'secretsocial', 'count' => 100, 'format' => 'array'));
var_dump($tweets);
This code basically gives me an array of json:
array (size=2)
'statuses' =>
array (size=20)
0 =>
array (size=24)
'metadata' =>
array (size=2)
'result_type' => string 'recent' (length=6)
'iso_language_code' => string 'en' (length=2)
'created_at' => string 'Fri May 16 14:28:14 +0000 2014' (length=30)
'id' => int 467310562603331586
'id_str' => string '467310562603331586' (length=18)
'text' => string 'On that #merch #grind. #spotify #swag just got shipped in. #secretsocial #free #leeds #leedsuniā¦ http://t.co/67phqjeg5W' (length=121)
'source' => string 'Instagram' (length=59)
'truncated' => boolean false
'in_reply_to_status_id' => null
'in_reply_to_status_id_str' => null
'in_reply_to_user_id' => null
'in_reply_to_user_id_str' => null
'in_reply_to_screen_name' => null
'user' =>
array (size=40)
'id' => int 167993141
'id_str' => string '167993141' (length=9)
'name' => string 'Maral Erol' (length=10)
'screen_name' => string 'liaaca' (length=6)
'location' => string 'Leeds / San Diego' (length=17)
'description' => string 'Music/Culture/Entertainment Enthusiast.' (length=39)
'url' => string 'http://t.co/FL3uuA6QcN' (length=22)
'entities' =>
array (size=2)
'url' =>
array (size=1)
'urls' =>
array (size=1)
0 =>
array (size=4)
'url' => string 'http://t.co/FL3uuA6QcN' (length=22)
'expanded_url' => string 'http://linkd.in/R70tjB' (length=22)
'display_url' => string 'linkd.in/R70tjB' (length=15)
'indices' =>
array (size=2)
0 => int 0
1 => int 22
'description' =>
array (size=1)
'urls' =>
array (size=0)
empty
So from that I wrote this:
if(isset($tweets->statuses) && is_array($tweets->statuses)) {
if(count($tweets->statuses)) {
foreach($tweets->statuses as $tweet) {
echo $tweet->text;
}
}
else {
echo 'The result is empty';
}
}
I get no errors on the page. Can anyone point me in the right direction please?
Cheers
Since each $tweet is an array so you should use $tweet['text'] instead of $tweet->text
foreach($tweets->statuses as $tweet) {
echo $tweet['text'];
}
Also $tweets->statuses should be $tweets['statuses'].