Reputation: 7094
I have a multidimensional array stored in $t_comments
that looks something like this:
Array (
[0] => Array
(
[0] => 889
[1] => First comment
[2] => 8128912812
[3] => approved
)
[1] => Array
(
[0] => 201
[1] => This is the second comment
[2] => 333333
[3] => deleted
)
// There is more...
)
Currently, I loop through the array like this:
foreach($t_comments as $t_comment) {
echo $t_comment[0]; // id
echo $t_comment[1]; // comment
echo $t_comment[2]; // timestamp
echo $t_comment[3]; // status
}
I want to foreach loop through the array and only display arrays that have the value approved
(as seen above). How do I do this when having performance in consideration?
Upvotes: 0
Views: 76
Reputation: 149
The best possible solution is to not have to check in the first place.
If you controlled the source that inflate the $t_comment array you could make it not send the comments that are not approved.
For example, if you have something like:
$t_comments = get_comments
You could use something like:
$t_comments = get_approved_comments
But if you can't then you will have to iterate through each array looking for what you want.
To do so you have to put an "if" inside your foreach to check your variable content so you know it's approved and then you know you can show it, echoing it.
Example:
foreach($t_comments as $t_comment) {
if($t_comment[3]=="approved"){
echo $t_comment[0];
echo $t_comment[1];
echo $t_comment[2];
echo $t_comment[3];
}
}
Upvotes: 1