PHP - How would one go about combining 3 different arrays into one - php

So i have 3 different ( yet they share some similarities ) and i would like to combine these into one to perform some calculations
Array
(
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
[1] => Array
(
[0] => Amber
[1] => 16
[3] => 2.0%
[4] => 0.2
[5] => 28.6
[6] => 1.8
[7] => 1861.1
[8] => 116.3
[9] => 0.0
[10] => 0.0
[11] => 234862.2
[12] => 3403.8
[13] => 69.0
[14] => 0.8%
[15] => 10
[16] => 6
[17] => 62.5%
[18] => 37.5%
)
[2] => Array
(
[0] => Amie
[1] => 106
[3] => 13.5%
[4] => 3.0
[5] => 721.7
[6] => 6.8
[7] => 12268.0
[8] => 115.7
[9] => 0.0
[10] => 0.0
[11] => 127011.0
[12] => 6350.5
[13] => 20.0
[14] => 9.7%
[15] => 54
[16] => 52
[17] => 50.9%
[18] => 49.1%
)
)
Array 2
Array
(
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
)
Array 3
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
[1] => Array
(
[0] => (7312
[1] => 1
[3] => 0.0%
[4] => 459.8
[5] => 0.0
[6] => 0.0
[7] => 0.4
[8] => 0.4
[9] => 0.0
[10] => 0.0
[11] => 7.8
[12] => 2.6
[13] => 3.0
[14] => 5.5%
[15] => 1
[17] => 100.0%
[18] => 0.0%
)
[2] => Array
(
[0] => Amber
[1] => 414
[2] => 9
[3] => 9.3%
[4] => 6.3
[5] => 1880.1
[6] => 4.4
[7] => 65209.8
[8] => 157.5
[9] => 240.4
[10] => 0.6
[11] => 234862.2
[12] => 3403.8
[13] => 69.0
[14] => 27.8%
[15] => 290
[16] => 124
[17] => 70.0%
[18] => 30.0%
)
So in this example i have 3 arrays, all have the same first initial array. But these arrays can differ in size and they may not share the same people in in.
People can also be in different places so i.e.
Array 1 : Common -> Amber -> Amie
Array 2 : Common ->
Array 3 : Common -> 7312 -> Amber ->Amie
Now the final array should look like
Array 4 : Common -> 7312 -> Amber -> Amie
All the values from all arrays should also be combined so i.e:
Amber 1st array / 1st value : 16
Amber 2nd array / 2nd value : 414
Amber final array value : 414 + 16 = 430
Hopefully i have made question clear.

Basically, you sum up all the info you got, remove the headers. Then rebuild a new array with the info consolidation.
//merge all arrays, remove the first element, it is headers.
$my_total_array = array_merge(
array_splice($array1,1),
array_splice($array2,1),
array_splice($array3,1)
);
$myagents = array();
foreach($my_total_array as $agent){
if(!isset($myagents[$agent[0]])){
//Never met the agent, add them.
$myagents[$agent[0]] = $agent;
}else{
//We already seen the agent, do maths.
$myagents[$agent[0]][1]+=$agent[1];
//TODO: Apply consolidation rules for other fields.
}
}
//Put the headings back at the beggining.
array_unshift($myagents,$array1[0]);
print_r($myagents);
I hope this helps.
EDIT: From PHP Docs
Merges the elements of one or more arrays together so that the values
of one are appended to the end of the previous one. It returns the
resulting array.
If the input arrays have the same string keys, then the later value
for that key will overwrite the previous one. If, however, the arrays
contain numeric keys, the later value will not overwrite the original
value, but will be appended.

Related

Splitting single array into multiple arrays for mysql data insert

I am trying to split a load of data from a text-area that is tab delimited data into multiple arrays.
The process is:
Pasta data into textarea
Post textarea to insert.php
Preg_replace Tabs with commas
Explode on comma
print_r
Date sample being used is:
Dr S Findlay 1234567890 Mickey Mouse 01/01/2001 Elective Waiting 02/02/2002 00:00 Dr one Day Case 03/03/2003 00:00 On Medical Advice Usual Place of Residence
The result I get from this is:
Array ( [0] => Dr S Findlay [1] => 1234567890 [2] => Mickey [3] => Mouse [4] => 01/01/2001 [5] => Elective Waiting [6] => 02/02/2002 00:00 [7] => Dr one [8] => Day Case [9] => 03/03/2003 00:00 [10] => On Medical Advice [11] => Usual Place of Residence [12] => )
This works well and is inserted perfectly into mysql database. However when I use a full data range which can be anything from 2x to 50x the data amount exampled above it displays differently:
More realistic data sample:
Dr S Findlay 1234567890 Mickey Mouse 01/01/2001 Elective Waiting 02/02/2002 00:00 Dr one Day Case 03/03/2003 00:00 On Medical Advice Usual Place of Residence
Dr Peter 1234557890 Mickey1 Mouse1 01/01/2003 Waiting 02/02/2014 00:00 Dr one Waiting Case
Dr Nail 1234569990 Mickey2 Mouse2 01/01/2009 Emergencey 02/02/2018 00:00 Dr one Emergencey 29/05/2018
Result:
Array ( [0] => Dr S Findlay [1] => 1234567890 [2] => Mickey [3] => Mouse [4] => 01/01/2001 [5] => Elective Waiting [6] => 02/02/2002 00:00 [7] => Dr one [8] => Day Case [9] => 03/03/2003 00:00 [10] => On Medical Advice [11] => Usual Place of Residence [12] => Dr Peter [13] => 1234557890 [14] => Mickey1 [15] => Mouse1 [16] => 01/01/2003 [17] => Waiting [18] => 02/02/2014 00:00 [19] => Dr one [20] => Waiting Case [21] => [22] => [23] => [24] => Dr Nail [25] => 1234569990 [26] => Mickey2 [27] => Mouse2 [28] => 01/01/2009 [29] => Emergencey [30] => 02/02/2018 00:00 [31] => Dr one [32] => Emergencey [33] => [34] => [35] => [36] => 29/05/2018 )
What I want to do (but cannot figure out how to do) is split this into arrays so that each line of data (13 columns not always with data in) displays like this so that I can insert all of it into mysql in one go. Im not sure if this will work so please correct me if it won't.
What I want it to look like:
Array ( [0] => Dr S Findlay [1] => 1234567890 [2] => Mickey [3] => Mouse [4] => 01/01/2001 [5] => Elective Waiting [6] => 02/02/2002 00:00 [7] => Dr one [8] => Day Case [9] => 03/03/2003 00:00 [10] => On Medical Advice [11] => Usual Place of Residence [12] => )
Array ( [0] => Dr Peter [1] => 1234557890 [2] => Mickey1 [3] => Mouse1 [4] => 01/01/2003 [5] => Waiting [6] => 02/02/2014 00:00 [7] => Dr one [8] => Waiting Case [9] => [10] => [11] => [12] => )
Array ( [0] => Dr Nail [1] => 1234569990 [2] => Mickey2 [3] => Mouse2 [4] => 01/01/2009 [5] => Emergencey [6] => 02/02/2018 00:00 [7] => Dr one [8] => Emergencey [9] => [10] => [11] => [12] => 29/05/2018 )
As I then presume I can give each part of the array an name $forename = $area[2] ; then use that to insert to the correct field of my database?
Sorry if this doesn't make sense - Or if I'm barking up the wrong tree. All & any advice, help or information is appreciated. Thanks AJ
Hello Brother you explanation is quite confusing but as you
described you Working Process in start so on the basis of that i
like to suggest you below code. Have a try.
NOT SURE WILL WORK BUT WORTH OF TRY
<?php
// explode by tab
$tabbedContent = 'Dr S Findlay 1234567890 Mickey Mouse 01/01/2001 Elective Waiting 02/02/2002 00:00 Dr one Day Case 03/03/2003 00:00 On Medical Advice Usual Place of Residence';
echo '<pre>';
var_dump(explode("\t", $tabbedContent));
echo '</pre>';
?>
at least this may short your process.

Get common values from a single multidimensional array in php

I have a array called $array which is multidimensional array. It consits of keys like brandname, productsubcategory (Keys are not static they can be anything and even number of keys are not fixed). I want all the common values from the array
Current array:
$array = array
(
[brandname] => Array
(
[0] => Array
(
[0] => sony_brandname
[1] => touch screen_type
[3] => 2.8_size
[4] => gsm + gsm_sim_support
[5] => 2_primary_camera
[6] => 64 mb_internal_memory
[7] => 32_expandable_memory
[8] => 1200_standard_battery_type
[9] => 3_size
[10] => 1000_standard_battery_type
[11] => touch and type_type
)
)
[productsubcategory] => Array
(
[1] => Array
(
[0] => karbonn_brandname
[1] => touch and type_type
[2] => micromax_brandname
[3] => 2.8_size
[4] => gsm_sim_support
[5] => 3.15_primary_camera
[6] => 52 mb_internal_memory
[7] => 8_expandable_memory
[8] => 1000_standard_battery_type
[9] => nokia_brandname
[10] => symbian s40_os_clean
[11] => 5_primary_camera
[12] => 128 mb_ram
[13] => 256 mb_internal_memory
[14] => 32_expandable_memory
[15] => 1110_standard_battery_type
[16] => gsm + gsm_sim_support
[17] => 2_primary_camera
[18] => 32 mb_ram
[19] => 10 mb_internal_memory
[20] => no_expandable_memory
[21] => 1020_standard_battery_type
[22] => 680 mhz_processor
[23] => 64 mb_ram
[24] => 128 mb_internal_memory
[25] => 860_standard_battery_type
[26] => blackberry_brandname
[27] => 2.45_size
[28] => 1 ghz_processor
)
)
);
Desired array :
$array = array
(
[0] => sony_brandname
[1] => touch and type_type
[2] => 2.8_size
[8] => 1000_standard_battery_type
[16] => gsm + gsm_sim_support
[17] => 2_primary_camera
)
Use array_intersect():
$result = array_intersect($array["brandname"][0], $array["productsubcategory"][1]);
print_r($result);

Looping over Dynamic Multi-dimensional Array with PHP

I am having difficulty with an multidimensional array. Here is a portion of the array. It's dynamic in that the top level array can have any number of elements and the child arrays, can have any number of elements as well. But each child array will contain the same amount of elements. If one child array has 16 elements, they will all have 16 elements, and if one has 20 elements, they will all have 20 elements.
[1] => Array
(
[1] => 0
[2] => 25
[3] => 38
[4] => 50
[5] => 63
[6] => 75
[7] => 85
[8] => 88
[9] => 100
[10] => 113
[11] => 125
[12] => 138
[13] => 150
[14] => 163
[15] => 175
[16] => 188
)
[2] => Array
(
[1] => 48
[2] => 37.22
[3] => 52.56
[4] => 63.17
[5] => 74.45
[6] => 87.98
[7] => 96.11
[8] => 98.36
[9] => 111.67
[10] => 132.20
[11] => 146.87
[12] => 160.85
[13] => 174.39
[14] => 187.70
[15] => 203.04
[16] => 215.90
)
What I am trying to do is to extract the data in a format like:
ProductCode: [2][1][1][2], Width: [2][1], Height: [1][2], Price: [2][2].
Can someone point me in the right direction of how I can loop over this to extract the data like I need to?
I ended up creating a stored procedure in SQL 2008 to handle this instead of using PHP.

preg_match only 4 digits numbers

Im trying to get all the 4 digit numbers from this website, However there are some numbers that are less than 4 digits for example #20 in the array is value 20.
Array ( [0] => 6280 [1] => 6279 [2] => 6278 [3] => 6277 [4] => 6276 [5] => 6275 [6] => 6274 [7] => 6273 [8] => 6272 [9] => 6271 [10] => 6270 [11] => 6269 [12] => 6268 [13] => 6267 [14] => 6266 [15] => 6265 [16] => 6264 [17] => 6263 [18] => 6262 [19] => 6261 [20] => 20 [21] => 6320 )
This is the script im using:
$pattern = '#<b>([0-9]+)</b>#';
preg_match_all($pattern,$website,$match_number);
Is it possible to only get it if there are 4 digits no less no more.
Thanks
Change the pattern to:
$pattern = '#<b>([0-9]{4})</b>#';

How to sort an array of items as numbers?

I have a number of files I am loading into a slideshow, I have them numbered by the client in the order that they'd like to be displayed.
I'm grabbing them with glob $pics = glob("images/slideshow/*.jpg", GLOB_NOSORT); but for some reason I'm getting the old Windows sorting bug, of 11 being sorted above 2.
For example,
Array
(
[0] => images/slideshow/1.jpg
[1] => images/slideshow/14.jpg
[2] => images/slideshow/15.jpg
[3] => images/slideshow/16.jpg
[4] => images/slideshow/18.jpg
[5] => images/slideshow/2.jpg
[6] => images/slideshow/20.jpg
[7] => images/slideshow/21.jpg
[8] => images/slideshow/22.jpg
[9] => images/slideshow/23.jpg
[10] => images/slideshow/24a.jpg
[11] => images/slideshow/25.jpg
[12] => images/slideshow/26.jpg
[13] => images/slideshow/29.jpg
[14] => images/slideshow/3.jpg
[15] => images/slideshow/36.jpg
[16] => images/slideshow/38.jpg
[17] => images/slideshow/4.jpg
[18] => images/slideshow/40.jpg
[19] => images/slideshow/41.jpg
[20] => images/slideshow/5.jpg
[21] => images/slideshow/6.jpg
[22] => images/slideshow/7.jpg
)
I've run it through asort() and I can't seem to find out why this isn't working, short of it being something about them being strings, rather than strictly numbers.
Check out natsort:
This function implements a sort algorithm that orders alphanumeric
strings in the way a human being would while maintaining key/value
associations.
$array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
print_r($array2);
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)

Categories