Created
July 2, 2016 16:05
-
-
Save sasezaki/b39a562b3afe50242b4432d58d62ad7f to your computer and use it in GitHub Desktop.
filter value for nested array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$post = [ | |
'k' => 'v', | |
'kk' => [ | |
'll' => 'あああ', | |
'l2' => [ | |
['xCode' => 'おおお'], | |
['x2Code' => 'おおお'], | |
'm1' => [ | |
'd1' => '3', | |
'd2' => 4, | |
] | |
] | |
] | |
]; | |
function filter ($key, $value) { | |
if (is_array($value)) { | |
foreach ($value as $childKey => $childValue) { | |
list($childKey, $childValue) = filter($childKey, $childValue); | |
$value[$childKey] = $childValue; | |
} | |
return [$key, $value]; | |
} else { | |
$value = $value . 'fooo'; // filter | |
return [$key, $value]; | |
} | |
} | |
foreach ($post as $k => $v) { | |
list($k, $v) = filter($k, $v); | |
$post[$k] = $v; | |
} | |
echo json_encode($post, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment