Reputation: 1929
So I ran a a print_r on my alliesArray and as a result came up with this. Now the problem here is I'm having an issue with trying to figure out how my foreach should look because there won't be values all the time inside this array. What I would like it to do though is if there's any that don't have values then don't even include it in the array that way it can only echo out the indexs. Any suggestions?
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>";
}
Upvotes: 0
Views: 67
Reputation: 29991
Try this:
foreach ($alliesArray as $key => $row) {
if (!empty($alliesArray[$key])) {
echo '<li>' . $row['character_name'] . '</li>';
}
}
Upvotes: 1