Cameron Mark Lewis Cameron Mark Lewis - 5 years ago 185
PHP Question

PHP Hide text from array

I'm pulling data from an array, but show certain parts of the data above others, I've managed to choose certain text to go above others. Is there anyway to hide text from the array if it matches the piece of text I give it, using the below code?

<?php $datas = array_slice($facilities, 0, $leftoshownumber); // First 6 items
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>


This is the full code for this section

<div class="checkmark-outer">

<?php $startnumber = 6; $parkingnumber = 0; $gardennumber = 0; $dishwashernumber = 0; $summerrentnumber = 0; ?>

<?php if (strpos($facilitiesstring, 'Reduced Summer') !== false) { $summerrentnumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Reduced Summer Rent Available</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Parking') !== false) {$parkingnumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Parking Available</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Garden') !== false) {$gardennumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Garden</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Dishwasher') !== false) {$dishwashernumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Dishwasher</div></div>';}; ?>

<?php $totalfixednumber = $parkingnumber + $gardennumber + $dishwashernumber + $summerrentnumber; $leftoshownumber = $startnumber - $totalfixednumber;?>

<?php $datas = array_slice($facilities, 0, $leftoshownumber); // First 6 items
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>

<div class="clearfix"></div>
<div class="read-more-show hide">Show More</div><div class="read-more-content"><?php $datas = array_slice($facilities, 6); // Items after 6
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?><div class="clearfix"></div><div class="read-more-hide hide">Show Less</div></span></div>
</div>


Any help would be great!

Answer Source

Do you mean you don't want to print the whole line, when the name matches a given variable? Then I would do:

$iDontWant="bla"

foreach($datas as $data) {
            if($data->Name == $iDontWant) continue; 
            echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
        }; ?>

EDIT:

if you want multiple names not to show you could do:

$iDontWant1="bla1"
$iDontWant2="bla2"
$iDontWant3="bla3"

foreach($datas as $data) {
        if($data->Name == $iDontWant1 || $data->Name == $iDontWant2 || $data->Name == $iDontWant3) continue; 
        echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
    }; ?>

('||' stands for 'or' by the way)

So basically what you are doing is saying, when

either the name is the same as the content of $iDontWant1, which is "bla1"

or the name is the same as $iDontWant2, which is "bla2"

or the name is the same as the content of $iDontWant3, which is "bla3"

do continue (don't print this part).

However if you know what "bla1", "bla2" ... are, it is easier to just do

 foreach($datas as $data) {
        if($data->Name == "bla1" || $data->Name == "bla2" || $data->Name == "bla3") continue; 
        echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
    }; ?>

You will just compare to the String directly instead of the variable containing the string.

I hope I can help you with this.

But if you'd like to do more stuff with PHP I'd recommend a tutorial like https://secure.php.net/manual/en/tutorial.php to really understand the concepts behind this language

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download