PHP array with dynamic index - php

I tried to make table from PHP array. But here all key are dynamic and have also child array
Here is the array structure:
array (
1371618448317 =>
array (
0 =>
array (
0 => '23.77311734',
1 => '90.396355125',
2 => '23.77313316',
3 => '90.396411867187',
4 => '23.77309048',
5 => '90.396419484375',
6 => '23.77307348',
7 => '90.3963645',
),
1 => 20911,
2 =>
array (
1371618713208 =>
array (
0 => 1,
1 => 'BRAC Delivery Centre',
2 => '371/A Shahinbag',
3 => 25,
4 => 91,
5 => 221,
6 => 1,
7 => 11,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 1,
),
),
),
1371619410448 =>
array (
0 =>
array (
0 => '23.77894566',
1 => '90.39968559375',
2 => '23.77916362',
3 => '90.400307765625',
4 => '23.77889887',
5 => '90.400401515625',
6 => '23.77870083',
7 => '90.399780515625',
),
1 => 24612,
2 =>
array (
1371619950162 =>
array (
0 => 1,
1 => 'EPI Centre (Govt.)',
2 => ' Mohakhali Road Mohakhali',
3 => 20,
4 => 91,
5 => 11,
6 => 1,
7 => 12,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 0,
),
),
),
1371621080807 =>
array (
0 =>
array (
0 => '23.77746206',
1 => '90.399232078125',
2 => '23.77744917',
3 => '90.399623390625',
4 => '23.77712934',
5 => '90.39958940625',
6 => '23.77714809',
7 => '90.399205125',
),
1 => 24566,
2 =>
array (
1371621897771 =>
array (
0 => 1,
1 => 'Society for Assistance to Hearing Impaired Children (SAHIC)',
2 => 'N/A Sattola Road Mohakhali',
3 => 20,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 99,
13 => 99,
14 => 99,
15 => 1,
16 => 0,
),
),
),
1371622305777 =>
array (
0 =>
array (
0 => '23.77357261',
1 => '90.36189965625',
2 => '23.77359605',
3 => '90.36197925',
4 => '23.77344028',
5 => '90.36201675',
6 => '23.77341802',
7 => '90.361931296875',
),
1 => 1325,
2 =>
array (
1371622497359 =>
array (
0 => 1,
1 => 'Natoinal Health Care Network',
2 => '3/Ka Pisiculture Housing Society Shamoly',
3 => 29,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 99,
10 => 99,
11 => 1,
12 => 99,
13 => 99,
14 => 1,
15 => 1,
16 => 0,
),
),
)
My Desire tale will be look like:
BRAC Delivery Centre 371/A Shahinbag 23.77311734 90.396355125
EPI Centre (Govt.) Mohakhali Road 23.77746206 90.399232078125
from lat long array I always try to collect first lat long.
Can any one give me solution ?

Here is your solution
Input
$array = array(
1371618448317 => array (
array ('23.77311734','90.396355125','23.77313316','90.396411867187','23.77309048','90.396419484375','23.77307348','90.3963645'),
20911,
array ('1371618713208' => array (1,'BRAC Delivery Centre','371/A Shahinbag',25,91,221,1,11,1,99,1,99,99,99,99,99,1))
),
1371619410448 => array (
array ('23.77894566','90.39968559375','23.77916362','90.400307765625','23.77889887','90.400401515625','23.77870083','90.399780515625',),
24612,
array ('1371619950162' => array (1,'EPI Centre (Govt.)',' Mohakhali Road Mohakhali',20,91,11,1,12,1,99,1,99,99,99,99,99,0))
),
1371621080807 => array(
array ('23.77746206','90.399232078125','23.77744917','90.399623390625','23.77712934','90.39958940625','23.77714809','90.399205125'),
24566,
array ('1371621897771' => array (1,
'Society for Assistance to Hearing Impaired Children (SAHIC)','N/A Sattola Road Mohakhali',20,91,222,1,7,1,1,1,1,99,99,99,1,0)
)
),
1371622305777 => array (
array ('23.77357261','90.36189965625','23.77359605','90.36197925','23.77344028','90.36201675','23.77341802','90.361931296875'),
1325,
array ('1371622497359' => array (1,
'Natoinal Health Care Network','3/Ka Pisiculture Housing Society Shamoly',29,91,222,1,7,1,99,99,1,99,99,1,1,0)
)
)
);
Solution
Coordinates are in 1st array.
Name and address is in 3rd.
Coordinates picks easily by $row[0][0].','.$row[0][1]; in given loop below.
For address and name you need to pick key of 3rd array, for this here I used $row[2][key($row[2]);
Now check the code below.
$new_array = array();
$i=0;
foreach($array as $key => $row){
//echo "<pre>";print_r(key($row[2]));
$new[$i]['coordinates'] = $row[0][0].','.$row[0][1];
$new[$i]['name'] = $row[2][key($row[2])][1];
$new[$i]['address'] = $row[2][key($row[2])][2];
$i++;
}
echo "<pre>";print_r($new);
Output
Array
(
[0] => Array
(
[coordinates] => 23.77311734,90.396355125
[name] => BRAC Delivery Centre
[address] => 371/A Shahinbag
)
[1] => Array
(
[coordinates] => 23.77894566,90.39968559375
[name] => EPI Centre (Govt.)
[address] => Mohakhali Road Mohakhali
)
[2] => Array
(
[coordinates] => 23.77746206,90.399232078125
[name] => Society for Assistance to Hearing Impaired Children (SAHIC)
[address] => N/A Sattola Road Mohakhali
)
[3] => Array
(
[coordinates] => 23.77357261,90.36189965625
[name] => Natoinal Health Care Network
[address] => 3/Ka Pisiculture Housing Society Shamoly
)
)

solved myself:
$assoc = true;
$result = json_decode ($json, $assoc);
echo "<table border='1'>";
echo "<tr><td>Hospital Name</td><td>Address</td><td>Lat1</td><td>Long1</td><td>Lat2</td><td>Long1</td><td>Lat3</td><td>Long3</td><td>Lat4</td><td>Long4</td></tr>";
foreach($result as $single_res){
$lat1 = $single_res[0][0];
$long1 = $single_res[0][1];
$lat2 = $single_res[0][2];
$long2 = $single_res[0][3];
$lat3 = $single_res[0][4];
$long3 = $single_res[0][5];
$lat4 = $single_res[0][6];
$long4 = $single_res[0][7];
$key = $single_res[2];
$key_val = array_keys($key);
$key_val = $key_val[0];
$name = $key[$key_val][1];
$address = $key[$key_val][2];

Related

Loop trough and modify JSON response

I am making API request to Google Sheets and I receive this response:
Array
(
[0] => Array
(
[Title] => Hours
[January] => 1
[February] => 2
[March] => 3
[April] => 4
[May] => 5
[June] => 6
[July] => 7
[August] => 8
)
[1] => Array
(
[Title] => Days
[January] => 3
[February] => 5
[March] => 1
[April] => 6
[May] => 3
[June] => 7
[July] => 4
[August] => 2
)
[2] => Array
(
[Title] => Weeks
[January] => 3
[February] => 5
[March] => 3
[April] => 4
[May] => 0
[June] => 0
[July] => 2
[August] => 6
[September] => 0
[October] => 0
[November] => 1
[December] => 0
)
)
How could I loop trough and modify this array to something like this so I can use it with HighCharts JS library?
series: [{
title: 'Hours',
data: [1, 2, 3, 4, 5, 6 .......]
}, {
title: 'Days',
data: [4, 6, 3, 6, ........]
}, {
title: 'Weeks',
data: [1, 9, 1, 3, ........]
}, {
....
}]
I tried this way:
if ($response->status) {
$rawData = json_decode(json_encode($response->data), true);
}
$series = [];
foreach ($rawData as $index => $rawDatum) {
if (!isset($rawDatum['Title'])) {
continue;
}
foreach ($rawDatum as $columnKey => $value) {
if ($columnKey == 'CvA') {
$series[$columnKey]['Title'][] = $value;
}
}
}
What I got as result:
Array
(
[Title] => Array
(
[title] => Array
(
[0] => Hours
[1] => Days
[2] => Weeks
)
)
)
Also is there a way to get all names of the months saved in $months array for example without doubles?
The following piece of code will create an array with title and the values for each respective subarray Hours, Days, Weeks.
We will also collect the union of the months in an array named $months.
$series = [];
$months = [];
foreach ($rawData as $subarray) {
$title = $subarray['Title'];
// Remove title key
unset($subarray['Title']);
$series[] = [
'title' => $title,
'data' => array_values($subarray),
];
// Union operator to keep unique months.
$months += array_keys($subarray);
}
echo '<pre>';
print_r($months);
echo '</pre>';
echo '<pre>';
print_r($result);
echo '</pre>';
Result $months:
Array
(
[0] => January
[1] => February
[2] => March
[3] => April
[4] => May
[5] => June
[6] => July
[7] => August
[8] => September
[9] => October
[10] => November
[11] => December
)
Result $series:
Array
(
[0] => Array
(
[title] => Hours
[data] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
)
)
[1] => Array
(
[title] => Days
[data] => Array
(
[0] => 3
[1] => 5
[2] => 1
[3] => 6
[4] => 3
[5] => 7
[6] => 4
[7] => 2
)
)
[2] => Array
(
[title] => Weeks
[data] => Array
(
[0] => 3
[1] => 5
[2] => 3
[3] => 4
[4] => 0
[5] => 0
[6] => 2
[7] => 6
[8] => 0
[9] => 0
[10] => 1
[11] => 0
)
)
)
Given this data:
$data = [
[
'Title' => 'Hours',
'January' => 1,
'February' => 2,
'March' => 3,
'April' => 4,
'May' => 5,
'June' => 6,
'July' => 7,
'August' => 8,
],
[
'Title' => 'Days',
'January' => 3,
'February' => 5,
'March' => 1,
'April' => 6,
'May' => 3,
'June' => 7,
'July' => 4,
'August' => 2,
],
[
'Title' => 'Weeks',
'January' => 3,
'February' => 5,
'March' => 3,
'April' => 4,
'May' => 0,
'June' => 0,
'July' => 2,
'August' => 6,
'September' => 0,
'October' => 0,
'November' => 1,
'December' => 0,
]
];
You should be able to do this:
// Store everything here
$result = [];
foreach ($data as $item) {
// Grab the title
$ret['title'] = $item['Title'];
// Remove it from the source dataset
unset($item['Title']);
// Take the remaining values, join with comma
$ret['data'] = implode(',', array_values($item));
// Append to main array
$result[] = $ret;
}
You can skip the implode if want an actual array

PHP - create date range array with values from multi array key search

I have multi array created from previous search. Point of this is enable hotel reservation in case hotel is full and quests must change room during stay. So array $freeRooms is created as array of free room by dates.
$freeRooms = array(
0 => array
(
'2020-07-23' => 37
),
1 => array
(
'2020-07-20' => 38,
'2020-07-21' => 38,
'2020-07-22' => 38
),
2 => array
(
'2020-07-25' => 38,
'2020-07-26' => 38
),
2 => array
(
'2020-07-20' => 59,
'2020-07-21' => 59
),
3 => array
(
'2020-07-20' => 86,
'2020-07-21' => 86
),
4 => array
(
'2020-07-20' => 39,
'2020-07-21' => 39
),
5 => array
(
'2020-07-25' => 39,
'2020-07-26' => 39
),
6 => array
(
'2020-07-20' => 40
),
7 => array
(
'2020-07-24' => 40,
'2020-07-25' => 40,
'2020-07-26' => 40
),
8 => array
(
'2020-07-20' => 41,
'2020-07-21' => 41,
'2020-07-22' => 41,
'2020-07-23' => 41
));
Second array is date range:
$dateRange = array(0 => '2020-07-20',
1 => '2020-07-21',
2 => '2020-07-22',
3 => '2020-07-23',
4 => '2020-07-24',
5 => '2020-07-25',
6 => '2020-07-26');
I need create some final array for every day from $dateRange use some rooms from $freeRooms. Point is to use as minimal id rooms as possible to get something like this:
$finalArray = array('2020-07-20' => 41,
'2020-07-21' => 41,
'2020-07-22' => 41,
'2020-07-23' => 41,
'2020-07-24' => 40,
'2020-07-25' => 40,
'2020-07-26' => 40);
This is my actual solution:
order by count of free days
array_multisort(array_map('count', $freeRooms), SORT_DESC, $freeRooms);
set $dateRange keys same as values
$newRange = array_combine($dateRange, $dateRange);
$finalRms = array();
loop and create new array
foreach($ffa as $k => $v) {
foreach($newRange as $nk => $nv) {
if(array_key_exists($nk, $v) && array_key_exists($nk, $finalRms) == false) {
$finalRms[$nk] = $v[$nk];
}
}
}

How to randomly get min value from Array?

I have an array with multiple similar minimum value.
May I know how to randomly get one of the minimum value?
Here is my sample code:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
$intNo = $b[0];
May I know how to get min value list (3 => 12, 4 => 12,5 => 12,10 => 12) and randomly pick one of them so that I can set in $intNo?
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
// Taking a random KEY from $b
$key = array_rand($b);
// Taking a KEY from $aryNo which is under `$key`
echo $b[$key];
// Taking a VALUE from `$aryNo` which is under `$b[$key]`
echo $aryNo[$b[$key]];
The fiddle.
Try something like this:
$aryNo = [34,34,34,51,12,12,12,56,876,453,43,12];
foreach($aryNo as $a) {
$finalArray[$a][] = $a;
}
print("<pre>".print_r($finalArray,true)."</pre>");
$minKey = min(array_keys($finalArray));
print("<pre>".print_r($finalArray[$minKey],true)."</pre>");
$randIndex = array_rand($finalArray[$minKey]);
print_r("Key: ".$randIndex.", ".$finalArray[$minKey][$randIndex]);
First print prints:
Array
(
[34] => Array
(
[0] => 34
[1] => 34
[2] => 34
)
[51] => Array
(
[0] => 51
)
[12] => Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
[56] => Array
(
[0] => 56
)
[876] => Array
(
[0] => 876
)
[453] => Array
(
[0] => 453
)
[43] => Array
(
[0] => 43
)
)
Than you select min key, and that prints this:
Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
At the end you pick random key from this array and print the value:
Key: 2, Value: 12
`<?php
$sortArr = array();
$aryNo = array(0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,5 =>
12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,10 => 12);
asort($aryNo);
$aryNo = array_values($aryNo);
print_r($aryNo);
echo $aryNo[0];
?>`
I found that if I using shuffle(); also work for me.
Here is my example:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$aryNo2 = array_keys($aryNo, min($aryNo));
shuffle($aryNo2); //
$intWinNo = $aryNo2[0];
Thanks #u_mulder suggestion & answer.

Format a nested set model result into PHP array

Looking to create a function which arrangesmy nested set model into an array based on levels (categories[0], sub_categories[1], types[2]).
categories[0]
Alcohol |
| sub_categories[1]
|—————Beer
|
|—————Spirts
|
|—————Cider
|
|—————Wine
| |
| | types[2]
| |—————White wine
| |
| |—————Red wine
|
Bvrages |
| sub_categories[1]
|————— Soft Drinks
I was having a look at an older post and it got me close but only transferred the title and not the whole array How do I format Nested Set Model data into an array?.
What i want to achieve is a similar structure like above. Just remember i want to move all data not just the title.
My current output from my query is as follows:
Array
(
[0] => Array
(
[id] => 1
[title] => alcohol
[level] => 0
[uri] => alcohol
[count] => 100
)
[1] => Array
(
[id] => 2
[title] => beer
[level] => 1
[uri] => beer
[count] => 50
)
[2] => Array
(
[id] => 3
[title] => cider
[level] => 1
[uri] => cider
[count] => 20
)
[3] => Array
(
[id] => 4
[title] => wine
[level] => 1
[uri] => wine
[count] => 20
)
[4] => Array
(
[id] => 6
[title] => white wine
[level] => 2
[uri] => white-wine
[count] => 5
)
[5] => Array
(
[id] => 7
[title] => red wine
[level] => 2
[uri] => red-wine
[count] => 15
)
[6] => Array
(
[id] => 8
[title] => spirits
[level] => 1
[uri] => spirits
[count] => 5
)
[7] => Array
(
[id] => 9
[title] => Beverages
[level] => 0
[uri] => beverages
[count] => 50
)
[8] => Array
(
[id] => 10
[title] => soft drinks
[level] => 1
[uri] => soft-drink
[count] => 10
)
)
Any thoughts?
Try to use this code:
<?php
//Represent your array like this (you may include other data to the each item)
//array must be ordered by 'left' field:
$data = array(
array("left" => 1, "right" => 10, "name" => "P0"),
array("left" => 2, "right" => 7, "name" => "P1"),
array("left" => 3, "right" => 4, "name" => "P11"),
array("left" => 5, "right" => 6, "name" => "P12"),
array("left" => 8, "right" => 9, "name" => "P2")
);
//Converter function gets nested sets array and returns nested php array
function nest($arrData){
$stack = array();
$arraySet = array();
foreach( $arrData as $intKey=>$arrValues) {
$stackSize = count($stack);
while($stackSize > 0 && $stack[$stackSize-1]['right'] < $arrValues['left']) {
array_pop($stack);
$stackSize--;
}
$link =& $arraySet;
for($i=0;$i<$stackSize;$i++) {
$link =& $link[$stack[$i]['id']]["children"]; //navigate to the proper children array
}
$tmp = array_push($link, array ('item'=>$arrValues,'children'=>array()));
array_push($stack, array('id' => $tmp-1, 'right' => $arrValues['right']));
}
return $arraySet;
}
//Print result
printArray(nest($data));
function printArray($array){
echo "<ul>";
foreach ($array as $row){
$children = $row['children'];
echo "<li>";
echo $row['item']['name'];
if (!empty($children)) printArray($children);
echo "</li>";
}
echo "</ul>";
}
?>
If I understood you right, then try this solution:
<?php
$data=Array(
0 => Array(
'id' => 1,
'title' => 'alcohol',
'level' => 0,
'uri' => 'alcohol',
'count' => 100,
),
1 => Array(
'id' => 2,
'title' => 'beer',
'level' => 1,
'uri' => 'beer',
'count' => 50,
),
2 => Array(
'id' => 3,
'title' => 'cider',
'level' => 1,
'uri' => 'cider',
'count' => 20,
),
3 => Array(
'id' => 4,
'title' => 'wine,',
'level' => 1,
'uri' => 'wine',
'count' => 20,
),
4 => Array(
'id' => 6,
'title' => 'white wine',
'level' => 2,
'uri' => 'white-wine',
'count' => 5,
),
5 => Array(
'id' => 7,
'title' => 'red wine',
'level' => 2,
'uri' => 'red-wine',
'count' => 15,
),
6 => Array(
'id' => 8,
'title' => 'spirits',
'level' => 1,
'uri' => 'spirits',
'count' => 5,
),
7 => Array(
'id' => 9,
'title' => 'Beverages',
'level' => 0,
'uri' => 'beverages',
'count' => 50,
),
8 => Array(
'id' => 10,
'title' => 'soft drinks',
'level' => 1,
'uri' => 'soft-drink',
'count' => 10,
)
);
$cats=array();
$prev_level=1;
$index0='';
$index1=0;
foreach($data as $dat){
switch($dat['level']){
case 0:
$cats[$dat['uri']]=array(
'name'=>$dat['title'],
'sub_categories'=>array()
);
$index0=$dat['uri'];
break;
case 1:
if($prev_level<1)
$index1=0;
else
$index1++;
$cats[$index0]['sub_categories'][$index1]=array(
'name'=>$dat['title']
);
break;
case 2:
if($prev_level<2){
$cats[$index0]['sub_categories'][$index1]['types']=array();
}
$cats[$index0]['sub_categories'][$index1]['types'][]=$dat['title'];
break;
}
$prev_level=$dat['level'];
}
print_r($cats);
?>

Sort an array according to the value in php

Here is my array . I want to sort the array according the value of each key
Input array:-
Array
(
[location_classroom] => 209
[location_daily_pe] => 1
[location_hallways] => 3
[location_playground] => 93
[location_shade_area] => 26
[location_specialist] => 8
[location_toilet] => 3
[location_others] => 27
[location_others_info] => 0
)
Output array:-
Array
(
[location_others_info] => 0
[location_daily_pe] => 1
[location_hallways] => 3
[location_toilet] => 3
[location_specialist] => 8
[location_shade_area] => 26
[location_playground] => 93
[location_classroom] => 209
[location_others] => 27
)
Indeed you should use asort():
$arr = [
'location_classroom' => 209,
'location_daily_pe' => 1,
'location_hallways' => 3,
'location_playground' => 93,
'location_shade_area' => 26,
'location_specialist' => 8,
'location_toilet' => 3,
'location_others' => 27,
'location_others_info' => 0
];
asort($arr);
# but you can't print the output of the sorting - it'll give you nothing meaningful (boolean)
# you should print the sorted array itself
print_r($arr);
OUTPUT
Array
(
[location_others_info] => 0
[location_daily_pe] => 1
[location_toilet] => 3
[location_hallways] => 3
[location_specialist] => 8
[location_shade_area] => 26
[location_others] => 27
[location_playground] => 93
[location_classroom] => 209
)

Categories