i am using mongodb and my query returns only a key value
the following is the format of the var_dump
<pre>array (size=471)
0 =>
array (size=1)
'sno' => string '162230' (length=6)
1 =>
array (size=1)
'sno' => string '165333' (length=6)
2 =>
array (size=1)
'sno' => string '181312' (length=6)
3 =>
array (size=1)
'sno' => string '181313' (length=6)
4 =>
array (size=1)
'sno' => string '181314' (length=6)
5 =>
array (size=1)
'sno' => string '181315' (length=6)
6 =>
array (size=1)
'sno' => string '181316' (length=6)
7 =>
array (size=1)
'sno' => string '181317' (length=6)
8 =>
array (size=1)
'sno' => string '181318' (length=6)
9 =>
array (size=1)
'sno' => string '181319' (length=6)
10 =>
array (size=1)
'sno' => string '181320' (length=6)</pre>
I do not want key . all i want is a array of values without a loop.
Solved it.
array_column($arr,"sno")
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");
I have an array of arrays like this one below and I want to convert it to object array.
array (size=3)
'declaration' =>
array (size=99)
'GO_IMPZONK_ID' => string '130334' (length=6)
'ID' => string '19802862' (length=8)
'CUSTE' => string '10100' (length=5)
'DCLEXP' => null
'DCL_BROKER_CODE' => string '' (length=0)
'RLCCODE' => string '' (length=0)
'items' =>
array (size=1)
0 =>
array (size=50)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TARIFYEAR' => string '85' (length=2)
'extensions' =>
array (size=6)
0 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
1 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
I did try casting it like this but it only makes Object with arrays inside.
$obj=(object)$array;
what can i do to have nesting multi level Objects from my array?
i improvised another way to do so:
$ar=[
'a'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
],
'b'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
]
];
function ToObj($data) {
if (gettype($data) == 'array')
return (object)array_map("ToObj", $data);
else
return $data;
}
$ObjectResult = array_map("ToObj", $ar);
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
I've got a very large JSON file that I've converted into an array. The code you see below is the result of
var_dump($array['talents'])
(for those wondering, yes, this is me attempting to use Blizzard Entertainment's API)
Maybe I just missed that day at PHP school, but I just can't seem to remember or figure out how to get the values inside these deeper arrays. And perhaps more importantly, pay attention to the two 'spec' fields - ONE of them has a value of 'SELECTED: BOOLEAN TRUE', and the other does not. Basically, I need the NAME field from the array that has that SELECTED value.
To be honest, much as I love working in PHP, this multi-level thinking is a tad beyond me at the moment, I need some more experience. In the meantime, could anyone give me some tips, pointers, or just straight-up answers as to how I can get the value I need?
Thanks a bunch for your time!
array (size=2)
0 =>
array (size=6)
'talents' =>
array (size=6)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
'glyphs' =>
array (size=2)
'major' =>
array (size=0)
...
'minor' =>
array (size=1)
...
'spec' =>
array (size=6)
'name' => string 'Frost' (length=5)
'role' => string 'DPS' (length=3)
'backgroundImage' => string 'bg-mage-frost' (length=13)
'icon' => string 'spell_frost_frostbolt02' (length=23)
'description' => string 'Freezes enemies in their tracks and shatters them with Frost magic.' (length=67)
'order' => int 2
'calcTalent' => string '120021.' (length=7)
'calcSpec' => string 'b' (length=1)
'calcGlyph' => string 'O' (length=1)
1 =>
array (size=7)
'selected' => boolean true
'talents' =>
array (size=6)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
'glyphs' =>
array (size=2)
'major' =>
array (size=0)
...
'minor' =>
array (size=0)
...
'spec' =>
array (size=6)
'name' => string 'Fire' (length=4)
'role' => string 'DPS' (length=3)
'backgroundImage' => string 'bg-mage-fire' (length=12)
'icon' => string 'spell_fire_firebolt02' (length=21)
'description' => string 'Ignite enemies with balls of fire and combustive flames.' (length=56)
'order' => int 1
'calcTalent' => string '122012.' (length=7)
'calcSpec' => string 'Z' (length=1)
'calcGlyph' => string '' (length=0)
should be
print_r($array['talents'][0]);
print_r($array['glyphs']['major']);
I have array like this
array (size=6)
0 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '231' (length=3)
'document_title' => string 'Renstra 2014-2018 Distamben COVER' (length=33)
'document_date' => string '2014-10-15 14:09:00' (length=19)
'site' => string 'Propinsi Nusa Tenggara Barat' (length=28)
'url_download' => string 'http://ntbprov.sip-ppid.net/index.php/document/download/231' (length=59)
1 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '7' (length=1)
'document_title' => string 'Data Menara 2014' (length=16)
'document_date' => string '2015-01-08 13:05:00' (length=19)
'site' => string 'Kabupaten Bima' (length=14)
'url_download' => string 'http://bimakab.sip-ppid.net/index.php/document/download/7' (length=57)
how to sort by document_date? I try with usort() but not working.
I am Newbie.