如何从多维数组中弹出数组?

时间:2017-03-28 07:28:37

标签: php html arrays

实际上我想要从多维数组中弹出第一个数组,并且我的数组具有自由和重量数组,当" weight"数组即将到来我需要弹出" free"数组否则请不要帮助我。

Array (
    [free] => Array (
        [title] => Free Shipping
        [quote] => Array (
            [free] => Array (
                [code] => free.free
                [title] => Free Shipping
                [cost] => 0
                [tax_class_id] => 0
                [text] => Rs 0
            )
        )
        [sort_order] =>
        [error] =>
    )
    [weight] => Array (
        [title] => Shipping Method - Courier Service
        [quote] => Array (
            [weight_6] => Array (
                [code] => weight.weight_6
                [title] => Courier charged - Karnataka/Andra Pradesh/Kerlala (Weight: 0.00g)
                [cost] => 40.00
                [tax_class_id] => 0
                [text] => Rs 40
            )
        )
        [sort_order] => 1
        [error] =>
    )
)

以下是我的代码,

<?php if ($shipping_methods) { ?>
<?php echo "<pre>";
print_r($shipping_methods);
die; ?>
<p><?php echo $text_shipping_method; ?></p>
<?php foreach ($shipping_methods as $shipping_method) { ?>
    <p><strong><?php echo $shipping_method['title']; ?></strong></p>
    <?php if (!$shipping_method['error']) { ?>
        <?php foreach ($shipping_method['quote'] as $quote) { ?>
            <div class="radio">
                <label>
                    <?php if ($quote['code'] == $code || !$code) { ?>
                        <?php $code = $quote['code']; ?>
                        <input type="radio" name="shipping_method" value="<?php echo $quote['code']; ?>" checked="checked" />
                    <?php } else { ?>
                        <input type="radio" name="shipping_method" value="<?php echo $quote['code']; ?>" />
                    <?php } ?>
                    <?php echo $quote['title']; ?> - <?php echo $quote['text']; ?>
                </label>
            </div>
        <?php } ?>
    <?php } else { ?>
        <div class="alert alert-danger"><?php echo $shipping_method['error']; ?></div>
    <?php } ?>
<?php } ?>

1 个答案:

答案 0 :(得分:1)

你可以在多维数组

上使用像这样的array_pop
  $id = array(
  array(123, "sub1"),
  array(321, "sub2"),
);
$result = array_pop($id);
echo $result[0] .' '. $result[1];

输出:

  

321 sub2

Array_pop为您提供数组的最后一个元素。 对于第一个数组,您可以使用array_shift()

  $id = array(
  array(123, "sub1"),
  array(321, "sub2"),
);
$result = array_shift($id);
echo $result[0] .' '. $result[1];

输出

  

123 sub1