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");
Related
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")
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.
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];
}
}
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];
}