php mapping keys to multidimensional array - php

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:join.me#192.168.1.20: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"

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:join.me#192.168.1.20: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:join.me#192.168.1.20: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:join.me#192.168.1.20: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!

Related

array_column() expects at most 3 parameters, 13 given

I have a PHP array as follows:
print_r($myarray);
Array
(
[0] => Array
(
[JAN] => 484603732
[FEB] => 350203732
[MAR] => 133347732
[APR] => 203347732
[MEI] => 79797732
[JUNI] => 112047732
[JULI] => 380597732
[AGS] => 76597732
[SEP] => 86597732
[OKT] => 120397732
[NOV] => 391597732
[DES] => 58597732
)
)
I want to delete element like [JAN], [FEB], [MAR], ...
But I little bit confused about how to handle it with array_column
So far, I've tried:
$array = array_column($myarray, 'JAN','FEB','MAR','APR','MEI','JUNI','JULI','AGS','SEP','OKT','NOV','DES');
But, still facing error
array_column() expects at most 3 parameters, 13 given
My expected output:
Array
(
[0] => 484603732
[1] => 350203732
[2] => 133347732
[3] => 203347732
[4] => 79797732
[5] => 112047732
[6] => 380597732
[7] => 76597732
[8] => 86597732
[9] => 120397732
[10] => 391597732
[11] => 58597732
)
Any well thought to advise will be appreciated.
Thanks.
You don't need array_column() here. According to PHP documentation
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
You can simply use array_values() to remove all the keys.
$array = array_values($myarray[0])

Compare Array elements and add based on key and value

I have two arrays like this:
$array_1 = Array ( [0] => 4 [1] => 6 [2] => 2 [3] => 6 [4] => 4 [5] => 10 [6] => 4 [7] => 6 [8] => 2 [9] => 2 [10] => 4 [11] => 4 [12] => 2 [13] => 2 );
$array_2 = Array ( [0] => DK [1] => GA [2] => DK [3] => GA [4] => DK [5] => GA [6] => WE [7] => VE [8] => WE [9] => VE [10] => PLA [11] => PRA [12] => PLA [13] => PRA ) ;
Now I want result like this:
$dk=4+2+4=10;
$ga=6+6+10=22;
$we=4+2=6;
$ve=6+2=8;
$pla=4+2=6;
$pra=4+2;
Explanation:
In $array_2, 'DK' exists 3 times and key values are = 0,2 and 4.
So, i have to add the values of $array_1 having key 0,2,4 and assign them to $dk. Here, $dk will be 4+2+4=10. This process will be same for all other variables.
How can i do this??
Instead separate variable name I suggest you to make array like this
<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
if(isset($newArray[$value])){
$newArray[$value] +=$array_1[$key];
}else{
$newArray[$value] =$array_1[$key];
}
}
print_r($newArray);
?>
Live Demo
Output :
Array
(
[DK] => 6
[GA] => 12
)
Another suggestion : Instead complex programming try to make good relation or binding to not get any inconsistency in records
This will loop array2 and build an array with the sum.
Then output it (just to see the result), then I use extract to pull out the variables as you want them.
But I would rather keep them in the array
Foreach($array_2 as $key => $val){
If(!isset($new[$val])) $new[$val] =0;
$new[$val] += $array_1[$key];
}
Var_dump($new);
Extract($new);
https://3v4l.org/jOR7Z

PHP Array get first item

There is probably a really simple way to do this but i can't work it out.
Array ( [0] => Array ( [] => US [1] => U.S. [2] => 21 [3] => 34 [4] => 33 [5] => 35 [6] => 39 [7] => 50 [8] => 61 ) [1] => Array ( [] => 79 [1] => 45 [2] => 84 [3] => 89 [4] => 59 [5] => 64 [6] => 34 [7] => 58 [8] => 55 ) [2] => Array ( [] => 63 [1] => 105 [2] => 68 [3] => 62 [4] => 64 [5] => 67 [6] => CL [7] => Chile [8] => 56 ) [3] => Array ( [] => 40 [1] => 40 [2] => 63 [3] => 37 [4] => 57 [5] => 64 [6] => 59 [7] => 53 [8] => 68 ) [4] => Array ( [] => 70 [1] => 66 [2] => 88 [3] => 48 [4] => 76 [5] => 83 [6] => 80 [7] => 53 [8] => 45 ) [5] => Array ( [] => 44 [1] => 51 [2] => 52 [3] => [4] => [5] => [6] => [7] => [8] => ) )
This is my array. There will all ways be the same number of object (9) in each however the number currently at 6 may increase.
I need to get the 1st item in each so for the 1st one i need (US) I'm stuck as if i put
echo $array[0][1];
Then I get U.S. however i need the first item (US) so i tried both
echo $array[0][0];
echo $array[0][];
Neither return a value. What am i doing wrong?
Using an “empty” string as key in an associative array is possible in PHP.
It is distinctively different from any other, non-empty string key values - so it fulfills the most basic requirement you have for such a key (and the PHP guys didn’t seem to see any need to explicitly prevent this.)
$arr = [
'foo' => 'bar',
'' => 'baz',
];
print_r would show this as
Array
(
[foo] => bar
[] => baz
)
and var_dump’ed it would look like this,
array(2) {
["foo"]=>
string(3) "bar"
[""]=>
string(3) "baz"
}
The latter makes it more obvious that the key is in fact an empty string, and therefor $arr[""] resp. $arr[''] can be used to access this element.
One might consider this one of PHP’s peculiarities - it does work, but it is hardly ever used in practice, because it just does not make the most sense for most use cases.
The other answers offer good explanation about the empty key that I won't reiterate, but a simple way to get the first item in each of the sub-arrays is to map the reset function which we mentioned in the comments over your main array.
$firsts = array_map('reset', $your_array);
This should work regardless of what the key is.
If I were you, I would begin by asking myself why my array has an empty key, my best guess is that you set your subarrays with keys instead of letting it being indexed incrementally by writing something like this :
array(
'' => 'US', // Maybe a '0' was intended to be there, and it's a type.
'1' => 'U.S.',
'2' => '21',
'3' => '34',
'4' => '33',
// etc...
);
In that case, you may benefit from fixing your code, or at least updating it so that your array is confortable to use, for example by removing the keys so that they are replaced with successive indexes.
Anyway, if you want to use that current array, do this :
echo $array[0][''];
Or iterate through it :
foreach ($array as $sub) {
echo $sub[''],'<br>';
}
If you don't have control over how the array is set, you can also reindex it using array_values(). That function takes an array as an argument and returns its values with successive indexes instead of its original keys :
foreach ($array as $key => $sub) {
$array[$key] = array_values($sub);
}
That code should give you the same array than before with the exception that the empty keys are replaced with 0.

Reorganise array, move indexes to specific locations php

I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.

Combine arrays, count values and total in PHP

After many attempts to crack this I am stuck so I turn to SO for help.
I have two arrays, as below. The keys from both arrays are relational to each other.
I need to combine both arrays together as a key=>value pair.
for example:
[Internet Explorer] => 3
[Internet Explorer] => 2
However, following this I need to total the values of the duplicate keys. Resulting in a unique total key=>value pair for each browser.
for example:
[Internet Explorer] => 5
[Google Chrome] => 3
Thank you for looking, I have tried many array functions and I always come to the same result of getting unique keys without totalised values.
Array
(
[0] => Unknown
[1] => Unknown
[2] => Unknown
[3] => Internet Explorer
[4] => Internet Explorer
[5] => Mozilla Firefox
[6] => Internet Explorer
[7] => Unknown
[8] => Unknown
[9] => Google Chrome
[10] => Google Chrome
[11] => Mozilla Firefox
[12] => Mozilla Firefox
[13] => Unknown
)
Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 1
[4] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => 1
[11] => 1
[12] => 2
[13] => 1
)
Edit: Adding code for clarity.
$agent_list is the result of a query which collects unique instances of USER AGENT and counts them.
The getBrowser function searches each $agents and extracts the browser type.
$agents = array();
$agents_count = array();
foreach($agent_list as $value1)
{
$agent = getBrowser($value1['agent']);
array_push($agents,$agent);
array_push($agents_count,(int)$value1['count']);
}
Assuming the keys array is $keys and the values array is $values, this should work.
$result = array_fill_keys(array_unique($keys), 0);
foreach($keys as $i=>$k){
$result[$k] += $values[$i];
}
Demo: http://ideone.com/9EIOU
Why not iterate over one array and add values if they exists or append if they don't exist:
foreach($array1 as $key => $value){
$array2[$key] += $value;
}
This will ad the value or add the key with the value if it doesn't exist in $array2.
Hope, I understand your question
Let $a,$b are input arrays, result will be in $a
foreach($b as $k=>$v){
if(isset($a[$k]))
$a[$k]+=$v;
else
$a[$k]=$v;
}

Categories