0

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.

8
  • 1
    The example you have given is just one of the many elements of another array? Commented Apr 14, 2015 at 7:55
  • a simple foreach should suffice, acess array indices as ['index here'] and the objects as ->properties_here Commented Apr 14, 2015 at 7:57
  • Just loop through array and check if post_id=963 Commented Apr 14, 2015 at 7:57
  • @Ghost can you share some codes? Commented Apr 14, 2015 at 7:59
  • @Justinas can you share some code? Commented Apr 14, 2015 at 8:00

3 Answers 3

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

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

1 Comment

Please add a little bit more detail to what the code does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.