php 将键与关联多维数组中的值组合

php combine keys with values in associative multidimensional array

我正在尝试操作关联多维数组。我已经从要应用于另一个值的数组中提取键。 . .

这些是我在另一个函数中提取的键

$keys = array (
    "id" => "id",
    "addr_street_num" => "addr_street_num",
    "addr_street" => "addr_street",
    "price" => "price",
    "days" =>"days",
    "state" => Array
        (
            "id" => "id",
            "name" => "name"
        ),

    "city" => Array
        (
            "id" => "id",
            "web_id" => "web_id",
            "name" => "name"
        )
);

此数组包含我想要组合在一起的值

$vals = array (
    "0" => "830680",
    "1" => "20",
    "2" => "Sullivan Avenue",
    "3" => "333000",
    "4" => "12",
    "5" => Array
        (
             "0" => "4",
             "1" => "Maryland",
        ),

    "6" => Array
        (
            "0" => "782",
            "1" => "baltimore",
            "2" => "Baltimore",
        )
);

当我尝试做 array_combine($keys, $val); 我收到 2 条关于数组到字符串转换的通知

我想 array_combine 只适用于一维数组,关于如何处理这个有什么想法吗?

如果修改了 $keys 是否可以将其与值组合 - 问题是 $keys 的形状是我最终想要的吗?

可以递归完成。

function combine_recursive($keys, $values) {
    $result = array();
    $key = reset($keys);
    $value = reset($values);
    do {
        if (is_array($value)) {
            $result[key($keys)] = combine_recursive($key, $value);
        } else {
            $result[key($keys)] = $value;
        }
        $value = next($values);
    } while ($key = next($keys));
    return $result;
}

这对我适用于您的示例数组。我敢肯定,如果数组结构彼此完全不同,这会给您带来各种奇怪的感觉 results/errors。