简体   繁体   中英

php get the values from an array

I have an array in php and from that array I want to get some values like

The given array is like this

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
        )

)

I want to get the values like start_date, adult_travelers, child_travelers, infant_travelers whose product_id is 963 from this array. So can someone tell me how to get those values where the post_id = 963. Any help and suggestions will be really appreciable.

$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;
    }
}

A simple foreach can do the job correctly as say Ghost :

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

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'];
    }
}

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