PHP function putting incorrect <ul><li> in tree hierarchy - php

I am using this function to display UL list:
function getList($arResult){
$diff = 0;
foreach($arResult as $result){
if($lastlevel != $result[depth]){
if($lastlevel < $result[depth]){
$html .= "<ul>\n";
$diff++;
}
else {
$html .= "</li>\n</ul>\n</li>\n";
$diff--;
}
}
else
$html .= "</li>\n";
$html .= "<li>$result[cat_name]";
$lastlevel = $result[depth];
}
$html .= str_repeat("</li>\n</ul>\n", $diff);
return $html;
}
The $arResult contains a tree like this:
Electronics
TV
Video
Old Movies
New Movies
Hollywood
Old hollywood
New HollyWood
Rotten Hollywood
Other Movies
Mobile
Nokia
Apple
Micromax
Laptop
Other
The above function prints everything under Electronics->Tv while the truth is that under Electronics there should be TV, Video, Mobile Laptop, Other ..
This function somehow is putting ul li in incorrect mode.
The Array Dump is here:
array(10) {
[0]=>
array(6) {
["cat_ID"]=>
string(3) "197"
["cat_name"]=>
string(13) "Student Corner"
["cat_nicename"]=>
string(13) "student-corder"
["parent"]=>
string(1) "0"
["post_count"]=>
string(1) "0"
["depth"]=>
string(1) "0"
}
[1]=>
array(6) {
["cat_ID"]=>
string(3) "198"
["cat_name"]=>
string(6) "GujCET"
["cat_nicename"]=>
string(13) "gujcet-gujrat"
["parent"]=>
string(3) "197"
["post_count"]=>
string(1) "0"
["depth"]=>
string(1) "1"
}
[2]=>
array(6) {
["cat_ID"]=>
string(3) "199"
["cat_name"]=>
string(13) "Sample Papers"
["cat_nicename"]=>
string(20) "sample-papers-gujcet"
["parent"]=>
string(3) "198"
["post_count"]=>
string(1) "1"
["depth"]=>
string(1) "2"
}
[3]=>
array(6) {
["cat_ID"]=>
string(3) "200"
["cat_name"]=>
string(8) "Syllabus"
["cat_nicename"]=>
string(15) "syllabus-gujcet"
["parent"]=>
string(3) "198"
["post_count"]=>
string(1) "8"
["depth"]=>
string(1) "2"
}
[4]=>
array(6) {
["cat_ID"]=>
string(3) "201"
["cat_name"]=>
string(4) "News"
["cat_nicename"]=>
string(11) "news-gujrat"
["parent"]=>
string(3) "197"
["post_count"]=>
string(1) "1"
["depth"]=>
string(1) "1"
}
[5]=>
array(6) {
["cat_ID"]=>
string(3) "202"
["cat_name"]=>
string(13) "Question Bank"
["cat_nicename"]=>
string(20) "question-bank-gujrat"
["parent"]=>
string(3) "197"
["post_count"]=>
string(1) "0"
["depth"]=>
string(1) "1"
}
[6]=>
array(6) {
["cat_ID"]=>
string(3) "203"
["cat_name"]=>
string(7) "Class X"
["cat_nicename"]=>
string(28) "class-x-gujrat-question-bank"
["parent"]=>
string(3) "202"
["post_count"]=>
string(1) "2"
["depth"]=>
string(1) "2"
}
[7]=>
array(6) {
["cat_ID"]=>
string(3) "204"
["cat_name"]=>
string(9) "Class XII"
["cat_nicename"]=>
string(30) "class-xii-gujrat-question-bank"
["parent"]=>
string(3) "202"
["post_count"]=>
string(1) "4"
["depth"]=>
string(1) "2"
}
[8]=>
array(6) {
["cat_ID"]=>
string(3) "205"
["cat_name"]=>
string(7) "Results"
["cat_nicename"]=>
string(15) "results-gujarat"
["parent"]=>
string(3) "197"
["post_count"]=>
string(1) "1"
["depth"]=>
string(1) "1"
}
[9]=>
array(6) {
["cat_ID"]=>
string(3) "206"
["cat_name"]=>
string(10) "About GSEB"
["cat_nicename"]=>
string(10) "about-gseb"
["parent"]=>
string(3) "197"
["post_count"]=>
string(1) "1"
["depth"]=>
string(1) "1"
}
}

Way too complicated a function. Something along these lines does it:
function treeList(array $data) {
$list = '<ul>';
foreach ($data as $item) {
$list .= '<li>';
$list .= $item['name'];
if (!empty($item['children'])) {
$list .= treeList($item['children']);
}
$list .= '</li>';
}
$list .= '</ul>';
return $list;
}
$data = array(
array(
'name' => 'Foo',
'children' => array(
array(
'name' => 'Bar'
)
)
)
);
echo treeList($data);

You are not opeaning the <li> tags you are closing it here,
fix this and try,
else {
$html .= "</li>\n</ul>\n</li>\n";
$diff--;
}

Related

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

PHP array and string

I have an array containing dates (year) and position.
I need to build a string out of it, where:
Every position is separated by a / if it's in a different year ; and a - must appear if there was no result on a year. And if we have a -, then there is no need to use the / to separate the years ...
I'm struggling to build a logic and code for it.
Example:
array(7) {
[0]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "2" }
[1]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "4" }
[2]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "5" }
[3]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "5" }
[4]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> int(0) }
[5]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "2" }
[6]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "3" } }
Should show: 32/05542
And
array(7) {
[0]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "2" }
[1]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "4" }
[2]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "5" }
[3]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "5" }
[4]=> array(2) {
["year"]=> string(4) "2013"
["hformpos"]=> int(0) }
[5]=> array(2) {
["year"]=> string(4) "2011"
["hformpos"]=> string(1) "2" }
[6]=> array(2) {
["year"]=> string(4) "2011"
["hformpos"]=> string(1) "3" } }
32-5542-
Thank you!
I am assuming that the array is sorted in the descending order of the dates(year).
$year_flag = $arrays[0]["year"];
$result = "";
foreach( $arrays as $array )
{
if($year_flag == $array["year"]){
if( $array["hformpos"] !==0){
$result.= $array["hformpos"];
}else {
$result.= "-";
}
}elseif( $array["hformpos"] ===0){
$result.= "-";
}else{
$result.= "/";
if( $array["hformpos"] !==0){
$result.= $array["hformpos"];
}else {
$result.= "-";
}
}
$year_flag = $array["year"];
}

Getting data out of an array

What would be my best option to get the data out of this array?
array(4) {
[0]=> array(10) {
["id"]=> string(3) "158"
["name"]=> string(8) "Tractors"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "37"
["blurb"]=> string(17) "Agrilife Tractors"
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "1"
["sorder"]=> string(1) "0"
["state"]=> string(1) "1"
}
[1]=> array(10) {
["id"]=> string(3) "159"
["name"]=> string(8) "Ride Ons"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "74"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "2"
["sorder"]=> string(1) "1"
["state"]=> string(1) "1"
}
[2]=> array(10) {
["id"]=> string(3) "160"
["name"]=> string(9) "Machinery"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "14"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "3"
["sorder"]=> string(1) "2"
["state"]=> string(1) "1"
}
[3]=> array(10) {
["id"]=> string(3) "161"
["name"]=> string(17) "Outdoor Equipment"
["parent_id"]=> string(1) "0"
["image_id"]=> string(3) "114"
["blurb"]=> NULL
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "5"
["sorder"]=> string(1) "3"
["state"]=> string(1) "1"
}
}
Tractors
My HTML looks like this I am trying to foreach to get all of the relevant data out so I can echo when or where needed.
HTML:
foreach($assoc_categories as $assoc_cat)
{
// Page load - does assoc exist?
$checked_state = "";
$does_assoc_exist = $this->Ps_products_model->brand_specific_cat_assoc_exist($brand_id, $assoc_cat['id']);
if($does_assoc_exist == "1")
{
$checked_state = " checked='checked'";
}
?>
<div>
<input type="checkbox" name="product_category" class="product_category_selector" id="product_category_<?php echo $assoc_cat['id']; ?>" data-id="<?php echo $assoc_cat['id']; ?>" <?php echo $checked_state; ?> /> <?php echo $assoc_cat['name']; ?>
</div>
<input class="order" type="input" />
<?php
}
?>
To dump all the values, you'd need nested foreach like this:
foreach ($original_array as $sub_array) {
foreach ($sub_array as $key=>$value) {
echo $key.' '.$value.'<br>';
}
}
To get just one value, you need to access it with its address. It may not be set, so check first:
foreach ($original_array as $sub_array) {
// Say you want all the `name`s
if (isset($sub_array['name'])) {
echo $sub_array['name'].'<br>';
}
}

getting a unique values from array

I currently have an array that looks like the one below but I only want to display show results in the array from which are unique (description), I presume with array_unique? but I keep getting the same results?
Here is my array:
$taglist = array(5) {
[0]=> array(5) {
["id"]=> string(2) "27"
["page_id"]=> string(2) "18"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
[1]=> array(5) {
["id"]=> string(2) "29"
["page_id"]=> string(2) "18"
["description"]=> string(3) "Tutorials"
["slug"]=> string(3) "tutorials"
["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
["id"]=> string(2) "31"
["page_id"]=> string(2) "21"
["description"]=> string(3) "tag"
["slug"]=> string(3) "tag"
["visibility"]=> string(7) "visible"
}
[3]=> array(5) {
["id"]=> string(2) "32"
["page_id"]=> string(2) "21"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
}
Here is my while:
$items = array();
$results = $taglist;
foreach ($results as $result)
{
$items[]= $result['description'];
$items = array_unique($items);
}
echo '<ul>';
while ($tag_item = current($items))
{
echo '<li>'.$tag_item['description'].'</li>';
next($items);
}
echo '</ul>';
$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);
// echo var_export($taglist, 1); // uncomment to see the diff vs var_dump()
foreach($taglist as $tag){
$new[] = $tag['description'];
}
var_dump(array_unique($new));

PHP Group By Day

Given this array:
array(1) {
[0]=>
array(2) {
["Project"]=>
array(5) {
["id"]=>
string(1) "2"
["user_id"]=>
string(2) "21"
["customer_id"]=>
string(1) "4"
["name"]=>
string(15) "WordPress Theme"
["created"]=>
string(19) "2011-09-26 21:30:38"
}
["Track"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "7"
["user_id"]=>
string(2) "21"
["project"]=>
string(1) "2"
["customer"]=>
string(1) "4"
["title"]=>
string(7) "Backend"
["notes"]=>
string(0) ""
["created"]=>
string(19) "2011-09-28 22:21:22"
["Lapse"]=>
array(2) {
[0]=>
array(5) {
["id"]=>
string(1) "4"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:21"
["stop"]=>
string(19) "2011-09-28 22:22:30"
["created"]=>
string(19) "2011-09-28 22:22:21"
}
[1]=>
array(5) {
["id"]=>
string(1) "3"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:07"
["stop"]=>
string(19) "2011-09-28 22:22:12"
["created"]=>
string(19) "2011-09-28 22:22:07"
}
}
}
}
}
}
How would i group by Day in the Lapse Array with PHP? This may have been easier to do directly with MySQL but i'm using CakePHP's recursive function and i cant figure out how to use Group By with that!
$list = array();
function extractByDates($arr) {
foreach ($arr as $key => $v)
if (is_array($v))
function extractByDates($v);
else if ($key == 'created')
$list[$v] = $v;
}
extractByDates($yourGivenArray);
I not tested!

Categories