1
votes

Quite new to php. I would be grateful is anyone can provide guidance about mapping the values in this array using php this is the output from var_dump

array(3) {
  ["k"]=>
  string(78) "method,from_tag,to_tag,callid,sip_code,sip_reason,time,from_user,to_user,token"
  ["v"]=>
  string(326) "BYE,gFNk8BZBg,B2B.269.327,KjmE8oPOV1,200,OK,Wed May 28 23:11:43 2014
,patientdemo1.gmail,sip:[email protected]:5060;transport=udp,037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4"
  ["query_type"]=>
  string(6) "insert"
}

as you may have noticed the column keys are within first array with key "k" and the values are under "v"

I need to pull some of these values out by referencing the keys within "k"

1
Have you made an attempt to do this at all? Can you show your code? Or are you just expecting someone to write this code for you? Other questions - why are you dealing with this input format to begin with? Hos do you handle cases with commas in the values themselves? - Mike Brant
Here is a perfect question. What is your input. What is your desired output. What code have you attempted to create to make your input look like your output. - Giacomo1968

1 Answers

1
votes

As stated in the comment. You'd use explode() to set your keys and values into their arrays respectively.

Done so as below:

<?php

$things = array(
'k' => 'method,from_tag,to_tag,callid,sip_code,sip_reason,time,from_user,to_user,token',
'v' => 'BYE,gFNk8BZBg,B2B.269.327,KjmE8oPOV1,200,OK,Wed May 28 23:11:43 2014
,patientdemo1.gmail,sip:[email protected]:5060;transport=udp,037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4'
);

$keys = explode(',', $things['k']);
$values = explode(',', $things['v']);

?>

Which returns:

Keys

Array
(
    [0] => method
    [1] => from_tag
    [2] => to_tag
    [3] => callid
    [4] => sip_code
    [5] => sip_reason
    [6] => time
    [7] => from_user
    [8] => to_user
    [9] => token
)

Values

Array
(
    [0] => BYE
    [1] => gFNk8BZBg
    [2] => B2B.269.327
    [3] => KjmE8oPOV1
    [4] => 200
    [5] => OK
    [6] => Wed May 28 23:11:43 2014

    [7] => patientdemo1.gmail
    [8] => sip:[email protected]:5060;transport=udp
    [9] => 037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4
)

And now you just need to loop through the values like so using foreach():

$data = array();
foreach($keys as $i => $key) {
    $data[$key] = $values[$i];
}

Which would product your final output of:

Array
(
    [method] => BYE
    [from_tag] => gFNk8BZBg
    [to_tag] => B2B.269.327
    [callid] => KjmE8oPOV1
    [sip_code] => 200
    [sip_reason] => OK
    [time] => Wed May 28 23:11:43 2014

    [from_user] => patientdemo1.gmail
    [to_user] => sip:[email protected]:5060;transport=udp
    [token] => 037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4
)

Working Example


Footnotes

This probably isn't the most efficient way to handle/do what you want to do. You should rethink how the first array with the columns/values is created and restructure that to suit your needs.

You should use array_combine() instead!


If this answers your question, just click on the arrow to the left there until it is green :) to mark this question as answered!