I'm having trouble looping through an object array and displaying data from it under certain conditions.
I want the webpage to display the following:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
I know how to do this by defining $user[0]->first_name . $user[0]->last_name specifically, but what I need is a way for the code to loop through and display the names dynamically using the user_id property as a unique identifier.
For all objects in the array with user_id 1, return "these values." For all the objects in the array with user_id 2, return "these values." And so on...looping through each unique user_id.
Here is a var_dump of the object array:
array(4)
{ [0]=> object(stdClass)#4336 (4)
{
["umeta_id"]=> string(1) "1"
["user_id"]=> string(1) "1"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "John"
}
[1]=> object(stdClass)#4333 (4)
{
["umeta_id"]=> string(1) "2"
["user_id"]=> string(1) "1"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(3) "Doe"
}
[2]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "3"
["user_id"]=> string(1) "2"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "Jane"
}
[3]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "4"
["user_id"]=> string(1) "2"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Smith"
}
[4]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "5"
["user_id"]=> string(1) "3"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(7) "William"
}
[5]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "6"
["user_id"]=> string(1) "3"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Green"
}
}
When I output the information on the webpage, I want it to look like this:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
Maybe I need to loop through objects and build new arrays based on user_ids? Please do not provide code with echo or print_r. Only "return" can be used for this project.
So $your_object is already sorted by umeta_id and user_id fields?
If not, you can use usort:
function cmp1($a, $b)
{
return strcmp($a->umeta_id, $b->umeta_id);
}
function cmp2($a, $b)
{
return strcmp($a->user_id, $b->user_id);
}
usort($your_object, "cmp1");
usort($your_object, "cmp2");
Then simply:
$str = '';
for($i=0;$i<count($your_object);$i+=2){
$str .= "<p>".$your_object[$i]->meta_value." "$your_object[$i+1]->meta_value."</p> ";
}
return $str; // this var contain all your data in string
Hope this helps!
This will generate the first-name and last-name pairs :
$user_group = array();
ksort($user_group, SORT_NUMERIC);
foreach ($users as $key => $item) {
$name_meta = get_object_vars($item);
foreach ($name_meta as $meta_in => $names) {
if ($names == 'first_name') {
$user_group[$item->user_id]['first_name'] = $name_meta['meta_value'];
}
if ($names == 'last_name') {
$user_group[$item->user_id]['last_name'] = $name_meta['meta_value'];
}
}
}
Output :
array(3) {
[1]=>
array(2) {
["first_name"]=>
string(4) "John"
["last_name"]=>
string(3) "Doe"
}
[2]=>
array(2) {
["first_name"]=>
string(4) "Jane"
["last_name"]=>
string(5) "Smith"
}
[3]=>
array(2) {
["first_name"]=>
string(7) "William"
["last_name"]=>
string(5) "Green"
}
}
And to print the user name pairs :
foreach ($user_group as $name) {
echo "<p>" . $name['first_name'] . " " . $name['last_name'] . "</p>";
}
Hi I have a array with lets say 3 arrays with another few arrays inside those like this:
array(3) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(1) "1"
["disease_mbr_5"]=>
string(4) "1234"
["disease_mtr_5"]=>
string(2) "12"
["disease_cpe_14"]=>
string(1) "1"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "4"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "4"
["disease_mbr_16"]=>
string(2) "21"
["disease_mtr_16"]=>
string(3) "122"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "1"
["disease_mbr_4"]=>
string(1) "1"
["disease_mtr_4"]=>
string(1) "9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
}
}
[1]=>
array(3) {
["disease_id"]=>
array(8) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(1) "8"
[6]=>
string(2) "15"
[7]=>
string(1) "4"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
}
["params"]=>
object(stdClass)#235 (27) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "1"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "1"
["disease_mbr_16"]=>
string(1) "5"
["disease_mtr_16"]=>
string(1) "6"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
}
}
[2]=>
array(3) {
["disease_id"]=>
array(6) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#184 (24) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
Now I need to merge these arrays in to one and where the values match up in the ["disease_id"] and ["risk_id"] sub array I should delete the duplicate, yet when the keys match up in the ["params"] object I need to keep both values and yet dump the duplicated key in the object, so that this is kind of the result:
array(1) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
[13]=>
string(1) "8"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(7) "1, 1, 2"
["disease_mbr_5"]=>
string(10) "1234, 1, 1"
["disease_mtr_5"]=>
string(8) "12, 1, 1"
["disease_cpe_14"]=>
string(7) "1, 2, 2"
["disease_mbr_14"]=>
string(7) "2, 2, 2"
["disease_mtr_14"]=>
string(7) "2, 2, 2"
["disease_cpe_12"]=>
string(10) "12, 12, 12"
["disease_mbr_12"]=>
string(10) "12, 12, 12"
["disease_mtr_12"]=>
string(10) "12, 12, 12"
["disease_cpe_17"]=>
string(4) "4, 1"
["disease_mbr_17"]=>
string(4) "1, 1"
["disease_mtr_17"]=>
string(4) "1, 1"
["disease_cpe_16"]=>
string(4) "4, 1"
["disease_mbr_16"]=>
string(5) "21, 5"
["disease_mtr_16"]=>
string(3) "122,6"
["disease_cpe_8"]=>
string(6) "11, 11"
["disease_mbr_8"]=>
string(1) "1, 1"
["disease_mtr_8"]=>
string(1) "1, 1"
["disease_cpe_15"]=>
string(13) "132, 132, 132"
["disease_mbr_15"]=>
string(13) "132, 132, 132"
["disease_mtr_15"]=>
string(7) "1, 1, 1"
["disease_cpe_4"]=>
string(7) "1, 7, 7"
["disease_mbr_4"]=>
string(7) "1, 8, 8"
["disease_mtr_4"]=>
string(7) "9, 9, 9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
["risk_cpe_4"]=>
string(1) "1, 1"
["risk_mbr_4"]=>
string(1) "2, 2"
["risk_mtr_4"]=>
string(1) "3, 3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
I have tried many thing but have not found an answer that effectively solves this issue. Here is one of my attempts:
$newArray = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
foreach ($value["disease_id"] as $pointer => $disease){
if (in_array($disease, $value["disease_id"])){
$newArray["disease_id"][] = $selected[$key]["disease_id"][$pointer];
unset($selected[$key]["disease_id"][$pointer]);
$cpe = "disease_cpe_".$disease;
$mbr = "disease_mbr_".$disease;
$mtr = "disease_mtr_".$disease;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
foreach ($value["risk_id"] as $pointer => $risk){
if (in_array($risk, $value["risk_id"])){
$newArray["risk_id"][] = $selected[$key]["risk_id"][$pointer];
unset($selected[$key]["risk_id"][$pointer]);
$cpe = "risk_cpe_".$risk;
$mbr = "risk_mbr_".$risk;
$mtr = "risk_mtr_".$risk;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
}
}
Here is another attempt:
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
}
$newArray["disease_id"] = array_unique($newArray["disease_id"]);
$newArray["risk_id"] = array_unique($newArray["risk_id"]);
}
}
out putt was:
array(3) {
["disease_id"]=>
array(14) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
[21]=>
string(2) "17"
[22]=>
string(2) "16"
[39]=>
string(1) "3"
[40]=>
string(2) "18"
[41]=>
string(1) "9"
[42]=>
string(1) "2"
[43]=>
string(2) "20"
[44]=>
string(1) "1"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["parpams"]=>
object(stdClass)#236 (0) {
}
}
But the ['params'] are still not included.
I have read many treads on stackoverflow, but non actuality address this complexity. If I missed a thread please point me in that direction. Please do remember that these arrays can become as much as twenty arrays. Tanks!
I think this is the answer if it can be of any help to any one else.
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
$newArray["params"] = array();
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
if(is_object($value["params"])){
$newArray["params"] = array_merge_recursive((array) $value["params"], (array) $newArray["params"]);
}
}
$newArray_risk = array_unique($newArray["risk_id"]);
if(sort($newArray_risk)){
$newArray["risk_id"]= array();
foreach ($newArray_risk as $risk_id){
$newArray["risk_id"][] = $risk_id;
}
}
$newArray_disease = array_unique($newArray["disease_id"]);
if(sort($newArray_disease)){
$newArray["disease_id"] = array();
foreach ($newArray_disease as $disease_id){
$newArray["disease_id"][] = $disease_id;
}
}
foreach ($newArray["params"] as $key => $value){
if (is_array($value)){
$i=0;
foreach ($value as $val){
if (!is_array($val)){
if ($i == 0){
$newArray["params"][$key] = $val;
} else {
$newArray["params"][$key] .= ', '.$val;
}
$i++;
}
}
}
}
$newArray["params"] = (object)$newArray["params"];
}
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));
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--;
}
i got an array like this:
array(28) {
[0]=> array(8) { ["jornada"]=> string(1) "1" ["team1"]=> string(12) "LEGANES F.C." ["team2"]=> string(9) "LAS CONOS" }
[1]=> array(8) { ["jornada"]=> string(1) "1" ["team1"]=> string(13) "BABACHAS F.C." ["team2"]=> string(19) "QUIRQUINCHAS VERDES" }
[2]=> array(8) { ["jornada"]=> string(1) "2" ["team1"]=> string(7) "TSU CHU" ["team2"]=> string(12) "SANTINO F.C." }
[3]=> array(8) { ["jornada"]=> string(1) "2" ["team1"]=> string(11) "LIBERO F.C." ["team2"]=> string(9) "LAS PUMAS" }
[4]=> array(8) { ["jornada"]=> string(1) "3" ["team1"]=> string(6) "PUCARA" ["team2"]=> string(15) "EL REJUNTE F.C." }
my question is, how can i get the differents "jornada's" values? because i have to make a fixture grouping by "jornada" something like :
jornada 1 :
LEGANES vs LAS CONOS
BABACHAS vs QUIRQUINCHAS
jornada 2:
TSU CHU vs SANTINO
LIBERO vs LAS PUMAS
thanks for your help and sorry about my english.
you could re-arrange them into a new array that has this grouping...
$grouped = array();
foreach ($data as $row) {
$grouped[$row['jornada']][] = $row;
}
now you can do:
foreach ($grouped as $jorndada => $matches) {
echo "Jornada $jornada\n";
foreach ($matches as $match) {
echo $match["team1"] ." vs ". $match["team2"] . PHP_EOL;
}
}