简体   繁体   中英

How do I loop PHP array in array?

I have this JavaScript array that I can loop:

let events = [{
      id: '2',
      eventName: 'ones',
      sessions: [{
        id: '1245',
        status: false
      }, {
        id: '2125',
        status: true
      }]
}];

Im accessing each event like this and setting it into a new row of a table:

// $tour_events its the array of events
<?php foreach ($events as $key => $event_prop) { ?>
<table class="table">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">NAME</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <?php echo $event_prop->id; ?>
            </td>
            <td>
                <?php echo $event_prop->eventName; ?>
            </td>

I want to access the data inside of sessions to create another table, I have tried this but it's not working:

( Insite the other foreach() )

<?php foreach ($events['sessions'] as $key2 => $value) { ?>
    <table class="table">
        <thead>
            <tr>
                <th scope="col">SESSION ID</th>
                <th scope="col">SESSION STATUS</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <?php echo $value->id; ?>
                </td>
                <td>
                    <?php echo $value->status; ?>
                </td>

sessions is a property the same as id or eventName , so you access it the same way

<?php foreach ($event_prop->sessions as $key2 => $value) { ?>

Everything else stays the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM