Convert Single Array to multidimensional - php

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
)
)
)
)
)
)

Related

Grouping php arrays and showing highest and lowest value in the array

I have an array that looks like this:
Array
(
[0] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 1
)
[1] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 3
)
[3] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 4
)
[4] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 5
)
[5] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 6
)
[6] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 7
)
[7] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 8
)
[8] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 9
)
[9] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 10
)
[10] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 11
)
[11] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 12
)
[12] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 20
)
[13] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 21
)
[14] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 22
)
[15] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 23
)
[16] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 24
)
[17] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 25
)
[18] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 26
)
[19] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 27
)
[20] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 33
)
[21] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 30
)
[22] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 35
)
[23] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 46
)
[24] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 47
)
[25] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 48
)
[26] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 50
)
[27] => Array
(
[0] => 19.0001.2
[1] => 3
[2] => 1
)
[28] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 40
)
[29] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 41
)
[30] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 42
)
[31] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 7
)
[32] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 8
)
[33] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 9
)
[34] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 13
)
[35] => Array
(
[0] => 19.0001.3
[1] => 4
[2] => 41
)
[36] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 2
)
[37] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 3
)
[38] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 4
)
[39] => Array
(
[0] => 19.0001.8
[1] => 4
[2] => 40
)
[40] => Array
(
[0] => 19.0009.8
[1] => 3
[2] => 8
)
)
I have a function that groups the last value in an array into a range of numbers, however it doesn't work with the above array. I have tried a number of different arrays with no success. The above array is generated by a mysql query result.
$getlocations = $db->query("
SELECT barcode
, test_num
, magnum
, sect
, slidenum
FROM fas_log
WHERE barcode LIKE '%$bcinv'
AND notes = '$notes'
AND removed = '$removed'
AND staffid= '$sid'
ORDER BY magnum, sect, slidenum
");
while ($getlocationsrow = $getlocations->fetch()) {
extract($getlocationsrow);
$magrange[]=array("$magnum", "$sect", "$slidenum");
}
function rangeslides($arr) {
$previous = null;
$result = array();
$consecutiveArray = array();
// Slice array by consecutive sequences
foreach($arr as $number) {
if ($number == $previous + 1) {
$consecutiveArray[] = $number;
} else {
$result[] = $consecutiveArray;
$consecutiveArray = array($number);
}
$previous = $number;
}
$result[] = $consecutiveArray;
// Get length of each sub array
$count = array_map('count', $result);
$first = reset($count);
$last = end($count);
prettyarray($result);
}
I want to extract data from the array to look like this showing the first and last numbers in the range:
19.0001.2 1 - 12
The above array should produce 15 different results with 9 of them showing no range of numbers because they are singular.
ie: 19.0001.8 4 40

building a multidimention array php

I generate this array $months from an awstats file that looks like this :
Array
(
[0] => Array
(
[0] => 10
[1] => 37
[2] => 11
)
[1] => Array
(
[0] => 11
[1] => 34
[2] => 19
)
[2] => Array
(
[0] => 12
[1] => 20
[2] => 11
)
)
Which $months[0][0] represent the number of a month and $months[0][1] it's data. What I'd like to do is to build an array that has all the number of months of the year with it's data and if it doesn't have one make it to 0.
Like this :
Array
(
[0] => Array
(
[0] => 01
[1] => 41
[2] => 52
)
[1] => Array
(
[0] => 02
[1] => 74
[2] => 66
)
[2] => Array
(
[0] => 03
[1] => 40
[2] => 83
)
[3] => Array
(
[0] => 04
[1] => 0
[2] => 0
)
[4] => Array
(
[0] => 05
[1] => 0
[2] => 0
)
[5] => Array
(
[0] => 06
[1] => 0
[2] => 0
)
and so on... Is there any way I can achieve this?Much appreciated!
Here's my idea (if I understood your question right):
First, create another array ($sortedMonths) with 12 elements and fill them with 'empty data':
$sortedMonths = array();
for ($i = 1; $i <= 12; $i++) {
$sortedMonths[] = array($i, 0, 0);
}
/*
Array
(
[0] => Array
(
[0] => 1,
[1] => 0,
[2] => 0
)
...
[11] => Array
(
[0] => 12,
[1] => 0,
[2] => 0
)
)
*/
Then loop through $months and assign data to the appropriate element of $sortedMonths:
foreach ($months as $month) {
$sortedMonths[$month[0]-1] = $month;
}

Split an array into mutiple arrays based on a value - PHP

What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.

determine average value from an array column

consider below array:-
Array
(
[0] => Array
(
[0] => 99895
[1] => 35378
[2] => 0.01
)
[1] => Array
(
[0] => 99895
[1] => 813
[2] => -0.97
)
[2] => Array
(
[0] => 99895
[1] => 771
[2] => 0.29
)
[3] => Array
(
[0] => 442
[1] => 833
[2] => -1.06
)
[4] => Array
(
[0] => 442
[1] => 485
[2] => -0.61
)
[5] => Array
(
[0] => 442
[1] => 367
[2] => -0.14
)
[6] => Array
(
[0] => 442
[1] => 478
[2] => 0.77
)
[7] => Array
(
[0] => 442
[1] => 947
[2] => -0.07
)
[8] => Array
(
[0] => 7977
[1] => 987
[2] => 0.76
)
[9] => Array
(
[0] => 7977
[1] => 819
[2] => 0.37
)
[10] => Array
(
[0] => 7977
[1] => 819
[2] => 0.36
)
[11] => Array
(
[0] => 7977
[1] => 653
[2] => 1.16
)
[12] => Array
(
[0] => 7977
[1] => 1653
[2] => 1.15
)
)
from the above array how will I determine the below array?
array
(
99895 => -0.223
442 => -0.22
7977 => 0.76
)
Actually I need the average value of column 3 in respect of column 1.
First collect all the column 3 elements into an array keyed off column 1:
$arrays = array();
foreach ($input as $vals) {
$key = $vals[0];
$val = $vals[2];
if (isset($arrays[$key])) {
$arrays[$key][] = $val;
} else {
$arrays[$key] = array($val);
}
}
Now go through all of them, calculating the averages:
foreach ($arrays as &$array) {
$array = array_sum($array)/count($array);
}

Is this the correct syntax for 'array_unique()' in PHP?

I want to get rid of duplicates in my array but I'm still printing duplicates with this:
$getuser = ltrim($top10['url'], " users/" ); //trim url to get user id
$array[$i++] = $getuser;
$dirty = $array;
$clean = array_unique($dirty);
print_r($clean)."<br />";
Input print_r($array)
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Output print_r($clean);
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Yes, it seems to be correct. Basically you specify an array to array_unique and it gives you unique items out of the array.
$unique_array = array_unique($your_array);

Categories