I have the followed string:
Host dnsmgr Username Refresh State Reg.Time
sip.voicetrading.com:5060 N travelAsk 105 Registered Thu, 27 Nov 2014 15:57:43
sip.alonia.es:5060 N user431 225 Registered Thu, 27 Nov 2014 23:28:24
I want to somehow make this string as associative array.
Something like this:
array(
'sip.voicetrading.com' => array (
'port' => 5060,
'username' =>'travelAsk',
'refresh' => 105,
'state' => 'Registered',
'time' => 'Thu, 27 Nov 2014 15:57:43'
),
'sip.alonia.es' => array (
'port' => 5060,
'username' =>' user431',
'refresh' => 225,
'state' => 'Registered',
'time' => 'Thu, 27 Nov 2014 23:28:24'
)
);
Can you suggest me a way how it would be the best way to do it.
So far I tried something like this:
$peer = array(
"data"=>
"Host dnsmgr Username Refresh State Reg.Time
sip.voicetrading.com:5060 N travelIhome 105 Registered Thu, 27 Nov 2014 16:39:45 " ;
foreach (explode("
", $peer['data']) as $line) {
$a = strpos('z' . $line, ':') - 1;
if ($a >= 0) {
$arrTrunkSettings = explode (' ', trim(substr($line, $a + 1)));
foreach ($arrTrunkSettings as $key => $value) {
if (!empty($value)) {
$data[trim(substr($line, 0, $a))][] = $value;
}
}
}
}
and the Output is like this array with numeric indexes: But this actually is not what I need.
array (
"sip.voicetrading.com" =>
array(
[0]=> "5060"
[1]=> "N"
[2]=> "travelAsk"
[3]=> "105"
[4]=> "Registered"
[5]=> "Thu,"
[6]=> "27"
[7]=> "Nov"
[8]=> "2014"
[9]=> "16:32:45"
)
)