Reputation: 11
I have 2 arrays:
$name = Array ( [0] => 2 [1] => 1 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 )
$id = Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )
And I would like to sort by $name
value, but I don't need, where is 0
But when I do this:
foreach ($name as $key => $n) {
if ($n ==0 ){} else{
echo "valami[" . $key . "] = " . $n . "//" .$id[$key]."";
}}
I lost the correct $id[$key]
Please help me!
The $_POST
:
<?php echo "<form name='pdf_form2' action='pdf.php' method='post' enctype='multipart/form-data'>";
$berelhetok = getreclist("SELECT *, count(*) as count FROM rent GROUP BY rent_id ORDER BY rent_id");
$result = mysql_query("SELECT *, count(*) as count FROM rent GROUP BY rent_id ORDER BY rent_id");
$num_rows = mysql_num_rows($result);
foreach($berelhetok as $berel){
echo "<tr>
<td><select name='name[]'>
<option value='0'></option>";
for ($i = 1; $i <= $num_rows; $i++) {
echo "<option value='".$i."'>".$i."</option>";
}
echo "</select></td>
<td align='center'>".$berel[rent_id]."</td>
<INPUT TYPE='hidden' name='id[]' VALUE='".$berel[rent_id]."'>
<td align='center'>".$berel[rent_megnevezes]."</td>
<td align='center'><img src='".$berel[rent_kezdokep]."' width='100px' /></td>";
} ?><INPUT TYPE="hidden" name="menu_id" VALUE="<?=$menu_id;?>">
<tr>
<td align="center" colspan="2"><input type="submit" name="submit" value="Mentés" class="button">
</td>
</tr>
</form>
Upvotes: 1
Views: 77
Reputation: 7762
You can use array_filter to filter those element having value 0
e.g
$name = array ( 0 => 2, 1 => 1, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0 );
$id = array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8, 8 => 9, 9 => 10);
$name = array_filter($name);
foreach ($name as $key => $n) {
echo "valami[" . $key . "] = " . $n . "//" .$id[$key]."";
}
Output:-
valami[0] = 2//1
valami[1] = 1//2
Upvotes: 1