1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

再起ってキモいと隣の席の人に言われたので

Last updated at Posted at 2012-03-16

再起で遊んでみた

<?php
function array_walk_deeply ( $v, $f ) {
  $is_obj = null;
  
  if ( is_object ( $v ) && $v instanceof stdClass ) {
    $is_obj = $v = (array)$v;
  }

  if ( is_array  ( $v ) ) {
    foreach ( $v as $key => $val ) {
      $v[$key] = array_walk_deeply ( $v[$key], $f  );
    }
  }
  elseif ( is_scalar ( $v ) ) {
    $v = $f ( $v );
  }

  if ( $is_obj ) $v = (object)$v;
  return $v;
}

/*
$val1 = array (
  'hoge' => 'fuga',
  'fuga' => 'piyo',
  'outer_array' => array(
    'inner_array' => array(
      'inner_hoge' => 'inner_fuga',
    ),
  ),
  'outer' => (object) array (
    'xyzzy'  => 'emacs like',
    'vim'    => 'vi like',
    'inner' => (object)array (
      'jojo' => 'dio',
      'sasuke' => 'naruto',
    ),
));

$f1 = function ( &$v ) { 
  if ( !is_scalar ( $v ) ) return $v;
  $v = $v . ' after';
  return $v;
};

print 'array_walk_deeply' . "\n";
var_dump ( array_walk_deeply ( $val1 , $f1 ));

array_walk ( $val1, $f1 ) ;
print 'array_walk' . "\n";
var_dump ( $val1 );
*/
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?