从值中获取子字符串作为数组中的键

时间:2019-07-17 17:42:05

标签: php arrays

我有一个如下数组。

$as = Array(
    Array
        (
            2 => "Name: SIP/3004",
            10 => "Status: 5"
        ),

    Array
        (
            2 => "Name: SIP/3001",
            10 => "Status: 2"
        ),

    Array
        (
            2 => "Name: Local/3001@bell-cab-agent",
            10 => "Status: 1"
        )

);

我想删除主数组,我想从值中获取子字符串作为数组键,并删除所有数组中的当前键。请求的输出如下

Array
        (
            "Name" => "SIP/3004",
            "Status" => "5"
        )

Array
        (
            "Name" => "SIP/3001",
            "Status" => "2"
        )

Array
        (
            "Name" => "Local/3001@bell-cab-agent",
            "Status" => "1"
        )

谢谢。

1 个答案:

答案 0 :(得分:0)

这应该按照您的要求将其全部放在一个阵列中

$new_array = [];

foreach($as as $inner_array)
{
    $temp_array = [];
    foreach($inner_array as $val){
        $temp = explode(": ", $val);
        $temp_array[$temp[0]] = $temp[1];
    }

    array_push($new_array, $temp_array);
}

print_r($new_array);