PHP - Converting Information Seperated By Commas In String Into Array?
Hi,
I am trying to find a way that will allow me to convert information passed via POST into array so I can run foreach on the array. For example if information in $_POST[ids] is 1000, 1001, 1002, 1003 I would like to get it into an array $array = array("1000", "1001", "1002", "1003") Thanks Similar TutorialsHi! So what I'm trying to do is split a multi dimensional array of strings ( each containing 3 strings inside of 1 array ) so I can write the results to a file. So say I had a bunch of strings inside 1 array : class File { function WriteFile ( $filename, $content ) { $list = [ $content ]; $delimiters = File :: checkDelimiter ( $content, true, false ); $fp = fopen ( $filename, 'w' ); foreach ( $list as $fields ) { $fields = File :: SplitFileByDelimiter ( $fields, ",", 3 ); file_put_contents ( $filename, $fields ); } fclose ( $fp ); } } $file-> File :: WriteFile ( $filepath . $filename, "TEST!, TEST!, TEST!, " . "TEST!, TEST!, TEST!, " . "TEST!, TEST!, TEST!" ); Instead of writing : TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST! ,TEST!, TEST!, TEST! to the file in a straight line, it should write it like this : TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST! Any help is ABSOLUTELY appreciated! Edited May 22 by AquariaXI I have the following function, which takes a string with commas in it and attempts remove those commas. The way I have it here is that I use explode to take out the commas, which makes an array, and then iterate through that array to put a string back together without the commas. function tags_to_sort_by( $sortMeta ) { $sortByArray = explode(",", $sortMeta); $sortByCount = count($sortByArray); for ($i = 0; $i < $sortByCount; $i++) { $sortByString += $sortByArray[$i]; } return $sortByString; } What does not work is the following line: $sortByString += $sortByArray[$i]; Somehow that outputs a 0, which I don't understand. It should output something like: arrayItem1 arrayItem2 array3 etc. My question is if there either is an easier way to remove the commas from the original string or what I am doing wrong in this line: $sortByString += $sortByArray[$i]; // I am trying to concatenate each part of the array back into the string. Thanks a lot for help with this! I need to some how pull comma separated images $data[23] from this $data array and then put them into there own array so I can insert them into separate rows in a database. This is the provided array: $data[0] => VIN $data[1] => StockNumber ...(all the other number here) $data[23] => Image_URLs (comma seperated) $data[24] => Equipment // I need something like this (obviously doesn't work?!@#) $delimiter = ","; $img = explode($delimiter, $data[23]); foreach($img as $pic){ $sqlPic = "insert into class_prodimages (pid image rank) values('".$LastId['id']."', '$pic', '".rand(1,20)."')"; } Any help here? I have some data in a table and some of it is Artist names stored as "Last, First" I need to be able to have the script search weather or not someone types "last, first" or "first last". Any ideas? Here's my code: <html> <head> <title>search script</title> </head> <body> <form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> </form> <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=100; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("localhost","username","password"); //(host, username, password) //specify database mysql_select_db("mydb") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from songs where Title like \"%$trimmed%\" or Artist like \"%$trimmed%\" order by Title"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned echo "<table border=1>"; while ($row= mysql_fetch_array($result)) { $title = $row["Title"]; $artist = $row["Artist"]; $number = $row["Number"]; echo "<tr><td>$count.)</td><td>$title</td><td>$artist</td><td>$number</td></tr>" ; $count++ ; } echo "</table>"; $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 20 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> </body> </html> I was wondering how you would go about separating words with commas between them into an array. In this case I want users to be able to message multiple other users by entering their names in the following format: user1, user2, user3, user4 and then have php separate those into an array so I can then run them through a foreach(); so they can be sent to the correct users... Any help or functions I should look into would be great... code not necessary, just point me in the right direction thanks! Hi All, I have a php form which is posting a country variable which is currently a number value, but before I post this to a flat text file I want to convert the number into a string (E.G. in the DB 1 = France 3=UK 6-Germany)... I was thinking something along the lines of: $Country = $_REQUEST['Country']; if ($Country =1) {$CountryW = 'France';} else if ($Country =3) {$Country = 'UK';} else if ($Country =9) {$Country = 'Test';} else {$Country = 'Germany';} But this doesn't seem to be working and there would be around 180 Countries so I can see its not efficient. Any help would be much appreciated. If you need more code let me know. Thanks so much for any help Martin I have a dropdown box filled with times in a form: Code: [Select] <select name = "closetime"> <option>Choose One</option> <option>All Day</option> <option>7:00 am</option> <option>8:00 am</option> <option>9:00 am</option> <option>10:00 am</option> <option>11:00 am</option> How do I convert "closetime" to a time format ($closetime) that I can insert into my database? I'm assuming that at some point in the code I'll use strtotime, but I'm not sure where... Inside the option tags?, or after "closetime" has a value?, or after "closetime" is sent to a processing file? Any ideas? Thanks! I have the following DOM document ( this is a sample document and is "as is", it's 100% intended to look like this ): <everything> <cars> <car>ladia</car> <car>ferrari</car> <garage>big blue building</garage> i also have a McLaren F1 but i wont brag about it </cars> <houses> <house>Big Red House</house> </houses> </everything> This is all being put into a $newdoc = new DOMDocument(); $myString = ................. ; // ????? I only want the content of <cars> in here, and without having the <cars> tag How can I make this "$myString look like the following: $myString = ' <car>ladia</car> <car>ferrari</car> <garage>big blue building</garage> i also have a McLaren F1 but i wont brag about it '; Preferably without any looping. Hi, A piece of software I'm using an array is made that looks like the following: Code: [Select] [["Select","Vehicle",0.142933,[0.15,0.4,0.05,0.01]]] How would I be best parsing it so: Square brackets currently showing an array becomes an array within PHP Text strings surrounded by "quotations" lose the quotation mark when stored in the PHP array Numbers remain as values in the PHP array The arrays can become quite large, so which would be the best way to go about it? Regards, Jason Hello, Anyone know a way I could extract information from YouTube. If I have the URL for the video, how could I get the title, runtime, tags and so on into different strings? url = "http://www.youtube.com/watch?v=kMAI26FPeyM"; Thanks Hi, I am very new to PHP and apologies if this question has already been answered in another thread.
I am trying to extract the information from one array into another when some search criteria is matched.
Here is an example array ($original):
Array ( [ 1234567 ] => Array ( [name] => john ) [ 3456 ] => Array ( [name] => johnny ) [ 45673 ] => Array ( [name] => james ) [ 987 ] => Array ( [name] => jamie ) [ 5628721 ] => Array ( [name] => Simon ))
So if I searched for the string john, jo, joh then the new array ($filtered) should be as follows:
Array ( [ 1234567 ] => Array ( [name] => john ) [ 3456 ] => Array ( [name] => johnny ))
I was trying to do this using array_search & preg_match but have been struggling.
Any help would be much appreciated!
Code: [Select] <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php $sushi = array( 'Fresh Tuna Sushi' => array ( 'Fresh Tuna Fillet' => '8 ounces', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons' ), 'Shrimp Sushi' => array ( 'Large Shrimps' => '10 pieces', 'Vinegar' => '2 tablespoons', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons', ), 'Nori Crab Sushi' => array ( 'Nori Seaweeds' => '4 sheets', 'Crabmeat' => '8 ounces', 'Cream Cheese' => '8 ounces', 'Worcestershire Sauce' => '3/4 teaspoon', 'Garlic Salt' => '1/4 teaspoon' ), 'Vegetarian Sushi' => array ( 'Rice Vinegar' => '1/2 cup', 'Sesame Oil' => '2 tablespoons', 'Nori Seaweeds' => '4 sheets', 'Sushi Rice' => '2 cups', 'Cucumber Strips' => '1 cup', 'Sweet Potatoes' => '1 cup', 'Sesame Seeds' => '2 tablespoon' ), 'California Roll Sushi' => array ( 'Sushi Rice' => '1 cup', 'Nori Seaweeds' => '8 sheets', 'Sesame Seeds' => '2 tablespoons', 'Crabmeat' => '1/2 cup', 'Mayonnaise' => '3 tablespoons', 'Avocado' => '1 piece', 'Cucumber Strips' => '1/2 cup' ) ); /* foreach($sushi as $type => $value){ //faster than for loop foreach($value as $ingredients => $measurement){ echo '<br/>' . $type . '-' . $ingredients . '=>' . $measurement . '</br>'; } }*/ ?> <table border="1"> <tr> <th>Sushi</th> <th colspan="2">Ingredients</th> </tr> <?php foreach ($sushi as $type => $ingredients){ ?> <tr> <td rowspan="<?php echo count($sushi[$ingredients]) + 1; ?>"> <?php echo $type; ?> </td> <?php foreach ($ingredients as $name => $meas){ ?> <td><?php echo $name;?></td> <td><?php echo $meas;?></td> <?php } ?> </tr> <?php } ?> </table> </body> </html> So here's mhy code in PHP. I'm trying to create a table from an array but I just can't.........get.........it..........right! The output's supposed to look like this: Please help Any help would be graeatly appreciated. Thank you. Hi there, I am working on an associative array: The array is: $currentIds //when we do print_r($currentIds); //it returns value 41 and 42. Array ( [0] => [1] => 41 [2] => 42 [3] => ) Which is OK but I have to use these two values in an SQL query but they are in Array forms. I need to convert them to integer to be able to use them in SQL query. Please let me know how I can convert this Array to Integer so that i can use the integer values for my SQL statement. Thank you All comments and feedback are always welcomed Cheers! Hi all,
I am using foreach statement to gather records and program levenshtein to detect any misspellings and suggesting the correct words.
The thing is i am unable to convert all those strings into a single array.
After trying to correct this issue so many times, i wonder if it is really possible to convert strings into a single array?
Any input is welcome
Hello all, I'm trying to get better at manipulating arrays and I'm stumped on this one. What would be the most efficient way of converting an array such as this: Quote Array ( [3] => 3 [9] => 1 [15] => 9 ) Into this: Quote 3,3,3,9,15,15,15,15,15,15,15,15,15 Hi all, I have an array $n[] and have made a DB query with fetch_array ($result[]). Now I'm trying to get the information out of the $result[] with $n[] so I made the following and it doesn't work. I checked if they're filled and they are and when I enter in the $result[] array with hardcoded info I do get what I need but I need it trough the array... Can anyone help me out?? Thnx Ryflex $sql = "SELECT * FROM units WHERE member_id = '$member_ID'"; $result = mysql_fetch_array(mysql_query($sql)) or die("Could not find amount of troops"); $troop_one = $result['$n[0]']; $troop_two = $result['$n[1]']; $troop_three = $result['$n[2]']; $troop_four = $result['$n[3]']; Dear All, How can I get information that return from this array Array ( [0] => Array ( [id] => 5 [t_test_date] => 2021-02-11 [t_test_number] => 1 [t_uid] => 202102110001 [t_test_type] => 1 [t_status_id] => 4 [t_test_result_pcr] => 1 [t_test_result_rdt] => 0 [t_test_time] => 2021-02-11 11:39:00 [t_report_date] => [p_first_name] => tesdt [p_last_name] => tesdt [p_gender] => 0 [p_nationality] => [p_date_birth] => [p_passport_no] => [p_personal_id] => [p_issue_country] => [p_date_expiry] => [phone] => [email] => [t_price] => 0 [p_valid] => 1 [created_date] => 2021-02-11 [modified_date] => 2021-02-11 11:40:18 [t_status_type] => ຂັ້ນຕອນທີ່ 4. ອອກການຢັ້ງຢືນ [t_status_typeEng] => Stage 4. Certificate Issued [t_status] => 4. ຢັ້ງຢືນແລ້ວ [t_statusEng] => 4. Issued [test_time] => 11/02/2021 11:39 ) [1] => Array ( [id] => 7 [t_test_date] => 2021-02-11 [t_test_number] => 3 [t_uid] => 202102110003 [t_test_type] => 2 [t_status_id] => 4 [t_test_result_pcr] => 1 [t_test_result_rdt] => 1 [t_test_time] => 2021-02-11 18:38:00 [t_report_date] => [p_first_name] => Hhh [p_last_name] => Tttt [p_gender] => 0 [p_nationality] => [p_date_birth] => [p_passport_no] => [p_personal_id] => [p_issue_country] => [p_date_expiry] => [phone] => [email] => [t_price] => 0 [p_valid] => 1 [created_date] => 2021-02-11 [modified_date] => 2021-02-11 18:38:51 [t_status_type] => ຂັ້ນຕອນທີ່ 4. ອອກການຢັ້ງຢືນ [t_status_typeEng] => Stage 4. Certificate Issued [t_status] => 4. ຢັ້ງຢືນແລ້ວ [t_statusEng] => 4. Issued [test_time] => 11/02/2021 18:38 ) );
I am having trouble parsing data that is separated by comas in an XLS file. The upload and parsing scripts work beautifully, but the problem I am having is the data is read in from one cell (all 5 fields for the row are in column A) I am exploding it by , but some of the cells contain comas. For example a cell might contain "jim,jones,12345678,jim@jones.com,More, Data,192.168.1.1" but the next one might be "Dave,Thomas,98765432,dave@wendys.com,something else, 255.255.255.0" The problem I am having is More, Data should be one cell. Not all position 4 will have a , so I can't just add it back because the IP address would be appended to more... Any ideas? I hope thats clear enough... I've been trying to get this to work for a few days now and can't seem to get anywhere with it! Any help is very much appreciated. I am creating a number generator where the user is required to insert the maximum number and generate rate. Maximum number is the highest number that the script will go to, and generate rate is how many numbers should be created. When the script runs, the numbers are stored into a MySQL database. The MySQL database creates a unique ID number so it can be recalled at a later date. The user then needs to go to Sorting.php and selects the record they wish to use. The numbers from this record are then counted and then stored into an array. If the numbers 2,5,5,0,2,2,1,3,5,1 where generated, the output of this new array will be: [5] => 3 [3] => 1 [2] => 3 [1] => 2 => 1 (These arrays can be of any length) I now need to extract this data and show it on screen. I don't know how to approach it. Format example: Number 5 appears 3 times Number 3 appears 1 times Number 2 appears 3 times Number 0 appears 1 times Sorting.php <?php $db1 = new Number_Information(); $db1->openDB(); $sql = "select * from GeneratedNumber"; $result = $db1->getResult($sql); if (!$_POST) { //page loads for the first time ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> Select the ID (If you have just generated a number, it will be the last one): <!--This is the drop down menu--> <select name="ID"> <?php while ($row = mysql_fetch_assoc($result)) { echo "<option value='{$row['ID']}'> {$row['ID']} </option>"; } ?> </select> <br /> <input type="submit" value="Continue" /> </form> <?php } else { $ID = $_POST['ID']; //This will contain the value that was selected in the drop down menu above. $result = mysql_query("SELECT NUMBERS2 FROM GeneratedNumber WHERE ID='{$ID}'"); //This tells the script to select NUMBERS2 contents which is assosiated with the ID. while ($row = mysql_fetch_array($result)) $NUMBERS2 = $row['NUMBERS2']; echo "$NUMBERS2"; //Testing reasons - remove it later. echo "<br />"; //same as above echo "$ID"; // as above. echo "<br />"; //as above //Convert NUMBERS2 back into an array when inserted into table from Generator.php $NUMBERS = explode(",", $NUMBERS2); // Counts how many times a number appears //******************************************************************************************************************* //IF ALL ELSE FAILS, REVERT BACK TO THIS. //print_r(array_count_values($NUMBERS)); //It will only print the statement, but gives you a good building block when things WILL go wrong. //******************************************************************************************************************* rsort($NUMBERS); print_r(array_count_values($NUMBERS)); (The questions when editing posts are hard. I've needed to Google both of them:() |