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)
Related
I'm trying to load a word (docx) document from PHP and it "kind of works"...
This code would produce an array from the document.xml part of the word-document:
$result = file_get_contents( 'zip://test.docx#word/document.xml' );
$content_arr = simplexml_load_string($result,null, 0, 'w', true);
BUT the nodes would rearranged when getting into the array with simplexml_load_string (and I don't want this rearrangement), where each node-type seems to be sorted out:
from:
p: some text
tbl: table
p: some text
p: some text
p: some text
to:
p: some text
p: some text
p: some text
p: some text
tbl: table
I would like something like this (that actually reflects the original order of the xml-nodes):
item0: p: some text
item1: tbl: table
item2: p: some text
item3: p: some text
item4: p: some text
Is this possible or is simplexml_load_string simply wrong function to use for my purpose?
It is. SimpleXML does not work well with mixed child nodes (text and element siblings). So it does not work with complex XML formats like OOXML.
Reading data from OOXML with DOM+Xpath is actually quite easy:
$xml = <<<"XML"
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>one</w:p>
<w:p>two</w:p>
<w:tbl>three</w:tbl>
<w:p>four</w:p>
</w:body>
</w:document>
XML;
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
// register a prefix for the used namespace
$xpath->registerNamespace(
'word', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
// iterate any p and tbl element nodes
foreach ($xpath->evaluate('//word:p|//word:tbl') as $index => $node) {
echo "item{$index} {$node->localName}: {$node->textContent}\n";
}
Output:
item0 p: one
item1 p: two
item2 tbl: three
item3 p: four
I found a function in php that would really make my day (and I hope it will make yours if you have the same issue that I had). Actually I think it would be much easier to implement now (and debug) because the way the xml is converted into the array(s).
The function is xml_parse_into_struct: https://www.php.net/manual/en/function.xml-parse-into-struct.php
$result = file_get_contents( 'zip://' . $file . '#word/document.xml' );
$p = xml_parser_create();
xml_parse_into_struct($p, $result, $content_arr, $index);
xml_parser_free($p);
var_dump($content_arr);
The dump looks like:
array (size=1087)
0 =>
array (size=4)
'tag' => string 'W:DOCUMENT' (length=10)
'type' => string 'open' (length=4)
'level' => int 1
'attributes' =>
array (size=20)
'XMLNS:WPC' => string '{value}' (length=66)
'XMLNS:CX' => string '{value}' (length=66)
'XMLNS:CX1' => string '{value}' (length=60)
'XMLNS:MC' => string '{value}' (length=59)
'XMLNS:O' => string '{value}' (length=39)
'XMLNS:R' => string '{value}' (length=67)
'XMLNS:M' => string '{value}' (length=58)
'XMLNS:V' => string '{value}' (length=29)
'XMLNS:WP14' => string '{value}' (length=67)
'XMLNS:WP' => string '{value}' (length=70)
'XMLNS:W10' => string '{value}' (length=37)
'XMLNS:W' => string '{value}' (length=60)
'XMLNS:W14' => string '{value}' (length=52)
'XMLNS:W15' => string '{value}' (length=52
'XMLNS:W16SE' => string '{value}' (length=58)
'XMLNS:WPG' => string '{value}' (length=65)
'XMLNS:WPI' => string '{value}' (length=63)
'XMLNS:WNE' => string '{value}' (length=52)
'XMLNS:WPS' => string '{value}' (length=65)
'MC:IGNORABLE' => string '{value}' (length=18)
1 =>
array (size=3)
'tag' => string 'W:BODY' (length=6)
'type' => string 'open' (length=4)
'level' => int 2
2 =>
array (size=4)
'tag' => string 'W:P' (length=3)
'type' => string 'open' (length=4)
'level' => int 3
'attributes' =>
array (size=3)
'W:RSIDR' => string '00DB5D45' (length=8)
'W:RSIDRDEFAULT' => string '00DB5D45' (length=8)
'W:RSIDP' => string '00DB5D45' (length=8)
3 =>
array (size=3)
'tag' => string 'W:PPR' (length=5)
'type' => string 'open' (length=4)
'level' => int 4
4 =>
array (size=4)
'tag' => string 'W:PSTYLE' (length=8)
'type' => string 'complete' (length=8)
'level' => int 5
'attributes' =>
array (size=1)
'W:VAL' => string 'Rubrik1' (length=7)
5 =>
array (size=4)
'tag' => string 'W:JC' (length=4)
'type' => string 'complete' (length=8)
'level' => int 5
'attributes' =>
array (size=1)
'W:VAL' => string 'center' (length=6)
6 =>
array (size=3)
'tag' => string 'W:PPR' (length=5)
'type' => string 'close' (length=5)
'level' => int 4
7 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'open' (length=4)
'level' => int 4
8 =>
array (size=5)
'tag' => string 'W:T' (length=3)
'type' => string 'complete' (length=8)
'level' => int 5
'attributes' =>
array (size=1)
'XML:SPACE' => string 'preserve' (length=8)
'value' => string 'Skövde Konståkningsklubb inbjuder till ' (length=41)
9 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'close' (length=5)
'level' => int 4
10 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'open' (length=4)
'level' => int 4
11 =>
array (size=3)
'tag' => string 'W:BR' (length=4)
'type' => string 'complete' (length=8)
'level' => int 5
12 =>
array (size=4)
'tag' => string 'W:T' (length=3)
'type' => string 'complete' (length=8)
'level' => int 5
'value' => string 'dagläger i konståkning' (length=24)
13 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'close' (length=5)
'level' => int 4
14 =>
array (size=3)
'tag' => string 'W:P' (length=3)
'type' => string 'close' (length=5)
'level' => int 3
15 =>
array (size=4)
'tag' => string 'W:P' (length=3)
'type' => string 'open' (length=4)
'level' => int 3
'attributes' =>
array (size=3)
'W:RSIDR' => string '00DB5D45' (length=8)
'W:RSIDRDEFAULT' => string '00A14053' (length=8)
'W:RSIDP' => string '00DB5D45' (length=8)
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.
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 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'].
I have this multidimensional array
I am wondered how can i sort this array again so i can use it in for loop.
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
3 =>
array (size=1)
3 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
How can I reindex this array to become
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
1 =>
array (size=1)
1 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
I tried array_shift and array_chunk but nothing works !!!
Please help, thank you all :)
Use array_multisort to sort multi-dimensional arrays or to sort an array using multiple keys.
I think this should do it but it's be a lot cleaner if you didn't have the extra level off the array.
$new_array = array();
$index = 0;
foreach($array as $i1 => $a1){
foreach($a1 as $i2 => $a2){
$new_array[$index][$index] = $a2;
}
$index++;
}
You can use 'array_values' for re-indexing which start index from 0. As per your requirement inner array are not starting from 0 but are same as parent array index. You have to use foreach for that. To index the way you want can be done like this:
$info = array(
0 => array (
0 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
2 => array (
2 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
3 => array (
3 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
)
);
var_dump($info); // original index
$info = array_values($info);
foreach ($info as $key => $value) {
$temp = array();
foreach($value as $k => $v) {
$temp[$key] = $v;
}
$info[$key] = $temp;
}
var_dump($info); // re-index