php从数组中获取值

时间:2015-04-14 07:52:13

标签: php arrays

我在php中有一个数组,从那个数组我希望获得一些像

这样的值

给定的数组就像这样

Array
(
    [product_id] => 963
    [variation] => Array
        (
            [start_date] => 8 May, 2015
            [adults_travelers] => 15
            [child_travelers] => 0
            [infant_travelers] => 0
        )

    [quantity] => 1
    [line_total] => 1185
    [line_tax] => 0
    [line_subtotal] => 1185
    [line_subtotal_tax] => 0

    [data] => WC_Product_Simple Object
        (
            [id] => 963
            [post] => WP_Post Object
                (
                    [ID] => 963
                    [post_author] => 2
                    [post_date] => 2015-03-31 13:23:32
                    [post_date_gmt] => 2015-03-31 13:23:32
                    [menu_order] => 0
                    [post_type] => product
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

            [product_type] => simple
            [dimensions:protected] => 
            [shipping_class:protected] => 
            [shipping_class_id:protected] => 0
            [price] => 60
        )

)

Array
(
    [product_id] => 960
    [variation] => Array
        (
            [start_date] => 28 May, 2015
            [adults_travelers] => 10
            [child_travelers] => 2
            [infant_travelers] => 4
        )

    [quantity] => 1
    [line_total] => 1185
    [line_tax] => 0
    [line_subtotal] => 1185
    [line_subtotal_tax] => 0

    [data] => WC_Product_Simple Object
        (
            [id] => 960
            [post] => WP_Post Object
                (
                    [ID] => 960
                    [post_author] => 2
                    [post_date] => 2015-03-31 13:23:32
                    [post_date_gmt] => 2015-03-31 13:23:32
                    [menu_order] => 0
                    [post_type] => product
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

            [product_type] => simple
            [dimensions:protected] => 
            [shipping_class:protected] => 
            [shipping_class_id:protected] => 0
            [price] => 60
        )

)

Array
(
    [product_id] => 958
    [variation] => Array
        (
            [start_date] => 22 May, 2015
            [adults_travelers] => 11
            [child_travelers] => 10
            [infant_travelers] => 2
        )

    [quantity] => 4
    [line_total] => 1185
    [line_tax] => 0
    [line_subtotal] => 1185
    [line_subtotal_tax] => 0

    [data] => WC_Product_Simple Object
        (
            [id] => 958
            [post] => WP_Post Object
                (
                    [ID] => 958
                    [post_author] => 2
                    [post_date] => 2015-03-31 13:23:32
                    [post_date_gmt] => 2015-03-31 13:23:32
                    [menu_order] => 0
                    [post_type] => product
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

            [product_type] => simple
            [dimensions:protected] => 
            [shipping_class:protected] => 
            [shipping_class_id:protected] => 0
            [price] => 60
        )

)

我希望从此数组中获取类似start_date, adult_travelers, child_travelers, infant_travelers的值,其product_id为963。所以有人可以告诉我如何获得post_id = 963的那些值。任何帮助和建议都会非常明显。

3 个答案:

答案 0 :(得分:1)

$product_id = 963;
foreach ($prodArr AS $eachArr) {
    if ($eachArr['product_id'] == $product_id) {
        $start_date = $eachArr['variation']['start_date'];
        $adult_travelers = $eachArr['variation']['adults_travelers'];
        $child_travelers = $eachArr['variation']['child_travelers'];
        break;
    }
}

答案 1 :(得分:0)

一个简单的foreach可以像Ghost一样正确地完成工作:

http://php.net/manual/en/control-structures.foreach.php

答案 2 :(得分:0)

foreach ($data AS $array) {

    if($array['data']->post->ID == '963') {

        echo $array['variation']['start_date'];
        echo $array['variation']['adults_travelers'];
        echo $array['variation']['child_travelers'];
    }
}