PHP - Splitting Data Into Arrays
Hi,
I am trying to split a string into an array and then seeing what sort of data it is. eg; I get the following string Quote api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse I need to be able to put this into an array so in this case Code: [Select] myarray[type]=somethingelse myarray[api]=somerandomkeyhere myarray[user]=someuser myarray[password]=somehashvaluehere but it could include many more/less different value's separated by the & symbol. Thanks, -mme Similar TutorialsHey guys,
I have an array of names sorted alphabetically. I'm trying to loop through them and create new arrays keyed by the letters of the alphabet containing the names which start with that letter.
so in the end i'm trying to create something like this:
$newarray = array( 'a' => array('anna','alan','andrew'),
'b' => array('bert','ben','bob'), 'c' => array('cait','carry'), etc,etc ); thanks hey guys just a quick onei having a blank moment i have an array at themoment pulling from a database. i basically want to split this array based on a min and max value. divide it equally and then give it a value on each smaller array and where each array starts... to make it clearer - say i have an array with 600 values in i want it split up into 8 smaller arrays link so Array ( => Array ( [start] => 0 [split] => 75 ) [1] => Array ( [start] => 75 [split] => 75 ) [2] => Array ( [start] => 150 [split] => 75 ) carrying on till the end ) how would i do this. Hi I have a function that takes a postcode and splits it so i can enter into DB into two formats, full postcode and prefix. The function is: function check_form_postcode($postcode) { $postcode = strtoupper(str_replace(chr(32),'',$postcode)); $suffix = substr($postcode,-3,3); $prefix = substr($postcode,0,(strlen($postcode)-3)); if (preg_match('/(^[A-Z]{1,2}[0-9]{1,2}|^[A-Z]{1,2}[0-9]{1}[A-Z]{1})$/',$prefix) && preg_match('/^[0-9]{1}[ABD-HJLNP-UW-Z]{2}$/',$suffix)) { $postcode_syntax_check = 1; } else { $postcode_syntax_check = 0; } if($postcode_syntax_check){ $return = $prefix."::".$suffix; } return($return); } I POST the form data and in the top of the same page I get all of the form data and here is is where things go wrong: if ($b == 'go') { //Gets form data $main_event_name = my_import('main_event_name', 'P', 'TXT'); $main_event_type = my_import('main_event_type', 'P', 'TXT'); $main_event_date = my_import('main_event_date', 'P', 'TXT'); $main_event_city = my_import('main_event_city', 'P', 'TXT'); $postcode = my_import('postcode', 'P', 'TXT'); $main_event_added = my_import('main_event_added', 'P', 'INT'); $main_event_active = my_import('main_event_active', 'P', 'INT'); $postcode_array = check_form_postcode($postcode); $postcode = explode($postcode_array, "::"); $prefix = $postcode[0]; $fullpostcode = $postcode[0]." ".$postcode[1]; $valkey = md5(microtime()); $info = array( "main_event_name"=> $main_event_name, "main_event_type" => $main_event_type, "main_event_date" => $main_event_date, "main_event_city" => $main_event_city, "main_event_active" => $main_event_active, "main_event_added" => $sys['now'], "main_event_pcode" => $fullpostcode, "main_event_pcode_prefix" => $prefix, "eo_id" => $eo['id'] ); $table = $db['main_event']; $userid = my_insert($info, $table); When I enter into the DB all I get is the "::" no data. I know that the POST works as I can enter the data easily without the function going wrong. Thanks for any help, i am new to arrays go easy if I have made an obvious mistake! Cheers... Basically I have a date of birth field (date) now how would I go about splitting that date field into 3 variables $year $month and $day? Any small push forward is much appreciated, I have searched but it seems that I might not be putting it into words correctly. Actually while writing this I suppose I might of thought of the solution Code: [Select] <?php $year = date('Y', strtotime($row['dob'])); $month = date('m', strtotime($row['dob'])); $day = date('d', strtotime($row['dob'])); ?> Would that be how you would go about it? Hello I am after some help with a concept to allow us to import some data into our database. I have a PDF which I have OCR'd and now have a word document... I then manually clean up the document to remove spurious characters, that part is fine. Now, the word document contains an unknown number of lines and an unknown number of records... and within those records an uknown number of fields... Here is an example of a single record: Mr G Aldred, 26/11/08, Canvardine Chance, Wolfies Dawn Tilley, KENINE SECRET DESTINY, d, c. Grey & White, KENINE SHADES OF THE NIGHT, d, c. Grey & White, KENINE SILENT WHISPER, b, c. Seal & White, KENINE SOFT KISSES, b, c. Grey & White, KENINE SPIRIT OF THE STORM, d, c. Seal & White KENINE STAR QUALITY, d, c. Seal & White And I could have multiple records like this, but with different number of names toward the end So, if I manually put some delimiting character instead of coma's , can I get PHP to read the WHOLE document in, split the document into records and then split those records into subfields to populate a form which I can then automatically submit to my database? Not sure if that is clear.... Thanks Hello , I have a table like $data[$var][$i] $data is an string, $var is a string and $i is an index int. I am tired to write for($i=1;,$i<=count($$data[$var],$i++) { echo $data['id'][$i] ... }oouuuff How i can make it with foreach ? I tried foreach ($data -> $field as $value) echo $value; but i doesnt work. Any advise ? Hi There all, having an issue with simple xml... hopefully someone will be able to shed some light on this. I have this data in an xml feed from an API: <ID>job no 1 </ID> <Name>job name</Name> <State>job status</State> <Tasks> <Task> <Name>task name 1</Name> </Task> <Task> <Name>task name 2</Name> </Task> <ID>job no 2</ID> <Name>job name</Name> <State>job status</State> <Tasks> <Task> <Name>task name 1</Name> </Task> <Task> <Name>task name 2</Name> </Task> I would like to display these in a table pretty much as it is shown above. Job no 1 job name job status job 1 task name 1 job 1 task name 2 Job no 2 job name job status job 2 task name 1 job 2 task name 2 etc my code looks like this : $required is narrowing down specific states not shown for clarity $xml_current=simplexml_load_string($jobs_task_response) or die("Error: Cannot create object"); foreach($xml_current->Jobs->Job as $item_current) { if (in_array((string)$item_current->State, $required)) { $projects_current[] = array( 'job_no' => (string)$item_current->ID, 'job_name' => (string)$item_current->Name, 'job_status' => (string)$item_current->State, ); foreach($item_current->Tasks->Task as $current_tasks){ $projects_task[] = array( 'job_tasks' => (string)$current_tasks->Name, ); } } } foreach ($projects_current as $proj_current) { $job_no =$proj_current['job_no']; $job_name =$proj_current['job_name']; $job_status =$proj_current['job_status']; $clr_current = $colors[$job_status]; $project_id = $job_no; $tdata_1 .= "<tr id='current' class='card-body collapse-show'>"; $tdata_1 .= "<td class='th-sm-1 bg-white '><a href='../details/index.php?pid=$job_no' class='text-left ml-1'>$job_no</a></td>"; $tdata_1 .= "<td data-target='#" . $job_no . "' data-toggle='collapse' class='th-sm-2 bg-white text-left ml-1'>$job_name</td>"; $tdata_1 .= "<td class='th-sm-2 " . $clr_current . " text-left ml-1 '>$job_status</td>"; foreach ($projects_task as $proj_tasks){ $job_tasks =$proj_tasks['job_tasks']; $tdata_1 .= "</tr>"; $tdata_1 .= "<tr id='" . $job_no . "' class='card-body collapse'> "; $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>"; $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>"; $tdata_1 .= "<td class='th-sm " . $clr_current . " text-left ml-1'>$job_tasks</td>"; $tdata_1 .= "</tr>"; } } what I get with this code however is; Job no 1 job name job status job 1 task name 1 job 1 task name 2 job 2 task name 1 job 2 task name 2
Job no 2 job name job status job 1 task name 1 job 1 task name 2 job 2 task name 1 job 2 task name 2
I'm sure there is a simple answer to this, I just can't seem to put my finger on it. Can anyone help please? Much appreciated thanks in advance! Hi wondering if there's a simple way to merge or update the data from one array to another . I've tried merge data but it just adds additional records rather than merging them off one common key.
The array is just simply adding a name to another array the format of both is as follows
Array 1
[0] => stdClass Object ( [name] => [market] => Football [selection] => 7051575 ) ) Array 2 Array ( [0] => stdClass Object ( [name] => Something [market] => [selection] => 7051575 ) ) There are other fields but I'm just trying to merge the name from Array two into Array one using the selection as a common key. Is there an easy command like merge or do I have to loop thru the arrays to add the data. Thanks Hi there, I am wanting to insert post data into my DB, this is guest information if i do a $safePost = filter_input_array(INPUT_POST); print_r($safePost); i get the following output. Array ( [action] => addGuestInfo [guestno] => 2 [bookingid] => 151 [customerid] => 22 [Guestname-0] => Jamie [Guestname-1] => Joe [GuestAge-0] => 4-3 [GuestInfo-0] => celiac [GuestAge-1] => 18 [GuestInfo-1] => wheat ) normally the guestname-0, guestage-0, guestinfo-0 would be in correct order but we have to ensure the just incase it isns't (see output above) we cater for that aswell, we can have upto 6 guest details coming in at once. so I need to loop through the data and insert into my DB. I have tried various for / foreach loops but cant seem to get it to group the data together I have tried things like below, but it inserts each record multiple times. $safePost = filter_input_array(INPUT_POST); foreach($safePost as $key => $value){ $exp_key = explode('-', $key); // for ($i =0; $i <= $guestcount; $i++) { switch ($exp_key[0]) { case 'Guestname': $booking->guestno = $exp_key[1]; $booking->guestname = $val; break; case 'GuestAge': $booking->guestno = $exp_key[1]; $booking->guestage = $val; break; case 'GuestInfo': $booking->guestno = $exp_key[1]; $booking->guestinfo = $val; break; default: break; } //CALLS FUNCTION TO INSERT TO DB result = lastInsertId(); $result= $booking->insertGuestDetails(); //} }
i have a text file that has spaces between each column. eg 12 12 13 22 34.5 10 13 18 88 32.5 12 33 17 23 22.3 (the actual data is weather data that accumulates a new line every 15 mins and is seen at www.maidenerleghweather.com/clientraw.txt ) each column represents a weather variable. i would like to use php to find the maximum values in each column and minimum values etc etc. how can i get the text data into arrays, so that i can then easily perform some simple stats on it? HOWEVER each line would NOT be an array. the first value in each line makes up array number one, second makes up two etc any ideas for a newbie beginner would be welcomed. Ok so I'm trying to use jpgraph which allows you to make graphs with php (you dont need to know anything about jpgraph to help me) anyway how it works is this line data = array(1,3,4,5); the numbers here are what the graph plots. now i'm trying to figure out a way to place values from my database in there. the problem is the amount of values is constantly changing. I tried a lot of things but nothing is working here is my last try: $result = mysql_query("SELECT thismonth FROM list"); while ($row = mysql_fetch_array($result)) { $data[] = $row[0]; } $number = count($data); for ($i = 0; $i < $number; $i++) { $wtf .= $data[$i] . ',' ; } now when i echo $wtf i get values such as 6,3,2,0,1 now i tried this $data = array($wtf); but it doesnt work any ideas? Hi,
please help me to solve my query... I was incurring a problem in executing a PHP code which executes 'n' number of data sets..
For example -- in the below shown outputs, I need to eliminate executing the 4th output which is similar (repeated data) to the 1st output,
input output
4,4,5,6,5 4,4,5,5,6
4,4,5,7,5 4,4,5,5,7
4,4,5,7,6 4,4,5,6,7
4,4,6,5,5 4,4,5,5,6
Kindly send me the function details to execute it without repetition.
Thanks a lot for your support!
Regards,
Maanu
Heya guys! What's up? OK here's my problem: I got a variable, $var1, which is to have values like "groan,100,200,350,50" I need to know how to split this $var1 into $split1, $split2, $split3, $split4, $split5, where $split1 = "groan"; $split2 = "100"; $split3 = "200"; $split4 = "350"; $split5 = "50"; Thanks in advance people! P.S. In my case I am going to use some of the values for calculations. So I don't think that its easy with arrays, which are difficult for me to understand . P.S.S. When I am retrieving data from the database, many variables like $var1 that need to be split are being extracted. I hope that you understand. Thanks and Regards, Thauwa http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/get_hit_list?user_id=100000952273457&target_id=100000556624340&level=300&auth_key=cb07c7575b38df48d82dd53619385faa884db5a2 When going to this URL, it gives entries between <entry> and </entry>, what I am trying to do, is collect the <user_id> ONLY if the <amount> is over a certain value. Any ideas to how I could do this (I am a complete PHP noob). All help is appreciated I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
I have a string like this: Code: [Select] Something,Somthing Else,"keyword1,keyword2,keyword3",Yet another I need to split that string by the ,. However, I need the data that is n the quotes to remain as one even though it has commas because it is one item. I assume I need to use preg_split and have tried some code but none of it seems to work properly. If anyone has a better method or knows how I can split this up using preg_split the help is appreciated. I could use some suggestions on how to break up my HTML and PHP. Currently I have a file called "payment_form.php" It looks like this... Code: [Select] <body> <?php // HANDLE FORM. if (isset($_POST['submitted'])){ // Handle Form. // Determine if any errors. if (empty($errors)){ // Process Payment. // PHP code to send payment to Payment Gateway... // Do not return to Payment Form!!! exit(); } } ?> <!-- HTML PAYMENT FORM --> <form id="payment" action="" method="post"> <fieldset> <legend>Billing Details</legend> <ol> <!-- First Name --> <li> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" class="text" type="text" maxlength="20" value="<?php echo $firstName; ?>" /> <?php if (!empty($errors['firstName'])){ echo '<span class="error">' . $errors['firstName'] . '</span>'; } ?> </li> I would like to keep the Form Validation PHP in the same file as the HTML Form so the user isn't ping-ponged between two pages if they make data entry errors. However, I am thinking it would be better to break out the Payment Processing PHP and place that in a different file. How should I do that? Maybe I could add a Redirect after this code... Code: [Select] if (empty($errors)){ // Process Payment. // PHP code to send payment to Payment Gateway... Suggestions welcome!! Thanks, Debbie Hi guys Say for example I have a text field with value 20/10/2011 how do I define it so that $x=20, $y=10 and $z=2011 so that I have three variables Thanks |