所以我在我的alliesArray上运行了一个print_r,结果得出了这个结果。现在问题是我在试图找出我的foreach应该如何看的问题,因为这个数组里面不会有值。我想要它做的是,如果有任何没有值,那么甚至不包括在数组中,它只能回显索引。有什么建议?
Array ( [0] => Ryu Satoshi [1] => Oriel [2] => \"The Ladies Man\" Luscious Landon [3] => [4] => )
function getCharacterAllies($character_id)
{
$testArray = array();
$this->db->select('ally1_id, ally2_id, ally3_id, ally4_id, ally5_id');
$this->db->from('characters_bio_allies');
$this->db->where('characters_id', $character_id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$row = $query->row();
$ids = array(
$row->ally1_id,
$row->ally2_id,
$row->ally3_id,
$row->ally4_id,
$row->ally5_id
);
foreach ($ids as $id)
{
if($id !== '0')
{
$this->db->select('character_name');
$this->db->from('characters');
$this->db->where('id', $id);
$query = $this->db->get();
$row = $query->row();
$testArray[] = $row->character_name;
}
else
{
$testArray[] = "";
}
}
}
else
{
for ($x = 0; $x < 5; $x++)
{
$testArray[] = "";
}
}
return $testArray;
}
foreach ($alliesArray as $row)
{
echo "<li>".$row['character_name']."</li>";
}
答案 0 :(得分:1)
试试这个:
foreach ($alliesArray as $key => $row) {
if (!empty($alliesArray[$key])) {
echo '<li>' . $row['character_name'] . '</li>';
}
}