Home > OS >  Fetch a specific item out of a array
Fetch a specific item out of a array

Time:12-13

I'm trying to fetch and echo a specific item out of an array loop, current code is as follows.

<?php if(sizeof($post->car_booking_extra) == 0): ?>
N/A
<?php else: ?>
<?php echo implode(array_column($post->car_booking_extra, 'name'), '<br />'); ?>
<?php endif; ?>

Those echos out the booking extras such as

Baby Seat
Charging
Etc.

What I'm trying to do is find a specific item in that loop, for example, "Charging" and then display another text.

Something in this direction.

<?php if(array_column($post->car_booking_extra, 'name') == "Charging" ); ?>
DISPLAY THIS
<?php else: ?>
<?php endif; ?>

I'm not very familiar with best practice on this, all i tried such as the above example gives me errors, anyone have an idea what i could try?.

Tried as in the second example above, all i get are errors.

CodePudding user response:

You can use in_array()

<?php if(in_array('Charging',array_column($post->car_booking_extra, 'name'))); ?>
DISPLAY THIS
<?php else: ?>
<?php endif; ?>

Note: Make sure that column has values in uppercase as you want to check uppercase value

https://3v4l.org/lLkqk

  •  Tags:  
  • php
  • Related