Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维关联数组中的替换_Php_Arrays - Fatal编程技术网

Php 多维关联数组中的替换

Php 多维关联数组中的替换,php,arrays,Php,Arrays,假设我有一个多维关联数组: $haystack = [ "httpMethod" => "GET", "responseFormat" => "", "query" => [ "uname" => "username", "pass" => "password" ] ]; 我还有一个简

假设我有一个多维关联数组:

    $haystack = 
         [
          "httpMethod" => "GET",
          "responseFormat" => "",
          "query" => [
            "uname" => "username",
            "pass" => "password"
             ]
          ];
我还有一个简单的数组:

    $needle = [
        "username" => "123",
        "password" => "456"
    ];
我的目标是递归搜索第一个数组,其中值为“username”,并用第二个数组中的值替换它

因此,返回的数组应该是:

$result = [
          "httpMethod" => "GET",
          "responseFormat" => "",
          "query" => [
            "uname" => "123",
            "pass" => "456"
             ]
          ];
最有效、可读性最强的方法是什么?知道haystack数组的结构可能不同。

试试看,任何包含数组的键都不会传递给函数

array_walk_recursive( $haystack, function( &$item, $key ) use ( $needle ) {

    if ( array_key_exists( $item, $needle ) )
        $item = $needle[ $item ];

});
请尝试,任何包含数组的键都不会传递给函数

array_walk_recursive( $haystack, function( &$item, $key ) use ( $needle ) {

    if ( array_key_exists( $item, $needle ) )
        $item = $needle[ $item ];

});
请尝试,任何包含数组的键都不会传递给函数

array_walk_recursive( $haystack, function( &$item, $key ) use ( $needle ) {

    if ( array_key_exists( $item, $needle ) )
        $item = $needle[ $item ];

});
请尝试,任何包含数组的键都不会传递给函数

array_walk_recursive( $haystack, function( &$item, $key ) use ( $needle ) {

    if ( array_key_exists( $item, $needle ) )
        $item = $needle[ $item ];

});

这是最基本的方法

$haystack['query']['uname'] = $needle['username'];

这是最基本的方法

$haystack['query']['uname'] = $needle['username'];

这是最基本的方法

$haystack['query']['uname'] = $needle['username'];

这是最基本的方法

$haystack['query']['uname'] = $needle['username'];

你有没有试过什么,或者你只是在寻找免费的代码?我只是卡住了,是的,我有一个完整的应用程序。请看我的其他帖子。你试过什么吗,或者你只是在寻找免费代码?我只是被卡住了,是的,我有一个完整的应用程序。请看我的其他帖子。你试过什么吗,或者你只是在寻找免费代码?我只是被卡住了,是的,我有一个完整的应用程序。请看我的其他帖子。你试过什么吗,或者你只是在寻找免费代码?我只是被卡住了,是的,我有一个完整的应用程序。请看我的其他帖子。