使用php显示精确的数组元素

I am able to display all the array elements using the code below but I would like to know how can I display several array elements only (for example 4th, 6th and 11th). Could you please help me.

$rates = $data->Rates->ExchangeRate;

    if (is_array($rates) && count($rates) > 0) {
        echo '<table><tr><th>ISO</th><th>Rate</th></tr>';

        foreach ($rates as $rate) {
            echo '<tr>';

            echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td></tr>';

            echo '</tr>';
        }

        echo '</table>';
    }

Just make an array of allowable key values and everytime check if the current key is included:

$rates = $data->Rates->ExchangeRate;
$allow = array(4,6,11);
    if (is_array($rates) && count($rates) > 0) {
        echo '<table><tr><th>ISO</th><th>Rate</th></tr>';

        foreach ($rates as $key => $rate) {
            if(!in_array($key, $allow){
                continue;//if not allowed, go to next iteration
            }
            echo '<tr>';

            echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td></tr>';

            echo '</tr>';
        }

        echo '</table>';
    }

You could filter the array in any number of ways before iterating over it, or you could use a condition inside the loop

$rates = $data->Rates->ExchangeRate;

if (is_array($rates) && count($rates) > 0) {
    echo '<table><tr><th>ISO</th><th>Rate</th></tr>';

    $count = 0;
    $show  = array(4, 8, 11);

    foreach ($rates as $rate) {

        if ( in_array($count++, $show) ) {
            echo '<tr>';
            echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td>';
            echo '</tr>';
        }

    }

    echo '</table>';
}

Other answers have given solutions but i think are slower than one posted here (use an array of included indices i.e $included_indices = array(4,6,11);):

$rates = $data->Rates->ExchangeRate;

if (is_array($rates) && count($rates) > 0) {
    echo '<table><tr><th>ISO</th><th>Rate</th></tr>';

    foreach ($included_indices as $included_indice) {
        $rate = $rates[$included_indice];
        echo '<tr>';

        echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td></tr>';

        echo '</tr>';
    }

    echo '</table>';
}