how to use mysql_num with somfony2 - php

I know its a very silly question but having issues with symfony and doctrine createQuery..
I need an array result like this way to feed my chart app:
array (size=6)
0 =>
array (size=2)
0 => string 'Firefox' (length=7)
1 => float 45
1 =>
array (size=2)
0 => string 'IE' (length=2)
1 => float 26.8
2 =>
array (size=2)
0 => string 'Chrome' (length=6)
1 => float 12.8
and im using this query:
$query = $this->getEntityManager()->createQuery(
'SELECT c.status, COUNT (c.status) FROM MyAppBundle:Content c GROUP BY c.status '
);
$results = $query->getArrayResult();
but its returning like this:
array (size=4)
0 =>
array (size=2)
'status' => string 'Converted' (length=9)
1 => string '4359' (length=4)
1 =>
array (size=2)
'status' => string 'Received Q' (length=10)
1 => string '277' (length=3)
2 =>
array (size=2)
'status' => string 'Signed' (length=6)
1 => string '1' (length=1)
3 =>
array (size=2)
'status' => string 'Uploaded' (length=8)
1 => string '1' (length=1)
Now in regular php and mysql:
while($row = mysql_fetch_array($query,MYSQL_NUM)){
$results[] = $row ;
}
can return the array result as expected but my q is how can i do it with symfony2 and doctrine way?

You can give your arrays numeric indexes by using array_values
foreach($results as &$result) {
$result = array_values($result);
}

Related

PHP array index to array associative

I got a array of array in PHP that look like this :
array (size=3)
0 =>
array (size=3)
0 => string 'abc' (length=3)
1 => string 'def' (length=3)
2 => string 'ghi' (length=3)
1 =>
array (size=3)
0 => string '01234' (length=5)
1 => string '01234' (length=5)
2 => string '01234' (length=5)
2 =>
array (size=3)
0 => string '98765' (length=5)
1 => string '98765' (length=5)
2 => string '98765' (length=5)
Now I want the first array to be the key of a assosiative array for the rest of the parent array, or kind :
array (size=2)
0 =>
array (size=3)
'abc' => string '01234' (length=5)
'def' => string '01234' (length=5)
'ghi' => string '01234' (length=5)
1 =>
array (size=3)
'abc' => string '98765' (length=5)
'def' => string '98765' (length=5)
'ghi' => string '98765' (length=5)
EDIT: But I can only get the first array like this to define the header :
$header = reset($tabOfTabs);
You can try this -
$indexes = array_shift($your_array); // pop out the first array to set the indexes
foreach($your_array as $key => $array) {
$your_array[$key] = array_combine($indexes, $array); // combine the keys & sub-arrays
}
Demo

how to use array_map/array_walk function for multidimensional array

I have the following multidimensional array:
array (size=2)
10 =>
array (size=7)
1 =>
array (size=4)
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)
2 =>
array (size=4)
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
3 =>
array (size=4)
0 => string '10view3' (length=7)
1 => string '10ins3' (length=6)
2 => string '10edit3' (length=7)
3 => string '10del3' (length=6)
11 =>
array (size=7)
1 =>
array (size=4)
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
2 =>
array (size=4)
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
3 =>
array (size=4)
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
I want to convert it to the array like:
1 => array(10 => array(
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
),
2 => array(
10 => array(
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
),
11 => array(
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
)
),
3 => array(
10 => array(
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
)
please help me i confuse to solve this problem thanks in advance.
It's quite obvious for me how to implement this using foreach loop, but I wonder if it's possible to do this with PHP array functions like array_map or array_walk. Please use PHP 5.3 for the callback function. Thank you!
Using array map you can achieve like this
<?php
function merge_array($n, $m)
{
$data = array();
$data[10] = $n;
$data[11] = $m;
return $data;
}
$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);
Using for loop it is very simple you can achieve like this
$count_10 = count($array[10]);
$count_11 = count($array[11]);
$count = ($count_10>$count_11)?$count_10:$count_11;
$data = array();
for($i=0; $i<$count; $i++)
{
if(isset($array[10][$i]))
{
$data[$i][] = $array[10][$i];
}
if(isset($array[11][$i]))
{
$data[$i][] = $array[11][$i];
}
}

Php Array_Search issue

I've been trying to work with the array_search function.
This is my array :
array (size=23)
0 =>
array (size=3)
0 => string '15' (length=2)
1 => float 115.08386533184
2 => string '2014-02-06 21:00:00' (length=19)
1 =>
array (size=3)
0 => string '81' (length=2)
1 => float 100.41587590619
2 => string '2014-03-28 00:00:00' (length=19)
2 =>
array (size=3)
0 => string '65' (length=2)
1 => float 99.096448338334
2 => string '2014-02-08 21:00:00' (length=19)
3 =>
array (size=3)
0 => string '53' (length=2)
1 => float 98.752479251378
2 => string '2014-03-05 12:00:00' (length=19)
4 =>
array (size=3)
0 => string '24' (length=2)
1 => float 98.303557178126
2 => string '2014-02-07 21:00:00' (length=19)
5 =>
array (size=3)
0 => string '23' (length=2)
1 => float 98.270536817788
2 => string '2014-02-08 21:30:00' (length=19)
6 =>
array (size=3)
0 => string '37' (length=2)
1 => float 98.139812350661
2 => string '2014-02-06 23:00:00' (length=19)
7 =>
array (size=3)
0 => string '13' (length=2)
1 => float 97.810954038756
2 => string '2014-02-09 19:45:00' (length=19)
8 =>
array (size=3)
0 => string '22' (length=2)
1 => float 95.975221202728
2 => string '2014-02-11 20:30:00' (length=19)
9 =>
array (size=3)
0 => string '66' (length=2)
1 => float 94.115075316114
2 => string '2014-02-04 19:00:00' (length=19)
10 =>
array (size=3)
0 => string '52' (length=2)
1 => float 93.957291067159
2 => string '2014-03-15 00:00:00' (length=19)
11 =>
array (size=3)
0 => string '12' (length=2)
1 => float 89.188128768086
2 => string '2014-05-17 10:00:00' (length=19)
12 =>
array (size=3)
0 => string '51' (length=2)
1 => float 88.381986169995
2 => string '2014-03-27 12:00:00' (length=19)
13 =>
array (size=3)
0 => string '98' (length=2)
1 => float 85.240471279545
2 => string '2014-02-17 18:00:00' (length=19)
14 =>
array (size=3)
0 => string '92' (length=2)
1 => float 82.721601210972
2 => string '2014-02-06 20:15:00' (length=19)
15 =>
array (size=3)
0 => string '82' (length=2)
1 => float 82.473535719129
2 => string '2014-02-27 17:00:00' (length=19)
16 =>
array (size=3)
0 => string '121' (length=3)
1 => float 56.833974620724
2 => string '2014-02-07 22:30:00' (length=19)
17 =>
array (size=3)
0 => string '120' (length=3)
1 => float 36.269423317467
2 => string '2014-02-06 21:30:00' (length=19)
18 =>
array (size=3)
0 => string '83' (length=2)
1 => float 35.863780709688
2 => string '2014-02-21 07:00:00' (length=19)
19 =>
array (size=3)
0 => string '35' (length=2)
1 => float 24.322967350005
2 => string '2014-02-07 21:30:00' (length=19)
20 =>
array (size=3)
0 => string '6' (length=1)
1 => float 19.71405484708
2 => string '2014-02-14 22:00:00' (length=19)
21 =>
array (size=3)
0 => string '45' (length=2)
1 => float 19.142717452481
2 => string '2014-03-15 00:00:00' (length=19)
22 =>
array (size=3)
0 => string '117' (length=3)
1 => float 17.641278106673
2 => string '2014-02-18 17:30:00' (length=19)
Now - The thing is I'm using array_search in order to get the key of a specific ID (Note : The ID is the first value in each array). The thing is, it doesn't work, probably because it can't get the key of the array itself.
Let me show you an example : ($array = The array above)
$r = array_search(53, array_keys($array));
I want $r to be equal to 3, because thats they array's key position.
Any idea how I can do it?
$r = array_search('53', array_map(function($x) { return $x[0]; }, $array));
array_map will construct an array of element 0 of each element of the array. Then you search for 53 in this, and $r will be its position.
Or you could simply write a loop:
foreach ($array as $i => $el) {
if ($el[0] == '53') {
$r = $i;
$result = $el;
break;
}
}
I searched SO and found a similar topic: PHP Multidimensional Array - Search for value and get the sub-array.
I slightly modified one of the solutions provided above.
$id=53;
$r=null;
$data=null;
foreach ($array as $idx=>$subArray) {
if ( $subArray[0] == $id ) {
$r=$idx;
$target=$subArray;
break;
}
}
use array_map function and array_filter function.
function find($a, $id) {
return ($a[0] == $id) ? $a : false;
}
function filter($a) {
return $a;
}
$r = array_filter(array_map("find", $array, array(53)), "filter");
A better call than array_map() to isolate the first column of values is: array_column(). This is the task that the function was created for.
Use this implementation:
$r = array_search('53', array_column($array,0));
Done.

Tri-mensional array looping

I'm really stuck at a certain point in my project. I'm trying to convert a two-dimensional array into a three-dimensional array. Currently I´m at this point:
array (size=4)
0 =>
array (size=4)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
1 =>
array (size=4)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
2 =>
array (size=4)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
3 =>
array (size=4)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
What I would like to do is make a new array with key values of array[ ][3] (I hope this makes any sense) so something like this:
array (size=2)
0 => array (size=2)
0 => array (size=2)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => array (size=2)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
Right now this is the best way I can explain it, I'm not sure if I make any sense or if this is even possible, but if anyone could shed a light on this it would be much appreciated!
Thanks in advance,
Thomas
$array = array();
$array[] = array('foo','foo','foo',1);
$array[] = array('a','a','a',1);
$array[] = array('b','a','a',0);
$array[] = array('c','a','a',0);
$new_array =group_by_sub_array_key($array,3);
print_r($new_array);
function group_by_sub_array_key($array,$kn){
$new_array = array();
foreach($array AS $v){
$key=$v[$kn];
if(!array_key_exists($v[$kn],$new_array)){
$new_array[$key]=array();
}
unset($v[$kn]);
$new_array[$key][] = $v;
}
return $new_array;
}

Array 1st value as a key on new array and second as its values

I have a array:
$array1 = array
0 =>
array
0 => string 'biodata' (length=7)
1 => string 'family_name' (length=11)
1 =>
array
0 => string 'biodata' (length=7)
1 => string 'first_name' (length=10)
2 =>
array
0 => string 'biodata_education' (length=17)
1 => string 'subject' (length=7)
3 =>
array
0 => string 'biodata_education' (length=20)
1 => string 'year' (length=5)
which need to converted like:
array
biodata =>
array
0 => string 'family_name' (length=7)
1 => string 'first_name' (length=11)
biodata_education =>
array
0 => string 'subject' (length=7)
1 => string 'year' (length=10)
as it can be done by simple iteration, I tried this one and done.
foreach($array1 as $tbl):
$table[$tbl[0]][] = $tbl[1];
endforeach;
<?php
//map the array using a foreach loop
foreach($array1 as $tbl)
{
$table[ $tbl[0] ][] = $tbl[1];
}

Categories