Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created July 2, 2016 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasezaki/b39a562b3afe50242b4432d58d62ad7f to your computer and use it in GitHub Desktop.
Save sasezaki/b39a562b3afe50242b4432d58d62ad7f to your computer and use it in GitHub Desktop.
filter value for nested array
<?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