Reputation: 13333
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.
Upvotes: 0
Views: 352
Reputation: 1374
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'];
}
}
Upvotes: 0
Reputation: 29
A simple foreach can do the job correctly as say Ghost :
http://php.net/manual/en/control-structures.foreach.php
Upvotes: 0
Reputation: 4739
$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;
}
}
Upvotes: 1