0

I have an array like this:

array(4) {

  [0] => "1. \Device\NPF_{1AAAAA99-7933-4CBC-AF26-2A1C1394BD94} (VirtualBox Host-Only Network)"

  [1] => "2. \Device\NPF_{2BBBBB88-75BB-4CD0-970A-36430287C245} (Wireless Network Connection 6)"

  [2] => "3. \Device\NPF_{3CCCCC77-8469-4445-A2ED-9C0FDDBA1C3D} (Wireless Network Connection 7)"

  [3] => "4. \Device\NPF_{4EEEEE66-2C9D-4D47-B7EA-8A20A40A149B} (Local Area Connection)"

}

and would like to manipulate into a new associative array like this:

array(4) {

  ["1"] => "VirtualBox Host-Only Network"

  ["2"] => "Wireless Network Connection 6"

  ["3"] => "Wireless Network Connection 7"

  ["4"] => "Local Area Connection"

}

I realise that I could use a foreach loop to go thru the original array then in each iteration use substr function to get the first char for the new array key and to look for the sentence inside the () pattern but failed to implement this. Any suggestion is much appreciated! Thanks!

1
  • You should post the code that you have tried. Commented Jan 23, 2015 at 15:46

3 Answers 3

3

In php you can do that without loops:

$data = [
    "1. \Device\NPF_{1AAAAA99-7933-4CBC-AF26-2A1C1394BD94} (VirtualBox Host-Only Network)",
    "2. \Device\NPF_{2BBBBB88-75BB-4CD0-970A-36430287C245} (Wireless Network Connection 6)",
    "3. \Device\NPF_{3CCCCC77-8469-4445-A2ED-9C0FDDBA1C3D} (Wireless Network Connection 7)",
    "4. \Device\NPF_{4EEEEE66-2C9D-4D47-B7EA-8A20A40A149B} (Local Area Connection)",
    ];

$names = array_combine(
    preg_replace('~\D.*~', '', $data),
    preg_replace('~.*\((.+?)\)$~', '$1', $data));

print_r($names);

Result:

Array
(
    [1] => VirtualBox Host-Only Network
    [2] => Wireless Network Connection 6
    [3] => Wireless Network Connection 7
    [4] => Local Area Connection
)
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that is fast! However, what I need is the final array key came from the first charater of the original array (e.g. the 1. 2. 3. 4.). Could I achieve that with your approach? Many many thanks!
Wow, work of a magician! After going through all the answers, I am amazed by the true power of regular expression and preg_match. My next topic of study. Again, thank you trully, everyone!
0

Your code should look something like this,

<?php
$items_extended = array("1. \\Device\\NPF_{1AAAAA99-7933-4CBC-AF26-2A1C1394BD94} (VirtualBox Host-Only Network)", 
"2. \\Device\\NPF_{2BBBBB88-75BB-4CD0-970A-36430287C245} (Wireless Network Connection 6)", 
"3. \\Device\\NPF_{3CCCCC77-8469-4445-A2ED-9C0FDDBA1C3D} (Wireless Network Connection 7)", 
"4. \\Device\\NPF_{4EEEEE66-2C9D-4D47-B7EA-8A20A40A149B} (Local Area Connection)");
$items = array();
foreach($items_extended as $key => $value){
    preg_match('#\((.*?)\)#', $value, $match);
    $items[($key + 1)] = $match[0];
}
?>

1 Comment

Thank you so much, your solution also works like a charm! I wish I have more reputation to vote up! Much appreciated!
0

Try to use that code : by using foreach, it should work. To test regex : http://www.phpliveregex.com/

<? 
var $arrInputs = array (......); // you put your array with data

var $arrest = [];
foreach ($arrInputs as $key => $input_line ){

preg_match("(^.).*[(](.*)[)]$", $input_line, $output_array);

$arrest[$output_array[1]] = $output_array[2];

}

print_r($arrest);


%>

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.