SQL query against an array? - php

Is there any kind of php library to execute SQL queries agains arrays?
Something like this:
$array = array(
array(
"field_a" => "first_a"
"field_b" => "something"
),
array(
"field_a" => "second_a"
"field_b" => "else"
),
array(
"field_a" => "third_a"
"field_b" => "something"
)
);
$rows = arrayQuery($array, "SELECT field_a WHERE field_b = 'something'");
/*
$rows = array(
array(
"field_a" => "first_a"
),
array(
"field_a" => "third_a"
)
);
*/
Found something similar: http://blog.phpdeveloper.org/?p=61, but that is not a query.

I believe that LINQ for PHP would do the trick. I think this is a close as it gets to 'querying arrays' in PHP.

Related

Add parameter to function php in option query mongo?

My mongoDB have field: $creatime, I want add field to function php.
How do it?
$ops = array(
array('$match' => $query),
array('$addFields' => array('date_code' => functionPHP('$create_time'))),
);
$results = $col->aggregate($ops,array('cursor' => array('batchSize' => 200)));

Merge multidimensional array in PHP

I have a multi array and I need to combine it with other. This array is made for send in XML format to SOAP, so it need to have the correct structure.
This array is like an invoice, it have "items" which i have to repeat. So, i think in make two arrays (one have always the same structure) and add the items array.
The problem is that if I use merge, I could not put the second array in the correct key. Here an example.
This is the correct array structure:
$params = array(
'authRequest' =>
array( 'token' => 'token',
'sign' => 'sign',
'cuitRepresentada' => 'CUIT' ),
'comprobanteRequest' =>
array( 'codigoTipoComprobante' => $codtipcbte,
'numeroPuntoVenta' => $ptovta,
'numeroComprobante' => $cbte,
**'arrayItems' =>
array( 'item' =>
array(
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
),
),**
'arraySubtotalesIVA' =>
array( 'subtotalIVA' =>
array(
'codigo'=> $compreqiva['codIva'],
'importe'=> $compreqiva['importe'],
),
),
),
);
So, i build the array with "arrayItems" empty
'arrayItems' => array(),
Then i build the arrayItem array:
$arrayitems =
array('arrayItems' =>
array( 'item' =>
array(
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
),
),
);
Then i use merge to join both array:
$resultado = array_merge($params['comprobanteRequest'], $arrayitems);
Works, but the first key is deleted...
'authRequest' =>
array( 'token' => 'token',
'sign' => 'sign',
'cuitRepresentada' => 'CUIT' ),
I dont know why is deleted, maybe the merge function is not the corect way...
Thanks in advance!
If in your first array, arrayItems is always empty, then you don't need a merge, just set the value :
$params['comprobanteRequest']['arrayItems'] = $arrayItems['arrayItems'];
Of course this can be simplified, since $arrayItems contains only one key, but you get the spirit.

How to use datas one by one from fetching with find('all',) in CakePHP?

I want to match course_id with $courseInfo which is fetching from $cLID.
When I change this $clID['Relationscl']['course_id'] line as 1 everything is ok, but the other way an error appears like Undefined index: Relationscl.
Here is my code:
$this->loadModel('Course');
$this->loadModel('Lecturer');
$clID = $this->Relationscl->find('all', array(
'conditions' => array(
'student_id' => $id
),
'fields' => array(
'course_id','lecturer_id'
)
));
$this->set('clIDs', $clID);
$courseInfo = $this->Course->find('first',array(
'conditions' => array(
'course_id' => $clID['Relationscl']['course_id']
),
'fields' => array(
'course_name','course_code','course_credit'
)
));
$this->set('cInfos', $courseInfo);
Correct me if I'm wrong, but the find('all') function returns and indexed array, so it should be something like
$clID = array( 0 => array('Relationscl'=>array('course_id'=>1 /*and more fields*/)),
1 => array('Relationscl'=>array('course_id'=>2 /*and more fields*/))
/*etc*/);
So clearly $clID['Relationscl'] is undefined. Try with $clID[0]['Relationscl']. Though even that seems weird, why would you do a find('all') if you only plan on using the one record, isn't find('first') better? Or set that $courseInfo definition inside a loop?

How to search in fulltext index using php in mongodb

I'm using mongodb 2.4 and added fulltext index to the "title" field in one the collection. How should I search something in that field using php?
This is the code I use right now:
$params = array(
'_id' => array(
'$gt' => (int)$gt
)
);
$r = $this->collection->find( $params )->limit($limit);
This seem to be the answer to my question:
<?php
$result = $db->command(
array(
'text' => 'bar', //this is the name of the collection where we are searching
'search' => 'hotel', //the string to search
'limit' => 5, //the number of results, by default is 1000
'project' => Array( //the fields to retrieve from db
'title' => 1
)
)
);
http://www.php.net/manual/en/mongodb.command.php#111891

Find Records in Mongo DB with PHP where ID != X

is it possible to find all records in an collection where the MongoID is not in
an provided array?
Something like this (?):
$search = array(
'_id' => array('$ne' => $ids)
'readby' => array('$ne' => $userId) // works
);
Iam using PHP with the Mongo Extension.
Use $nin instead of $ne with arrays. Something like:
$search = array(
'_id' => array('$nin' => $ids),
'readby' => array('$ne' => $userId)
);
should do what you want.

Categories