3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * I want it to go through the entire array, dropping down the nest until it reaches the array with index "Body" then return it * Why? Because I don't know how many times it will be nested */ $array = array(0 => array( "Headers" => array(), "Parts" => array( 0 => array( "Headers" => array("content-type:" => "CONTENT-TYPE"), "Parts" => array( 0 => array( "Headers" => array("content-type:" => "CONTENT-TYPE"), "Parts" => array( 0 => array( "Headers" => array("content-type:" => "CONTENT-TYPE"), "Parts" => array(), "Body" => "OMG BODY!!!!")))))))); var_dump( getBody($array) ); function getBody($array) { foreach ($array as $key => $val) { if ($key == 'Body') return $val; if (is_array($val)) { return getBody($val); } } return array("body" => $body, "ctype" => $ctype); }
Output for 4.3.0 - 7.1.0
array(2) { ["Headers"]=> array(0) { } ["Parts"]=> array(1) { [0]=> array(2) { ["Headers"]=> array(1) { ["content-type:"]=> string(12) "CONTENT-TYPE" } ["Parts"]=> array(1) { [0]=> array(2) { ["Headers"]=> array(1) { ["content-type:"]=> string(12) "CONTENT-TYPE" } ["Parts"]=> array(1) { [0]=> array(3) { ["Headers"]=> array(1) { ["content-type:"]=> string(12) "CONTENT-TYPE" } ["Parts"]=> array(0) { } ["Body"]=> string(12) "OMG BODY!!!!" } } } } } } }