将回调应用于多维数组

发布于 2024-10-20 15:41:35 字数 287 浏览 1 评论 0原文

我在 PHP 中有这个:

$data = array("test"=>array("abc"=>"xyz"));

我想在 'data:' 后面附加数组值,因此 $data 的输出将为 'data:xyz'强>数组。

这只是一个样本。我怎样才能用多维数组做到这一点。 IE;附加带有多维数组值的字符串?我可以在同一个数组中使用持久值,以便我可以重复使用它吗?

I have this in PHP:

$data = array("test"=>array("abc"=>"xyz"));

I want to append 'data:' with array values, so output will be 'data:xyz' for that $data array.

Its just a sample. How can I do this with multi dimension arrays. i.e; appending string with multi dimension array values ? Can I do with persistent values in same array so I can re-use this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海 2024-10-27 15:41:35

有一个 PHP 函数,“将用户函数递归地应用于数组的每个成员”,它是 array_walk_recursive()

下面是一个使用闭包的 PHP 5.3 示例:

array_walk_recursive($data, function(&$str) { $str = "data:$str"; });

如果您觉得有趣,可以将其设为接受可配置前缀的函数,例如:

function prepend(&$v, $k, $prefix)
{
    $v = $prefix . $v;
}

array_walk_recursive($data, 'prepend', 'data:');

There's a PHP function that "[applies] a user function recursively to every member of an array" it's array_walk_recursive().

Here's an example for PHP 5.3 that uses a closure:

array_walk_recursive($data, function(&$str) { $str = "data:$str"; });

If you feel fancy, you can make it a function that accepts a configurable prefix, such as:

function prepend(&$v, $k, $prefix)
{
    $v = $prefix . $v;
}

array_walk_recursive($data, 'prepend', 'data:');
坚持沉默 2024-10-27 15:41:35

想象一下,您想要将自定义函数应用于数组,但仅应用于特定键 =>值对,这是我的改进版本:

$data = array( "test" => array("abc" => "xyz", 'def'=> 2), 'test2' =>  array ( 'abc' => 'jkl', 'def' => 5) );
$keytochange = 'def';
$apply = 'md5';
$data = f($data,$keytochange,$apply);

function f($array, $keytochange, $apply) 
{
   foreach ($array as $key => $value) 
   {
      if (is_array($value))
         $array[$key] = f($value,$keytochange,$apply);
      else
         $array[$keytochange] = $apply( $value );
   }
   return $array;
}

print_r($data);

输出:

Array
(
    [test] => Array
        (
            [abc] => xyz
            [def] => c81e728d9d4c2f636f067f89cc14862c
        )
    [test2] => Array
        (
            [abc] => jkl
            [def] => e4da3b7fbbce2345d7772b0674a318d5
        )
)

Imagine that you want to apply a custom function to your array but only to an specific key => value pair, this is my improved version:

$data = array( "test" => array("abc" => "xyz", 'def'=> 2), 'test2' =>  array ( 'abc' => 'jkl', 'def' => 5) );
$keytochange = 'def';
$apply = 'md5';
$data = f($data,$keytochange,$apply);

function f($array, $keytochange, $apply) 
{
   foreach ($array as $key => $value) 
   {
      if (is_array($value))
         $array[$key] = f($value,$keytochange,$apply);
      else
         $array[$keytochange] = $apply( $value );
   }
   return $array;
}

print_r($data);

Output:

Array
(
    [test] => Array
        (
            [abc] => xyz
            [def] => c81e728d9d4c2f636f067f89cc14862c
        )
    [test2] => Array
        (
            [abc] => jkl
            [def] => e4da3b7fbbce2345d7772b0674a318d5
        )
)
梦明 2024-10-27 15:41:35

如果您不关心人类可读性,只想序列化一个数组供以后使用:

<?php
$data = Array("test" => Array("abc" => "xyz"));
$str  = serialize($data);

echo 'data:' . $str;
print_r(unserialize($str));

// data:<some characters here>
// Array("test" => Array("abc" => "xyz"));
?>

如果您尝试修改原始数组,以便在 每个级别上数组中的每个 值:

<?php
$data = Array("test" => Array("abc" => "xyz"));

function f($array) {
   foreach ($array as $key => $value) {
      if (is_array($value))
         $array[$key] = f($value);
      else
         $array[$key] = "data:" . $value;
   }
   return $array;
}
$data = f($data);
print_r($data);

// Output: Array("test" => Array("abc" => "data:xyz"));
?>

If you don't care about human-readability and just want to serialise an Array for later use:

<?php
$data = Array("test" => Array("abc" => "xyz"));
$str  = serialize($data);

echo 'data:' . $str;
print_r(unserialize($str));

// data:<some characters here>
// Array("test" => Array("abc" => "xyz"));
?>

If you're trying to modify the original array such that the text "data:" is prepended to every value in the Array on every level:

<?php
$data = Array("test" => Array("abc" => "xyz"));

function f($array) {
   foreach ($array as $key => $value) {
      if (is_array($value))
         $array[$key] = f($value);
      else
         $array[$key] = "data:" . $value;
   }
   return $array;
}
$data = f($data);
print_r($data);

// Output: Array("test" => Array("abc" => "data:xyz"));
?>
森林散布 2024-10-27 15:41:35

要修改数组中的每个项目,可以使用函数 array_map()

示例:

  function Prepend($s)
  {
    return 'Data:'.$s;
  }

  $old_array = array('abc','def','ghi','xyz');

  $new_Array = array_map('Prepend',$old_array);

  var_dump($old_array);
  var_dump($new_Array);

输出:

array
  0 => string 'abc' (length=3)
  1 => string 'def' (length=3)
  2 => string 'ghi' (length=3)
  3 => string 'xyz' (length=3)
array
  0 => string 'Data:abc' (length=8)
  1 => string 'Data:def' (length=8)
  2 => string 'Data:ghi' (length=8)
  3 => string 'Data:xyz' (length=8)

To modify each item in an array, you can use the function array_map()

Example:

  function Prepend($s)
  {
    return 'Data:'.$s;
  }

  $old_array = array('abc','def','ghi','xyz');

  $new_Array = array_map('Prepend',$old_array);

  var_dump($old_array);
  var_dump($new_Array);

Output:

array
  0 => string 'abc' (length=3)
  1 => string 'def' (length=3)
  2 => string 'ghi' (length=3)
  3 => string 'xyz' (length=3)
array
  0 => string 'Data:abc' (length=8)
  1 => string 'Data:def' (length=8)
  2 => string 'Data:ghi' (length=8)
  3 => string 'Data:xyz' (length=8)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文