I've an array in php like bellow:
array:2 [
"element_a" => array:1 [
"string" => array:3 [
"min" => 5
"max" => 50
0 => "nullable" // this element need be flipped
]
]
"element_b" => array:1 [
0 => "string" // this need too
]
]
And I need flip elements with numeric key value.. I want get the following output:
array:2 [
"element_a" => array:1 [
"string" => array:3 [
"min" => 5
"max" => 50
"nullable" => true
]
]
"element_b" => array:1 [
"string" => true
]
]
Any ideas about how i can get it?
Thanks.
I tried this, but no results:
array_walk_recursive($array, function (&$value, &$key) {
if (is_numeric($key)) {
list($value, $key) = [$key, $value];
}
});
This solution will fix your array in place (by reference):
function fix( array &$array ) {
foreach( $array as $key => &$value ) {
if( is_array( $value ) ) {
fix( $value );
}
else if( is_numeric( $key ) ) {
$array[ $value ] = true;
unset( $array[ $key ] );
}
}
}
$yourArray = [
"element_a" => [
"string" => [
"min" => 5,
"max" => 50,
0 => "nullable" // this element need be flipped
]
],
"element_b" => [
0 => "string" // this need too
]
];
fix( $yourArray );
var_dump( $yourArray );
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.