3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function dive(array $map){ $columns = $tokens = array(); foreach($map as $key => $value){ if(is_array($value)){ // check for bool switch if(array_key_exists(0, $value)){ if($value[0] !== true) continue; $columns[$key] = $key; $tokens[$key] = $value[1]; }else{ // if another array recurse $tmp = dive($value); $columns = array_merge($columns, array_keys($tmp)); $tokens = array_merge($tokens, array_values($tmp)); } }else{ if(is_null($value)) continue; // catch non-null scalars $columns[$key] = $key; $tokens[$key] = "?"; } } // because $columns will inevitably contain duplicate values, once the // two arrays are combined, they will collapse/uniquify. #darkcorner return array_combine($columns, $tokens); } call_user_func(function(){ $a = array( "col1" => "val", "col2" => "val", "col3" => "val", "col4" => "val", ); $columns = array( "col1", "col2", "col3", "col4", ); $tokens = array( "?", "?", "?", "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( "col1" => "val", "col2" => null, "col3" => "val", "col4" => "val", ); $columns = array( "col1", "col3", "col4", ); $tokens = array( "?", "?", "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( "col1" => array(true, "val"), "col2" => array(true, "val"), "col3" => array(true, "val"), "col4" => array(true, "val"), ); $columns = array( "col1", "col2", "col3", "col4", ); $tokens = array( "val", "val", "val", "val", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( "col1" => "val", "col2" => "val", "col3" => array(true, "val"), "col4" => array(true, "val"), ); $columns = array( "col1", "col2", "col3", "col4", ); $tokens = array( "?", "?", "val", "val", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( "col1" => array(true, "val"), "col2" => array(true, "val"), "col3" => "val", "col4" => "val", ); $columns = array( "col1", "col2", "col3", "col4", ); $tokens = array( "val", "val", "?", "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( "col1" => "val", "col2" => array(true, "val"), "col3" => array(true, "val"), "col4" => "val", ); $columns = array( "col1", "col2", "col3", "col4", ); $tokens = array( "?", "val", "val", "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( array("col1" => "val"), array("col1" => "val"), array("col1" => "val"), array("col1" => "val"), ); $columns = array( "col1", ); $tokens = array( "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( array("col1" => array(true, "val")), array("col1" => array(true, "val")), array("col1" => array(true, "val")), array("col1" => array(true, "val")), ); $columns = array( "col1", ); $tokens = array( "val", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( array("col1" => array(true, "val"), "col2" => array(true, "val")), array("col1" => array(true, "val"), "col2" => array(true, "val")), array("col1" => array(true, "val"), "col2" => array(true, "val")), array("col1" => array(true, "val"), "col2" => array(true, "val")), ); $columns = array( "col1", "col2", ); $tokens = array( "val", "val", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( array("col1" => "val", "col2" => "val"), array("col1" => "val", "col2" => "val"), array("col1" => "val", "col2" => "val"), array("col1" => "val", "col2" => "val"), ); $columns = array( "col1", "col2", ); $tokens = array( "?", "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( array("col1" => "val", "col2" => null), array("col1" => "val", "col2" => null), array("col1" => "val", "col2" => null), array("col1" => "val", "col2" => null), ); $columns = array( "col1", ); $tokens = array( "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( array("col1" => "val", "col2" => array(true, "val")), array("col1" => "val", "col2" => array(true, "val")), array("col1" => "val", "col2" => array(true, "val")), array("col1" => "val", "col2" => array(true, "val")), ); $columns = array( "col1", "col2", ); $tokens = array( "?", "val", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } }); call_user_func(function(){ $a = array( array("col1" => array(true, "val"), "col2" => "val"), array("col1" => array(true, "val"), "col2" => "val"), array("col1" => array(true, "val"), "col2" => "val"), array("col1" => array(true, "val"), "col2" => "val"), ); $columns = array( "col1", "col2", ); $tokens = array( "val", "?", ); // list($c, $t) = dive($a); $tmp = dive($a); $c = array_keys($tmp); $t = array_values($tmp); if($columns == $c){ echo "TRUE\n"; } if($tokens == $t){ echo "TRUE\n"; } });
Output for 5.3.0 - 7.1.0
TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE