I new to PHP, I need to convert single dimension array into multi dimension array in php
I have data like this, need to minimize as below.
Array
(
[0] => Array
(
[0] => David
[1] => School
[2] => 19
[3] => 29
)
[1] => Array
(
[0] => Paul
[1] => Home
[2] => 19
[3] => 29
)
[2] => Array
(
[0] => Paul
[1] => Cinema
[2] => 19
[3] => 29
)
[3] => Array
(
[0] => Paul
[1] => Park
[2] => 19
[3] => 29
)
[4] => Array
(
[0] => Rossie
[1] => Playground
[2] => 19
[3] => 29
)
[5] => Array
(
[0] => Rossie
[1] => Hotel
[2] => 19
[3] => 29
)
[6] => Array
(
[0] => Rossie
[1] => Hospital
[2] => 19
[3] => 29
)
)
And I want convert it to multidimensional
Array
(
[0] => Array
(
[0] => Array
(
[0] => David
(
[0] => School
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Paul
(
[0] => Home
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Cinema
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Park
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[2] => Array
(
[0] => Array
(
[0] => Rossie
(
[0] => Playground
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hotel
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hospital
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
)
I hope you get an idea. But my function doesn't do this correctly or maybe there are other ways to do this easier ?
I would be grateful for any help.
Thanks
Below is one way to do it:
<?php
$arr = [array('David','School',19,29),
array('Paul','Home',19,29),
array('Paul','Cinema',19,29),
array('Paul','Park',19,29),
array('Rossie','Playground',19,29),
array('Rossie','Hotel',19,29),
array('Rossie','Hospital',19,29)];
// Get all names.
$names = array_unique(array_map(function($value){return $value[0];}, $arr));
$places = [];
// Create the multidimensional array, grouping by name.
foreach($names as $key => $name){
$tempArr = [];
foreach($arr as $record){
if($record[0] === $name){
$tempArr[][$record[1]] = array_slice($record,2);
}
}
$places[][][$name] = $tempArr;
}
print("<pre>".print_r($places,true)."</pre>");
This will return the following result:
Array
(
[0] => Array
(
[0] => Array
(
[David] => Array
(
[0] => Array
(
[School] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[Paul] => Array
(
[0] => Array
(
[Home] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Cinema] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Park] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[2] => Array
(
[0] => Array
(
[Rossie] => Array
(
[0] => Array
(
[Playground] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Hotel] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Hospital] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
)
I'm getting values for my flot chart via ajax.
At the backend script my test array looks like this:
Array
(
[0] => Array
(
[0] => 6
[1] => 1
)
[1] => Array
(
[0] => 7
[1] => 7
)
[2] => Array
(
[0] => 8
[1] => 37
)
[3] => Array
(
[0] => 9
[1] => 44
)
)
The value with the offset [0] represents the hour.
Now I need 24 array objects for every hour. How to maintain this without touching the given elements?
e.g.
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[...]
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
Thanks in advance.
You want to "re-key" this result set according to the first subarray value, and then merge it into an array of 24 hour elements.
Try this:
$data = [
[6, 1],
[7, 7],
[8, 37],
[9, 44]
];
$hours = array_fill(0, 25, [0, 0]);
$data = array_combine(array_column($data, 0), $data);
$hours = array_replace($hours, $data);
print_r($hours);
This:
Creates a 24 element array $hours containing sub-arrays containing default values [0, 0]
Re-keys your $data, pulling out and using the first value of each sub-array to do so. This assumes you are using PHP 5.5.
Finally, replace the modified $data into $hours
This yields:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
)
[2] => Array
(
[0] => 0
[1] => 0
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 0
[1] => 0
)
[5] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
[10] => Array
(
[0] => 0
[1] => 0
)
[11] => Array
(
[0] => 0
[1] => 0
)
[12] => Array
(
[0] => 0
[1] => 0
)
[13] => Array
(
[0] => 0
[1] => 0
)
[14] => Array
(
[0] => 0
[1] => 0
)
[15] => Array
(
[0] => 0
[1] => 0
)
[16] => Array
(
[0] => 0
[1] => 0
)
[17] => Array
(
[0] => 0
[1] => 0
)
[18] => Array
(
[0] => 0
[1] => 0
)
[19] => Array
(
[0] => 0
[1] => 0
)
[20] => Array
(
[0] => 0
[1] => 0
)
[21] => Array
(
[0] => 0
[1] => 0
)
[22] => Array
(
[0] => 0
[1] => 0
)
[23] => Array
(
[0] => 0
[1] => 0
)
)
Hope this helps :)
You can try also this:
// yours test array
$times = array(
array(6,1),
array(7,7),
array(8,37),
array(9,44)
);
// array with all hours
$fullTimes = array_fill(0, 24, array(0,0));
foreach ($times as $time) {
$fullTimes[$time[0]] = $time;
}
Solution for me:
while ($stmt->fetch()) {
$x = $hour;
$y = $zugriffe;
$data1[$x] = array ($x, $y);
}
for ($i=0;$i<=24;$i++){
if (!isset($data1[$i])){
$data1[$i] = array ($i,0);
}
}
sort($data1);
Assume the array's name is $a, you can do like this:
for($i = 0; $i < 24; $i++) {
if(isset($a[$i])) continue;
else {
$a[$i] = array($i,"something you like here");
}
}
I am getting data from Google Analytics API. The GA API returns these data:
Array
(
[0] => Array
(
[0] => 00
[1] => bing
[2] => 1
)
[1] => Array
(
[0] => 00
[1] => google
[2] => 12
)
[2] => Array
(
[0] => 00
[1] => yahoo
[2] => 1
)
[3] => Array
(
[0] => 01
[1] => google
[2] => 7
)
[4] => Array
(
[0] => 02
[1] => google
[2] => 5
)
[5] => Array
(
[0] => 03
[1] => bing
[2] => 1
)
[6] => Array
(
[0] => 03
[1] => google
[2] => 4
)
[7] => Array
(
[0] => 04
[1] => google
[2] => 7
)
[8] => Array
(
[0] => 05
[1] => google
[2] => 5
)
[9] => Array
(
[0] => 05
[1] => yahoo
[2] => 1
)
[10] => Array
(
[0] => 06
[1] => bing
[2] => 1
)
[11] => Array
(
[0] => 06
[1] => google
[2] => 2
)
[12] => Array
(
[0] => 07
[1] => google
[2] => 4
)
[13] => Array
(
[0] => 08
[1] => bing
[2] => 1
)
[14] => Array
(
[0] => 08
[1] => google
[2] => 8
)
[15] => Array
(
[0] => 09
[1] => bing
[2] => 4
)
[16] => Array
(
[0] => 09
[1] => google
[2] => 13
)
[17] => Array
(
[0] => 10
[1] => bing
[2] => 1
)
[18] => Array
(
[0] => 10
[1] => google
[2] => 19
)
[19] => Array
(
[0] => 10
[1] => yahoo
[2] => 1
)
[20] => Array
(
[0] => 11
[1] => bing
[2] => 1
)
[21] => Array
(
[0] => 11
[1] => google
[2] => 23
)
[22] => Array
(
[0] => 11
[1] => yahoo
[2] => 1
)
[23] => Array
(
[0] => 12
[1] => bing
[2] => 1
)
[24] => Array
(
[0] => 12
[1] => google
[2] => 18
)
[25] => Array
(
[0] => 13
[1] => bing
[2] => 1
)
[26] => Array
(
[0] => 13
[1] => google
[2] => 17
)
[27] => Array
(
[0] => 13
[1] => yahoo
[2] => 1
)
[28] => Array
(
[0] => 14
[1] => bing
[2] => 3
)
[29] => Array
(
[0] => 14
[1] => google
[2] => 30
)
[30] => Array
(
[0] => 14
[1] => yahoo
[2] => 2
)
[31] => Array
(
[0] => 15
[1] => google
[2] => 15
)
[32] => Array
(
[0] => 15
[1] => yahoo
[2] => 2
)
[33] => Array
(
[0] => 16
[1] => bing
[2] => 1
)
[34] => Array
(
[0] => 16
[1] => google
[2] => 22
)
[35] => Array
(
[0] => 16
[1] => yahoo
[2] => 1
)
[36] => Array
(
[0] => 17
[1] => google
[2] => 18
)
[37] => Array
(
[0] => 17
[1] => yahoo
[2] => 2
)
[38] => Array
(
[0] => 18
[1] => google
[2] => 15
)
[39] => Array
(
[0] => 19
[1] => bing
[2] => 1
)
[40] => Array
(
[0] => 19
[1] => google
[2] => 18
)
[41] => Array
(
[0] => 19
[1] => yahoo
[2] => 3
)
[42] => Array
(
[0] => 20
[1] => google
[2] => 15
)
[43] => Array
(
[0] => 21
[1] => google
[2] => 18
)
[44] => Array
(
[0] => 21
[1] => yahoo
[2] => 1
)
[45] => Array
(
[0] => 22
[1] => bing
[2] => 1
)
[46] => Array
(
[0] => 22
[1] => google
[2] => 21
)
[47] => Array
(
[0] => 23
[1] => google
[2] => 8
)
[48] => Array
(
[0] => 23
[1] => yahoo
[2] => 1
)
)
I want that array to form like this:
$example_data = array(
array('00',1,12,1),//bing,google,yahoo
array('01',0,7,0),
array('02',0,5,0),
array('03',1,4,0),
array('04',0,7,0),
array('05',0,5,1),
array('06',1,2,0),
array('07',0,7,0),
array('08',1,8,0),
array('09',0,13,0),
array('10',1,19,1),
//should have more arrays in here, but I hope you got my point...
);
So basically, all the 00 of bing,google, and yahoo is being grouped by on a single array, then the other 01,02,03,etc.. are to be grouped by on a single array again.
So if you noticed that we have a zero on this array array('01',0,7,0), it because bing doesn't have a 01 value in the array, and yahoo also doesn't have a 01 value in the array, except for google which has a 01 that has a value of 7.
Any help how to transform this kind of array to the one I posted.
Your help will be greatly appreciated! Thanks!
<?php
$array=json_decode('[["00","bing","1"],["00","google","12"],["00","yahoo","1"],["01","google","7"],["02","google","5"],["03","bing","1"],["03","google","4"],["04","google","7"],["05","google","5"],["05","yahoo","1"],["06","bing","1"],["06","google","2"],["07","google","4"],["08","bing","1"],["08","google","8"],["09","bing","4"],["09","google","13"],["10","bing","1"],["10","google","19"],["10","yahoo","1"],["11","bing","1"],["11","google","23"],["11","yahoo","1"],["12","bing","1"],["12","google","18"],["13","bing","1"],["13","google","17"],["13","yahoo","1"],["14","bing","3"],["14","google","30"],["14","yahoo","2"],["15","google","15"],["15","yahoo","2"],["16","bing","1"],["16","google","22"],["16","yahoo","1"],["17","google","18"],["17","yahoo","2"],["18","google","15"],["19","bing","1"],["19","google","18"],["19","yahoo","3"],["20","google","15"],["21","google","18"],["21","yahoo","1"],["22","bing","1"],["22","google","21"],["23","google","8"],["23","yahoo","1"]]');
$temp=array();
$temp1=array();
foreach($array as $arr){
if(array_search($arr[0], $temp)===FALSE)
$temp[]=$arr[0];
}
foreach($temp as $t){
$bing=0;
$google=0;
$yahoo=0;
foreach($array as $arr){
if($t==$arr[0]){
if($arr[1]=='bing'){
$bing=$arr[2];
}else if($arr[1]=='google'){
$google=$arr[2];
}else if($arr[1]=='yahoo'){
$yahoo=$arr[2];
}
}
}
$temp1[]=array($t,$bing,$google,$yahoo);
}
var_dump($temp1);
?>
I managed to answer it on my own, not sure if this is efficient but it works! :)
//$new variable is the array that I the google analytics api returned
foreach($new as $key => $value){
if($value[1] == 'bing')
$combine['bing'][$value[0]] = $value[2];
if($value[1] == 'google')
$combine['google'][$value[0]] = $value[2];
if($value[1] == 'yahoo')
$combine['yahoo'][$value[0]] = $value[2];
}
$example_data = array();
for($i=0;$i<=23;$i++){
$tempI = $i;
if($i < 10)
$tempI = "0".$i;
$bing = 0;
$google = 0;
$yahoo = 0;
if(isset($combine['bing'][$tempI])){
$bing = $combine['bing'][$tempI];
}
if(isset($combine['google'][$tempI])){
$google = $combine['google'][$tempI];
}
if(isset($combine['yahoo'][$tempI])){
$yahoo = $combine['yahoo'][$tempI];
}
//time,bing,google,yahoo
$example_data[$i][0] = $tempI;
$example_data[$i][1] = $bing;
$example_data[$i][2] = $google;
$example_data[$i][3] = $yahoo;
}
echo "<pre>";
print_r($example_data);
echo "</pre>";
Thanks again StackOverflow! :)
How about this:
<?php
$initial = [
[ '00', 'bing', 1 ],
[ '00', 'google', 12 ],
[ '00', 'yahoo', 1 ],
[ '01', 'google', 7 ],
[ '02', 'google', 5 ],
[ '03', 'bing', 1 ],
[ '03', 'google', 4 ],
];
$searchEngines = ['bing', 'google', 'yahoo' ];
$temp = [];
foreach ( $initial as $data )
{
// Create array of the type $data['00']['google'] = 12
$temp[ $data[0] ][ $data[1] ] = $data[2];
}
$final = [];
// Now go over the newly created array
foreach( $temp as $code => $entry )
{
// Initialize a new array holding initially that $code ( I don't know if $code is correct term though )
$temp2 = [ $code ];
// And now for each of the defined search engines
foreach( $searchEngines as $se )
{
// Check if we have a value set
if (isset( $entry[$se] ) )
{
// If we do - use it
$temp2[] = $entry[$se];
}
else
{
// Otherwise use 0
$temp2[] = 0;
}
}
// Set it to the final resulting array
$final[] = $temp2;
}
echo '<pre>';print_r( $final );echo '</pre>';
And if someday you decide to add another search engine to the list all you'll have to do will be to add it to the $searchEngines array.