如何从数组中获取字符串中的值

时间:2015-01-28 05:11:34

标签: php arrays

我有一个数组:

Array
(
    [0] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9276
            [post_id] => 905
            [meta_key] => _billing_email
            [meta_value] => code4webtest@gmail.com
        )

    [1] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9275
            [post_id] => 905
            [meta_key] => _billing_postcode
            [meta_value] => 94025
        )

    [2] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9274
            [post_id] => 905
            [meta_key] => _billing_state
            [meta_value] => CA
        )

    [3] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9273
            [post_id] => 905
            [meta_key] => _billing_city
            [meta_value] => Menlo Park
        )

    [4] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9271
            [post_id] => 905
            [meta_key] => _billing_address_1
            [meta_value] => 3723 Heaven Avenue
        )

    [5] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9272
            [post_id] => 905
            [meta_key] => _billing_address_2
            [meta_value] => Suite 119
        )

    [6] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9302
            [post_id] => 905
            [meta_key] => _completed_date
            [meta_value] => 2015-01-27 11:41:07
        )

    [7] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9267
            [post_id] => 905
            [meta_key] => _billing_country
            [meta_value] => US
        )

    [8] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9268
            [post_id] => 905
            [meta_key] => _billing_first_name
            [meta_value] => Prem
        )

    [9] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9269
            [post_id] => 905
            [meta_key] => _billing_last_name
            [meta_value] => Tiwari
        )

    [10] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9270
            [post_id] => 905
            [meta_key] => _billing_company
            [meta_value] => trantor
        )

    [11] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9264
            [post_id] => 905
            [meta_key] => _customer_ip_address
            [meta_value] => 192.168.177.60
        )

    [12] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9265
            [post_id] => 905
            [meta_key] => _customer_user_agent
            [meta_value] => Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
        )

    [13] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9266
            [post_id] => 905
            [meta_key] => _customer_user
            [meta_value] => 2
        )

    [14] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9263
            [post_id] => 905
            [meta_key] => _prices_include_tax
            [meta_value] => no
        )

    [15] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9262
            [post_id] => 905
            [meta_key] => _order_currency
            [meta_value] => SGD
        )

    [16] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9261
            [post_id] => 905
            [meta_key] => _order_key
            [meta_value] => wc_order_54c74375c9c0d
        )

    [17] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9299
            [post_id] => 905
            [meta_key] => _edit_last
            [meta_value] => 1
        )

    [18] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9300
            [post_id] => 905
            [meta_key] => _transaction_id
            [meta_value] => 
        )

    [19] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9301
            [post_id] => 905
            [meta_key] => slide_template
            [meta_value] => default
        )

    [20] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9298
            [post_id] => 905
            [meta_key] => _edit_lock
            [meta_value] => 1422358750:1
        )

    [21] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9297
            [post_id] => 905
            [meta_key] => _recorded_coupon_usage_counts
            [meta_value] => yes
        )

    [22] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9296
            [post_id] => 905
            [meta_key] => _recorded_sales
            [meta_value] => yes
        )

    [23] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9295
            [post_id] => 905
            [meta_key] => _download_permissions_granted
            [meta_value] => 1
        )

    [24] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9294
            [post_id] => 905
            [meta_key] => _order_total
            [meta_value] => 10.00
        )

    [25] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9293
            [post_id] => 905
            [meta_key] => _order_shipping_tax
            [meta_value] => 0
        )

    [26] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9292
            [post_id] => 905
            [meta_key] => _order_tax
            [meta_value] => 0
        )

    [27] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9291
            [post_id] => 905
            [meta_key] => _cart_discount
            [meta_value] => 0
        )

    [28] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9290
            [post_id] => 905
            [meta_key] => _order_discount
            [meta_value] => 0
        )

    [29] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9289
            [post_id] => 905
            [meta_key] => _order_shipping
            [meta_value] => 0
        )

    [30] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9288
            [post_id] => 905
            [meta_key] => _payment_method_title
            [meta_value] => Pay Using Your Account Balance
        )

    [31] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9287
            [post_id] => 905
            [meta_key] => _payment_method
            [meta_value] => cod
        )

    [32] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9286
            [post_id] => 905
            [meta_key] => _shipping_postcode
            [meta_value] => 94025
        )

    [33] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9285
            [post_id] => 905
            [meta_key] => _shipping_state
            [meta_value] => CA
        )

    [34] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9284
            [post_id] => 905
            [meta_key] => _shipping_city
            [meta_value] => Menlo Park
        )

    [35] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9283
            [post_id] => 905
            [meta_key] => _shipping_address_2
            [meta_value] => Suite 119
        )

    [36] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9282
            [post_id] => 905
            [meta_key] => _shipping_address_1
            [meta_value] => 3723 Heaven Avenue
        )

    [37] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9281
            [post_id] => 905
            [meta_key] => _shipping_company
            [meta_value] => trantor
        )

    [38] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9280
            [post_id] => 905
            [meta_key] => _shipping_last_name
            [meta_value] => Tiwari
        )

    [39] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9279
            [post_id] => 905
            [meta_key] => _shipping_first_name
            [meta_value] => Prem
        )

    [40] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9278
            [post_id] => 905
            [meta_key] => _shipping_country
            [meta_value] => US
        )

    [41] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9277
            [post_id] => 905
            [meta_key] => _billing_phone
            [meta_value] => (650) 777 5480
        )

)

但我需要上面数组中的帐单邮寄地址和电子邮件ID。我怎样才能得到。

提前致谢...

2 个答案:

答案 0 :(得分:2)

您可以在vanilla-PHP中使用array_filter

$filter = array_filter($array, function ($element) {
    return in_array($element['meta_key'], array('_billing_email', '_billing_address_1'));
});

或者您可以使用库ouzo-goodies

$filter = Arrays::filter($array, function ($element) {
    return in_array($element['meta_key'], array('_billing_email', '_billing_address_1'));
});

结果:

Array
(
    [0] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9276
            [post_id] => 905
            [meta_key] => _billing_email
            [meta_value] => code4webtest@gmail.com
        )

    [2] => Array
        (
            [order_item_id] => 2
            [meta_id] => 9276
            [post_id] => 905
            [meta_key] => _billing_address_1
            [meta_value] => 3723 Heaven Avenue
        )

)

答案 1 :(得分:-1)

<?php

foreach ($array as $value1) 
{
    foreach ($value1 as $key=>$value2) 
    {
        echo $key . " -> " . $value . "</br>";
    }
}

?>