php 提取关联数组

问题描述 投票:0回答:3

有一个数组保存动态数据。它随机变化。我使用

foreach()
循环来检索
["space"]
["channel"]
["Parker Dial String"]

$parking_lot = array
(
    0 => "bellcab-parked-call",
    2 => "Parking Extension   :  300",
    3 => "Parking Context     :  bellcab_call_parking_lot",
    4 => "Parking Spaces      :  3000-3099",
    5 => "Parking Time        :  0 sec",
    6 => "Comeback to Origin  :  no",
    7 => "Comeback Context    :  bellcab-parked-call-return",
    8 => "Comeback Dial Time  :  30 sec",
    9 => "MusicOnHold Class   :  ",
    10 => "Enabled             :  yes",
    11 => "Dynamic             :  no",
    12 => "",
    13 => "Parked Calls",
    14 => "------------",
    15 =>  "Space               :  3000",
    16 =>  "Channel             :  SIP/itc-vitel-inbound-00000ee4",
    17 =>  "Parker Dial String  :  3004",
    18 => "",
    19 =>   "Space               :  3001",
    20 =>   "Channel             :  SIP/itc-vitel-inbound-00000ee7",
    21 =>   "Parker Dial String  :  3004",
    22 =>   "Space               :  3002",
    23 =>   "Channel             :  SIP/itc-vitel-inbound-00000e82",
    24 =>   "Parker Dial String  :  3004",
    25 => "",
    26 => "",
    27 => "--END COMMAND--"
);

foreach($parking_lot as $val)
{
    $temp = explode(" : ", $val);
    if(isset($temp[1])){
        $new_array[trim($temp[0])][] = trim($temp[1]);
    }
}
print_r($new_array);

输出

Array
(
    [Parking Extension] => Array
        (
            [0] => 300
        )

    [Parking Context] => Array
        (
            [0] => bellcab_call_parking_lot
        )

    [Parking Spaces] => Array
        (
            [0] => 3000-3099
        )

    [Parking Time] => Array
        (
            [0] => 0 sec
        )

    [Comeback to Origin] => Array
        (
            [0] => no
        )

    [Comeback Context] => Array
        (
            [0] => bellcab-parked-call-return
        )

    [Comeback Dial Time] => Array
        (
            [0] => 30 sec
        )

    [MusicOnHold Class] => Array
        (
            [0] =>
        )

    [Enabled] => Array
        (
            [0] => yes
        )

    [Dynamic] => Array
        (
            [0] => no
        )

    [Space] => Array
        (
            [0] => 3000
            [1] => 3001
            [2] => 3002
        )

    [Channel] => Array
        (
            [0] => SIP/itc-vitel-inbound-00000ee4
            [1] => SIP/itc-vitel-inbound-00000ee7
            [2] => SIP/itc-vitel-inbound-00000e82
        )

    [Parker Dial String] => Array
        (
            [0] => 3004
            [1] => 3004
            [2] => 3004
        )

)

想要的输出是这样的

Array
(
   Array(
         [Space] => 3000
         [Channel] => SIP/itc-vitel-inbound-00000ee4
         [Parker Dial String] => 3004
        )
   Array(
         [Space] => 3001
         [Channel] => SIP/itc-vitel-inbound-00000ee7
         [Parker Dial String] => 3004
        )
   Array(
         [Space] => 3002
         [Channel] => SIP/itc-vitel-inbound-00000e82
         [Parker Dial String] => 3004
        )
)
php arrays
3个回答
1
投票

首先过滤您必须仅使用所需键的数组。

然后使用 array-map 对它们进行分组(注意使用

...
运算符来展平数组):

$keys = array("Space", "Channel", "Parker Dial String");
foreach($keys as $k)
    $filtered[] = $new_array[$k];
$res = array_map(null, ...$filtered);
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res);
print_r($res);

实例:3v4l


1
投票

这是一种可能性:

  1. 定义您想要的键。

    $keys = ['Space', 'Channel', 'Parker Dial String'];
    
  2. 使用

    $new_array
    array_intersect_key
    数组中提取它们。

    $values = array_values(array_intersect_key($new_array, array_flip($keys)));
    
  3. 使用数组解包将

    array_combine
    映射到结果上以获得所需的结果。

    $result = array_map(
        function(...$values) use ($keys) { return array_combine($keys, $values); },
        ...$values
    );
    

array_values
在第二步中是必需的,因为您无法使用字符串键解压数组。如果您愿意,可以使用
...array_values($values)
而不是
...$values
将其移动到第三步。


0
投票

您可以通过避免换行符拆分和对连续数据集进行聚类的子过程来简化流程。

相反,只需对整个字符串输入调用

preg_match_all()
,然后根据其结果形成关联行。

为了使我的脚本更易于修改,我将从一组所需的键开始,并从中构建正则表达式模式。

代码:(演示)

$keys = ['Space', 'Channel', 'Parker Dial String'];
$delimAndValue = '\s*:\s*(.*)';
$regex = '/' . implode('\v+', array_map(fn($k) => $k . $delimAndValue, $keys)) . '/';
preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
var_export(
    array_map(
        fn ($m) => array_combine($keys, array_slice($m, 1)),
        $matches
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.