0

There is an array which is hold the dynamic data. it changes randomly. I used foreach() loop to retrieve the ["space"] ["channel"] and ["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);

Output

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
        )

)

The desired output is like this

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
        )
)
1
  • 2
    If it changes randomly how can you specify any pattern to extract values of of it? Commented Sep 19, 2019 at 21:23

3 Answers 3

1

First filter the array you have to be with only the desire keys.

Then use array-map to group them (notice the use of the ... operator to flat the array):

$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);

Live example: 3v4l

Sign up to request clarification or add additional context in comments.

Comments

1

Here is one possibility:

  1. Define the keys you want.

    $keys = ['Space', 'Channel', 'Parker Dial String'];
    
  2. Extract them from your $new_array array using array_intersect_key.

    $values = array_values(array_intersect_key($new_array, array_flip($keys)));
    
  3. Map array_combine over the result using array unpacking to get your desired result.

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

array_values is necessary in the second step because you can't unpack arrays with string keys. You could move it to the third step instead with ...array_values($values) instead of ...$values if you prefer.

3 Comments

Great mind and all... - but I think your is better - I used the array_map twice and you only once as you use the better advantage of the ...
@dWinder It actually occurred to me recently when I was answering another question here and it was one of those things that seemed obvious in hindsight. I had gotten used to using array_map(null, with multiple arrays before the splat operator existed, and it took me until just last week to notice you could do the same thing in one step.
Thanks guys. it is replacing the ["Channel"] with ["Space"]. Array( [0] => Array ( [Space] => SIP/itc-vitel-inbound-00000ee4 [Channel] => 3000 [Parker Dial String] => 3004 ) )
0

You can simplify your process by avoiding the subprocesses of splitting on newlines and clustering consecutive data sets.

Instead, just call preg_match_all() on the entire string input then form associative rows from its results.

To make my script easier to modify, I'll start from an array of desired keys and build the regex pattern from it.

Code: (Demo)

$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
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.