How To Get Data from JSON Into Unordered List? - php

I have a JSON file that contain this:
"Menus": [{
"MenuId": "1",
"MenuName": "Perencanaan dan Pengadaan",
"MenuParent": "",
"MenuLink": ""
}, {
"MenuId": "1-1",
"MenuName": "RKA / DPA",
"MenuParent": "1",
"MenuLink": ""
}, {
"MenuId": "1-1-1",
"MenuName": "Daftar RKA / DPA",
"MenuParent": "1-1",
"MenuLink": "rkbu"
},
I want to put that data into unordered list dynamically. So the output I want is like this (with 3 level list):
Perencanaan dan Pengadaan
RKA / DPA
Daftar RKA / DPA
I have tried this code:
echo "<ul>";
foreach($get_data['Menus'] as $node){
if(strlen($node['MenuId']) == 1){
echo "<li>" . $node['MenuName'];
echo "</li>";
}
echo "<ul>";
if(strlen($node['MenuId']) == 3){
echo "<li>".$node['MenuName']."</li>";
}
if(strlen($node['MenuId']) == 5){
echo "<ul>";
echo "<li>".$node['MenuName']."</li>";
echo "</ul>";
}
echo "</ul>";
}
echo "</ul>";
But I find that it is not dynamic because it depends on string length. I've read that the best method is using recursive method. But I cannot find the recursive pattern of my JSON file. Can anybody help me find the solution? Thanks

I don't think it is possible to make recursive calls directly on your flat JSON data.
I suggest you first convert your flat data to a multidimensional array and afterwards recursively generate your menu.
I took parts of the code from here: Dynamically creating/inserting into an associative array in PHP
$get_data = array(
array(
"MenuId" => "1",
"MenuName" => "Perencanaan dan Pengadaan",
"MenuParent" => "",
"MenuLink" => ""
),
array(
"MenuId" => "1-1",
"MenuName" => "RKA / DPA",
"MenuParent" => "1",
"MenuLink" => ""
),
array(
"MenuId" => "1-1-1",
"MenuName" => "Daftar RKA / DPA",
"MenuParent" => "1-1",
"MenuLink" => "rkbu"
)
);
function insert_into(&$array, array $keys, $value) {
$last = array_pop($keys);
foreach($keys as $key) {
if(!array_key_exists($key, $array) ||
array_key_exists($key, $array) && !is_array($array[$key])) {
$array[$key]['items'] = array();
}
$array = &$array[$key]['items'];
}
$array[$last]['value'] = $value;
}
function create_menu($menuItems) {
$content = '<ul>';
foreach($menuItems as $item) {
$content .= '<li>' . $item['value'];
if(isset($item['items']) && count($item['items'])) {
$content .= create_menu($item['items']);
}
$content .= '</li>';
}
$content .= '</ul>';
return $content;
}
$menuItems = array();
foreach($get_data as $item) {
$levels = explode('-', $item['MenuId']);
insert_into($menuItems, $levels, $item['MenuName']);
}
print_r($menuItems);
print create_menu($menuItems);
DEMO: http://3v4l.org/dRK4f
Output:
Array (
[1] => Array (
[value] => Perencanaan dan Pengadaan
[items] => Array (
[1] => Array (
[value] => RKA / DPA
[items] => Array (
[1] => Array (
[value] => Daftar RKA / DPA
)
)
)
)
)
)
<ul>
<li>Perencanaan dan Pengadaan
<ul>
<li>RKA / DPA
<ul>
<li>Daftar RKA / DPA</li>
</ul>
</li>
</ul>
</li>
</ul>

Related

Find highest value entry in sub array using JSON & PHP

I have been playing around with this for a few hours now and have had not much luck.
My current JSON looks like this:
https://pastebin.com/TSmWFA2g
"10-10-2019 12:00AM":[
{
"speed":33,
"latitude":-11.2588112,
"longitude":100.8249533
},
{
"speed":33,
"latitude":-11.2381112,
"longitude":100.82509
},
{
"speed":31,
"latitude":-11.827312,
"longitude":100.8242733
}
],
"10-10-2019 12:01AM":[
{
"speed":29,
"latitude":-11.2902112,
"longitude":100.8202849
},
{
"speed":26,
"latitude":-11.2826432,
"longitude":100.3760333
}
]
What I am attempting to do is for each date find the entry that has the highest "speed" and remove the other entries under that date (or create a new array with the single entry).
EDIT 01:
I have now tried:
function my_sort($a,$b)
{
return $b['speed'] - $a['speed'];
}
usort($finalData,"my_sort");
echo json_encode($finalData);
This sorts the data by speed but the JSON now does not include the date found in the original JSON.
[{"speed":33,"latitude":-11.2588112,"longitude":100.82509},
{"speed":33,"latitude":-11.2588112,"longitude":100.82509},
{"speed":31,"latitude":-11.2588112,"longitude":100.82509},
{"speed":31,"latitude":-11.2588112,"longitude":100.82509},
{"speed":33,"latitude":-11.2588112,"longitude":100.82509},
{"speed":32,"latitude":-11.2588112,"longitude":100.82509},
{"speed":24,"latitude":-11.2588112,"longitude":100.82509},
{"speed":16,"latitude":-11.2588112,"longitude":100.82509},]
<?php
$data = json_decode('{
"10-10-2019 12:00AM":[
{
"speed":33,
"latitude":-11.2588112,
"longitude":100.8249533
},
{
"speed":33,
"latitude":-11.2381112,
"longitude":100.82509
},
{
"speed":31,
"latitude":-11.827312,
"longitude":100.8242733
}
],
"10-10-2019 12:01AM":[
{
"speed":29,
"latitude":-11.2902112,
"longitude":100.8202849
},
{
"speed":26,
"latitude":-11.2826432,
"longitude":100.3760333
}
],
"10-10-2019 12:02AM":[
{
"speed":35,
"latitude":-11.2991112,
"longitude":100.0129199
},
{
"speed":33,
"latitude":-11.9273112,
"longitude":100.8734016
},
{
"speed":32,
"latitude":-11.2533212,
"longitude":100.19229
},
{
"speed":30,
"latitude":-11.2928112,
"longitude":100.2495099
},
{
"speed":24,
"latitude":-11.2228112,
"longitude":100.9266033
}
]
}',true);
$newArray=array();
foreach ($data as $key => $value) {
array_multisort(array_column($value, 'speed'), SORT_DESC, $value);
$newArray[$key]=$value[0];
}
echo "<pre>";
print_r($newArray);
echo "<pre>";
?>
$max = []; //store highest speeds in new array
foreach ($json as $key => $obj) {
$max[$key] = max(array_map(function($o) {
return $o;
}, $obj));
}
Working sample: http://sandbox.onlinephpfunctions.com/code/2f6e1a86775e206650bfe86f7602464c0fce17f0
As you just want the highest for each date, this code just loops round each item and stores the one with the highest speed for the date, if there are multiple ones with the same speed, the first is taken. This saves having to sort the arrays and then chop them up and makes 1 pass through the data...
$output = [];
$input = json_decode($data, true);
foreach ( $input as $date => $dateSection ) {
$max = ["speed" => 0];
foreach ( $dateSection as $item ) {
if ( $item["speed"] > $max["speed"] ) {
$max = $item;
}
}
$output[$date] = $max;
}
print_r($output);
this gives the output...
Array
(
[10-10-2019 12:00AM] => Array
(
[speed] => 33
[latitude] => -11.2588112
[longitude] => 100.8249533
)
[10-10-2019 12:01AM] => Array
(
[speed] => 29
[latitude] => -11.2902112
[longitude] => 100.8202849
)
[10-10-2019 12:02AM] => Array
(
[speed] => 35
[latitude] => -11.2991112
[longitude] => 100.0129199
)
)

Create a hierarchical list of array elements

I have a an array of products. Each product contains its category and subcategories listed in a hierarchical order:
Array
(
[product_id_1] => Array
(
[0] => Men
[1] => Sunglasses
[2] => Luxury
[3] => Ray-ban
)
[product_id_2] => Array
(
[0] => Women
[1] => Lenses
[2] => Casual
[3] => Gucci
)
[product_id_3] => Array
(
[0] => Men
[1] => Sunglasses
[2] => Casual
[3] => Prada
)
[...]
)
I want to create an unordered hierarchical HTML menu like so:
-Men
--Sunglasses
---Luxury
----Ray-ban
---Casual
----Prada
-Women
--Lenses
---Casual
----Gucci
The function should strip out repetitive categories and subcategories. This script returns the array of products that I've posted at the top:
<?php
function displayNestedMenu( $posts, $taxonomies ) {
foreach ( $posts as $post ) {
foreach ( $taxonomies as $key => $taxonomy ) {
$push = wp_get_object_terms( $post->ID, $taxonomy );
if ( !empty( $push ) ) {
$list[$post->ID][] = $push[0]->name;
}
}
}
return $list;
}
print_r( displayNestedMenu( $posts, $taxonomies ) );
?>
I imagine the solution should invoke the function inside the function but after trying a couple of methods I haven't succeeded yet. Any suggestions are appreciated!
Here is a simple idea :
$array = array(
'product_id_1' => array(
'Men',
'Sunglasses',
'Luxury',
'Ray-ban'
),
'product_id_2' => array(
'Women',
'Lenses',
'Casual',
'Gucci',
),
'product_id_3' => array(
'Men',
'Sunglasses',
'Casual',
'Prada'
)
);
The idea is to recreate the keys depending on the parent category, after that we sort them using ksort():
function tree($array){
$newArray = array();
foreach ($array as $arr) {
foreach ($arr as $key => $row) {
if ($key > 0) {
$index = array();
for ($i = 0; $i <= $key; $i++)
$index[] = $arr[$i];
$index = implode('_', $index);
} else
$index = $row;
$newArray[$index] = $row;
}
}
ksort($newArray);
return $newArray;
}
Then display the HTML :
$products = tree($array);
$i = 0;
echo '<ul style="list-style-type:none">';
foreach ($products as $key => $row) {
if(strcmp($row, $key) == 0 && $i != 0)
echo '</ul><br><ul style="list-style-type:none">';
++$i;
$level = count(explode('_', $key));
$padding = 15 * (--$level);
echo
'<li style="padding-left:' . $padding . 'px">
<span style="border-left:1px dashed black;border-bottom:1px dashed black;"> ' . $row . '</span>
</li>';
}
echo '</ul>';
PHP has powerful array features: string-indexed arrays can help provide solutions to problems like this one.
For the array conversion step:
$hrchy=array();
foreach($products AS $product){//$products is as per your first array, at start…
hrchy_ins($hrchy,$product);
}
function hrchy_ins(array &$hierarchy,array $product){//$hierarchy should be passed by reference…
if(\count($product)>0){//Condition necessary to implement base case, avoiding infinite recursion
if(!isset($hierarchy[$product[0]])){$hierarchy[$product[0]]=array();}//Conditional execution ignores duplicates…
if(\count($product)>1){hrchy_ins($hierarchy[$product[0]],\array_slice($product,1));}//Condition may not be strictly necessary (see condition above!)
} }
We might now use a recursive approach for a further HTML-writing step (the recursion secret sauce = a simple recursive function including a branch-on-condition for the base-case):
function prod_list(array $hierarchy){
if(\count($hierarchy)===0){
return '';
}else{
$list='';
$list.='<ul>';
foreach($hierarchy AS $cat => $children){
$list.='<li>'.$cat;
$list.=prod_list($children);//Recursive step…
$list.='</li>';
}
$list.='<ul>';
return $list;
}
}
Finally, after defining the function, we invoke it:
echo(prod_list($hrchy));
Disclaimer: I have not tested this code.
You could transform the array in a static way, as the structure you describe always has four parts;
$hierarchy = array();
foreach($products as $product_id => $product) {
list($gender, $category, $type, $brand) = $product;
$hierarchy[$gender][$category][$type][$brand][] = $product_id;
}

php create navigation menu from multidimensional array dynamically

I did research on this, and wasn't able to find an exact answer. Most of the questions/answers on here pertaining to this seem to be unfinished. If anyone knows of a finished solution similar to my question, please point me in that direction!
Here is my array:
Array
(
['home'] => Array
(
[0] => sub-home1
[1] => sub-home2
)
['about'] => Array
(
[0] => sub-about
['about2'] => Array
(
[0] => sub-sub-about
)
)
['staff'] => Array
(
[0] => sub-staff1
[1] => sub-staff2
)
['contact'] => contact
)
And here is what I would like to turn it into:
<ul>
<li><a href="">home<a/>
<ul>
<li>sub-home1</li>
<li>sub-home2</li>
</ul>
</li>
<li><a href="">about<a/>
<ul>
<li>sub-about</li>
<li>about2
<ul>
<li><a href="">sub-sub-about<a/></li>
</ul>
</li>
</ul>
</li>
<li><a href="">staff<a/>
<ul>
<li>sub-staff1</li>
<li>sub-staff2</li>
</ul>
</li>
<li><a href="">contact<a/></li>
</ul>
The array will be dynamically generated, but will have a limit of 3 levels ex: about->about2->sub-sub-about. I tried going off of this question: PHP/MySQL Navigation Menu but they didn't really seem to come to a conclusion? I am familiar with foreach's whiles and for loops but I just can't seem to wrap my head around this one.
EDIT: Enzino, your code works!
Here is my solution:
<?php
function MakeMenu($items, $level = 0) {
$ret = "";
$indent = str_repeat(" ", $level * 2);
$ret .= sprintf("%s<ul>\n", $indent);
$indent = str_repeat(" ", ++$level * 2);
foreach ($items as $item => $subitems) {
if (!is_numeric($item)) {
$ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $item);
}
if (is_array($subitems)) {
$ret .= "\n";
$ret .= MakeMenu($subitems, $level + 1);
$ret .= $indent;
} else if (strcmp($item, $subitems)){
$ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $subitems);
}
$ret .= sprintf("</li>\n", $indent);
}
$indent = str_repeat(" ", --$level * 2);
$ret .= sprintf("%s</ul>\n", $indent);
return($ret);
}
$menu = Array(
'home' => Array("sub-home1", "sub-home2"),
'about' => Array("sub-about", "about2" => Array("sub-sub-about")),
'staff' => Array("sub-staff1", "sub-staff2"),
'contact' => "contact"
);
print_r($menu);
echo MakeMenu($menu);
?>
Calvin's solution worked for me. Here's the edited version. We can use more nested loops to get sub - sub menu items.
echo '<ul>';
foreach ($menu as $parent) {
echo '<li>' . $parent . '';
if (is_array($parent)) {
echo '<ul>';
foreach ($parent as $children) {
echo '<li>' . $children . '';
}
echo '</ul>';
}
echo '</li&gt';
}
echo '</ul>';
I think you can use recursion? Here is some pseudocode, not very familiar with php.
function toNavMenu(array A){
for each element in A{
echo "<li>" + element.name + ""
if (element is an array){
echo "<ul>"
toNavMenu(element)
echo "</ul>"
}
echo "</li>"
}
}
I would probably slightly adapt the array to be something like the following:
Array(
0 => Array(
'title' => 'Home',
'children' => Array()
),
1 => Array(
'title' => 'Parent',
'children' => Array(
0 => Array(
'title' => 'Sub 1',
'children' => Array(),
),
1 => Array(
'title' => 'Sub 2',
'children' => Array(
0 => Array(
'title' => 'Sub sub 2-1',
'children' => Array(),
),
),
),
)
)
)
With a structure like this you could use recursion to build your menu HTML:
function buildMenu($menuArray)
{
foreach ($menuArray as $node)
{
echo "<li><a href='#'/>" . $node['title'] . "</a>";
if ( ! empty($node['children'])) {
echo "<ul>";
buildMenu($node['children']);
echo "</ul>";
}
echo "</li>";
}
}

Taking a string of period separated properties and converting it to a json object in php

I'm fairly sure I'm missing something blindingly obvious here but here it goes.
I am working on updating a search function in an application which was running a loop and doing a very large number of sql queries to get object / table relations to one large query that returns everything. However the only way I could think to return relations was period separated, what I am now wanting to do is take the flat array of keys and values and convert it into an associative array to then be jsonified with json_encode.
For example what I have is this...
array(
"ID"=>10,
"CompanyName"=>"Some Company",
"CompanyStatusID"=>2,
"CompanyStatus.Status"=>"Active",
"addressID"=>134,
"address.postcode"=>"XXX XXXX",
"address.street"=>"Some Street"
);
And what I want to turn it into is this...
array(
"ID"=>10,
"CompanyName"=>"Some Company",
"CompanyStatusID"=>2,
"CompanyStatus"=>array(
"Status"=>"Active"
),
"addressID"=>134,
"address"=>array(
"postcode"=>"XXX XXXX",
"street"=>"Some Street"
)
);
Now I'm sure this should be a fairly simple recursive loop but for the life of me this morning I can't figure it out.
Any help is greatly appreciated.
Regards
Graham.
Your function was part way there mike, though it had the problem that the top level value kept getting reset on each pass of the array so only the last period separated property made it in.
Please see updated version.
function parse_array($src) {
$dst = array();
foreach($src as $key => $val) {
$parts = explode(".", $key);
if(count($parts) > 1) {
$index = &$dst;
$i = 0;
$count = count($parts)-1;
foreach(array_slice($parts,0) as $part) {
if($i == $count) {
$index[$part] = $val;
} else {
if(!isset($index[$part])){
$index[$part] = array();
}
}
$index = &$index[$part];
$i++;
}
} else {
$dst[$parts[0]] = $val;
}
}
return $dst;
}
I am sure there is something more elegant, but quick and dirty:
$arr = array(
"ID"=>10,
"CompanyName"=>"Some Company",
"CompanyStatusID"=>2,
"CompanyStatus.Status"=>"Active",
"addressID"=>134,
"address.postcode"=>"XXX XXXX",
"address.street"=>"Some Street"
);
$narr = array();
foreach($arr as $key=>$val)
{
if (preg_match("~\.~", $key))
{
$parts = split("\.", $key);
$narr [$parts[0]][$parts[1]] = $val;
}
else $narr [$key] = $val;
}
$arr = array(
"ID" => 10,
"CompanyName" => "Some Company",
"CompanyStatusID" => 2,
"CompanyStatus.Status" => "Active",
"addressID" => 134,
"address.postcode" => "XXX XXXX",
"address.street" => "Some Street",
"1.2.3.4.5" => "Some nested value"
);
function parse_array ($src) {
$dst = array();
foreach($src as $key => $val) {
$parts = explode(".", $key);
$dst[$parts[0]] = $val;
if(count($parts) > 1) {
$index = &$dst[$parts[0]];
foreach(array_slice($parts, 1) as $part) {
$index = array($part => $val);
$index = &$index[$part];
}
}
}
return $dst;
}
print_r(parse_array($arr));
Outputs:
Array
(
[ID] => 10
[CompanyName] => Some Company
[CompanyStatusID] => 2
[CompanyStatus] => Array
(
[Status] => Active
)
[addressID] => 134
[address] => Array
(
[street] => Some Street
)
[1] => Array
(
[2] => Array
(
[3] => Array
(
[4] => Array
(
[5] => Some nested value
)
)
)
)
)

Problem to generate nested ul lists using PHP

I am working on a front-end web app where a nested unordered list would be used for the jQuery plugin mcdropdown.
Here is the data structure from PHP: a nested array of arrays :
Array
(
[0] => Array
(
[fullpath] => ../foil/alphanumeric/
[depth] => 0
)
[1] => Array
(
[fullpath] => ../foil/alphanumeric/letters/
[depth] => 1
)
[2] => Array
(
[fullpath] => ../foil/alphanumeric/numbers/
[depth] => 1
)
[3] => Array
(
[fullpath] => ../foil/alphanumeric/numbers/symbols/
[depth] => 2
)
)
Basically, I took the excellent answer from this question on SO, modified it a bit :
global $fullpaths; // $fullpaths contains the above data structure in print_r
$result = '';
$currentDepth = -1;
while(!empty($fullpaths))
{
$currentNode = array_shift($fullpaths);
if($currentNode['depth'] > $currentDepth)
{
$result .='<ul>';
}
if($currentNode['depth'] < $currentDepth)
{
$result .=str_repeat('</ul>', $currentDepth - $currentNode['depth']);
}
$result .= '<li>'. $currentNode['fullpath'] .'</li>';
$currentDepth = $currentNode['depth'];
if(empty($fullpaths))
{
$result .= str_repeat('</ul>', 1 + $currentDepth);
}
}
print $result;
and got the following output:
<ul>
<li>../foil/alphanumeric/</li>
<ul>
<li>../foil/alphanumeric/letters/</li>
<li>../foil/alphanumeric/numbers/</li>
<ul>
<li>../foil/alphanumeric/numbers/symbols/</li>
</ul>
</ul>
</ul>
Which cannot be accepted by the mcdropdown jQuery plugin, it expects something like this:
<li rel="1">
'Alphanumeric'
<ul>
<li rel="2">'Letters'</li>
<li rel="3">'Numbers'
<ul>
<li rel="4">'Symbols'</li>
</ul>
</li>
</ul>
</li>
To be frank, I don't quite understand how the answer from that question works, I have been trying to modify that solution to cope with my situation, but still failed.
Any help and suggestion is much appropriated in advance.
If you already have the correct depth values, then you don't need recursion. I have a similar function that I use for <ul>-<li>-generation:
function ulli($newlevel, &$level, $UL="ul", $once=1) {
if ($level == $newlevel) {
echo "</li>\n";
}
while ($level<$newlevel) {
$level++;
echo "\n <$UL>\n";
}
while ($level>$newlevel) {
if ($once-->0) { echo "</li>\n"; }
$level--;
echo " </$UL>"
. ($level>0 ? "</li>" : "") . "\n"; // skip for final </ul> (level=0)
}
}
It needs a current $level variable for reference (=$currentDepth). And you pass it your depth as $newlevel. It however needs the first depth to be 1.
Basic usage is like:
$currentDepth=0;
foreach ($array as $_) {
ulli($_["depth"]+1, $currentDepth);
echo "<li>$_[path]";
}
ulli(0, $currentDepth);
Well, quirky. But it worked for me.
Does this code (indentation apart) produces the result you want?
$d = array(
0 => array(
'fullpath' => '../foil/alphanumeric/',
'depth' => 0
),
1 => array(
'fullpath' => '../foil/alphanumeric/letters/',
'depth' => 1
),
2 => array(
'fullpath' => '../foil/alphanumeric/numbers/',
'depth' => 1
),
3 => array(
'fullpath' => '../foil/alphanumeric/numbers/symbols/',
'depth' => 2
)
);
echo "<ul>\n";
$cdepth = 0; $rel = 1; $first = true; $veryfirst = true;
foreach($d as $e)
{
$mpath = "'" . ucfirst(basename($e['fullpath'])) ."'";
if ( $e['depth'] == $cdepth ) {
if ( $first && !$veryfirst) { echo "</li>\n";}
echo "<li rel=\"$rel\">", $mpath;
$rel++; $first = false; $veryfirst = false;
} else {
$depthdiff = $e['depth'] - $cdepth;
if ( $depthdiff < 0 ) {
for($i = 0; $i < -$depthdiff; $i++) {
echo "</ul>\n</li>\n";
}
} else {
for($i = 0; $i < $depthdiff; $i++) {
echo "\n<ul>\n";
$first = true;
// indeed buggy if $depthdiff > 1...
}
}
echo "<li rel=\"$rel\">", $mpath, "\n";
$rel++; $first = true;
}
$cdepth = $e['depth'];
}
for($i = 0; $i < $cdepth; $i++) {
echo "</ul>\n</li>\n";
}
echo "</ul>\n";
EDITED code: Still not perfect but you can work on it... :D

Categories