Insert all needed unique <td> values in one array

Hello!
I have html table that are made with foreach.
So what i need is to get all unique values in one array!

I get multiple arrays (seems one array for each line)

what am i doing wrong?
As i have big code will try to make one little just for this

$age is coming from database and is holding also array key example 01

//names array
$names = array(
	"18" => "John",
	"17" => "Mark",
	"19" => "Tod",
);
echo '<table><tr><th>name<th>age</tr>';
$commaList=array();
foreach($names as $key => $value){ //getting all names

    foreach($commaList as $val) { //gets only names where age is 18+

           $bzzzzz .= $val.',';	   //i put the names in comma separated list
    }					
					
					
    foreach(explode(',', $bzzzzz) as $valz){ // here i check if user form  comma separated list == $value

	    if($value==$valz){
		
		    echo'<td>'.$value; if so i echo name
                    echo '<td>'.$age;
	    }						
    }


if($age>18){ // here i check if 18+ add to array to show it in <td> $value ^
array_push($commaList,$value);	
}

}

$commalist is an empty array so the foreach loop will never be implemented.

i need to remove $commaList=array(); ?
nope. that just gives an error. …

No doubt the error is due to $bzzzz not being declared?

I noticed you updated your original post and added age.

Can you give an example of the results expected?

at the end there should be 2 rows lets say with John and Tod
them both should go to the array so l could show table just with John and Tod

I want to make an array of people that 18+ in this case

Also declaring inside does not help it just doesnt show any names.
And if i declare it before first foreach nothing changes, still missing one record

and what about Tod? Where are your filtering this value? $age is undefined in your example. i don’t see what this concat/explode should do. your table cells are broken and not even within a row. give an example of the expected results. comment your code.

There are numerous mistakes with the supplied script.

Please use the following as a base and explain your further requirements:

<?php
$names = array(
  "John" => 21,
  "Mark" => 42,
  "Todd" => 55,
);
// PHP HereDoc is so much simpler :)
$hdr = <<< ____TMP
  <table>
    <tr>
      <th> name </th>
      <th> age  </th>
    </tr>
____TMP;
echo $hdr;

$commaList = array();
foreach($names as $name => $age)
{
  $line = <<< ____TMP
    <tr>
      <td> $name </td>
      <td> $age  </td>
    </tr>
____TMP;
echo $line;

  #if( 0 && $aValue[1] > 18 )
  {
    #array_push($commaList,$value);  
  }

}
echo '</table></pre>'
?>

I edited my post please check!I m just a bad in explain :sweat:

Hey thanks for base code but age is not inside names array

i don’t see any relevant change in the code. i don’t see any answer on what i said. i don’t see any use of the provided codebase. i don’t see any problem description for the new code.

What do you actually want to end up with at the end of the routine? You go into it with an array of names and ages, and a variable called $age that comes from your database. If you just want to end up with an array of names and ages where the age is greater than the value of $age, surely that’s simply

$oldpeople = array();
foreach ($names as $key => $name) {
  if ($key > $age) {
    $oldpeople[] = array($key => $name);
    }
  }

I must be misunderstanding the requirement.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.