Splitting single array into multiple arrays for mysql data insert - php

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.

Related

Empty tag reading out CSV to array

I'm converting a csv file to an array.
Then I'm reading out the array with php and do a foreach.
The strange thing I have know is that for a certain product an item in the array does not get a value.
To be more clear. When I do a print_r of they array I get following output:
[2494] => Array
(
[0] => 874082
[1] => DVR DL 8X,USB2.0
[2] => Samsung
[3] => SE-208GB/RSBD
[4] => Black,Ultra Slim,Retail
[5] => PARENT PRODUCT GROUP
[6] => External DVD burner
[7] => S
[8] => 8806086112031
[9] => 45
[10] =>
[11] => 17,97
[12] => 0,04
[13] => 0,00
[14] => 0,00
[15] => 0,00
[16] => 0
[17] => SE-208GB/R
So on [11] you can see the price.
But when I echo this field it is empty. While for all other products I can echo the price.
I can not find an answer for this problem. Can anybody help me troubleshooting this?
Kr,

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

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.

PHP mysqli_fetch_array missing some fields

I have crated a database that stores information about boats, when I tried to use this data in PHP it worked, then I added some new fields to the database but when I use them in PHP they return "Undefined index: fieldName".
From what I know it means that the index has not been set, so I checked the contents of the array to find that all of the fields are there except the new ones, however i am able to view them in phpmyadmin.
PHPMyAdmin:
See Image: https://dl.dropboxusercontent.com/u/65222600/phpmyadminshot.png
PHP Code:
<?php
include_once "php/connect.php";
$result = mysqli_query($con,"SELECT * FROM vessels");
while($row = mysqli_fetch_array($result)){
print_r( $row );
}
?>
PHP Returns:
Array (
[0] => 1 [idVessels] => 1
[1] => 1 [userId] => 1
[2] => cams boat [name] => cams boat
[3] => 1000000000 [price] => 1000000000
[4] => 0 [VAT] => 0
[5] => GBP [Currency] => GBP
[6] => UK [Location] => UK
[7] => ME [builder] => ME
[8] => ME [make] => ME
[9] => ME [model] => ME
[10] => 10/10/13 [yearConstructed] => 10/10/13
[11] => 1 [cabins] => 1
[12] => 1 [heads] => 1
[13] => 1 [#engines] => 1
[14] => ME [engineModel] => ME
[15] => 100 [enginePower] => 100
[16] => petrol [fuelType] => petrol
[17] => 122 [nominalLength] => 122
[18] => 122 [overallLength] => 122
[19] => 122 [waterlineLength] => 122
[20] => 122 [beam] => 122
[21] => 122 [maxDraft] => 122
[22] => leather [hullMaterial] => leather
[23] => MINT [hullType] => MINT
[24] => MINT [keeltype] => MINT
[25] => 122 [displacement] => 122
[26] => 122 [waterCapacity] => 122
[27] => 2014-01-10 02:18:39 [dateAdded] => 2014-01-10 02:18:39
[28] => For Sale [status] => For Sale )
and nowhere is description or primaryImageURL.
any help is greatly appreciated, Thanks
Thanks for all the help guys, turns out i was using the project DB not my Dev DB
Try to reference explicitly fields names:
$result = mysqli_query($con,"SELECT IdVessels, userId, name, description, primaryImageURL FROM vessels");
That way you'll be able to find the problem more quickly. If the db answer that the fields doesn't existe, probably you are pointing your connection to an old database.

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>#';

Finding decimal numbers in a string via regex

I got decimal problem while I parsing my command. 12.90 has been parsed wrongly:
// My command
$cmd = 'CREATE product price:12.90, name: "Create me"';
// My parser
preg_match_all('/\w+|".*?"|(?!\s)\W/', $cmd, $list);
Output:
Array
(
[0] => CREATE
[1] => product
[2] => price
[3] => :
[4] => 12 // << problem starts here
[5] => .
[6] => 90
[7] => ,
[8] => name
[9] => :
[10] => "Create me"
)
I am looking for this output:
Array
(
[0] => CREATE
[1] => product
[2] => price
[3] => :
[4] => 12.90 // supposed
[5] => ,
[6] => name
[7] => :
[8] => "Create me"
)
So, how can I solve this problem?
Edit: (the better solution)
Guys, I accepted Jesse's answer according to my question. But I realized it is not good enough for my complex commands. It's better, because it works with both decimals like 12.90 and alias names like p.price. So just look at my example and parser below for it. I hope this help someone.
// My command
$cmd = 'GET `order` -o, product -p
LIST o.user_id, o.product_id, p.name, p.price
REL o.order_id = 3 AND p.price > 12.90';
// My complex command:
preg_match_all('/[0-9_\.]+|\w+|".*?"|`.*?`|\'.*?\'|!=|<=|>=|(?!\s)\W/', $cmd, $list);
// Output:
Array
(
[0] => Array
(
[0] => GET
[1] => `order`
[2] => -
[3] => o
[4] => ,
[5] => product
[6] => -
[7] => p
[8] => LIST
[9] => o
[10] => .
[11] => user_id
[12] => ,
[13] => o
[14] => .
[15] => product_id
[16] => ,
[17] => p
[18] => .
[19] => name
[20] => ,
[21] => p
[22] => .
[23] => price
[24] => REL
[25] => o
[26] => .
[27] => order_id
[28] => =
[29] => 3
[30] => AND
[31] => p
[32] => .
[33] => price
[34] => >
[35] => 12.90
)
)
\w class does not include the . so the "quick" fix is to add it as follows:
preg_match_all('/[\w.]+|".*?"|(?!\s)\W/', $cmd, $list);
Do you have any other requirements regarding where the period is allowed? (ie only matching when it is between numbers?). If so, you may need to make the matching more specialized.
The biggest thing thing that jumps out at me is if the period would be used as delimiter for some other aspect of your parser. If the RE that you gave is your entire parser you are probably ok.
This works for me:
/[a-zA-Z0-9_\.]+|:|,|".*?"/

Categories