Explode Array Created An Automatic Key For Each Row In The Array.
I have a string, like this:
Userid1 Pass1
Userid2 Pass2
Userid3 Pass3
Userid4 Pass4
Each column is tab separated. I want to explode it into an array so that the userid will be the key, and the password the value. I tried to do it like this:
$array = explode(" ", $string); // Where $string is the string above.
However, this returned both the userid and the password as the value, and created an automatic key for each row in the array.
View Complete Forum Thread with Replies
Related Forum Messages:
Array Explode Problem Result In A 6 Item Array
I have the following: $BB_Basket = "basket=¦APL6007,2.00¦AVX8900,1.00¦ARCAV500,1.00"; $basket_containers = explode('¦', $BB_Basket); unset($basket_containers[0]); print_r($basket_containers); This results in Array ( [1] => APL6007,2.00 [2] => AVX8900,1.00 [3] => ARCAV500,1.00 ) Now when I try to explode further with $basket_items = explode(',', $basket_containers); print_r($basket_items); This results in Array ( [0] => Array ) This should result in a 6 item array. Any ideas what I'm doing wrong?
View Replies !
Explode Array
I have an array name, text=value, text=value, name, text=value, name,.... and I want to do something like: for($n = 0; $n < count($a), $n++){ //where $a is the array $b = explode ("=",$a[n]); } So that I get a new array $b looking like name,text,value, text,value.
View Replies !
Explode Into An Array?
I'm trying to do: explode some data into another array. I will have the code in a loop so that the data in all elements of $RowData gets split up. I would like $ZoneCodes[0] to contain 4 elements, but I would like them to be in the next dimension. i.e. [0][0], [0][1],[0][2],[0][3] Code:
View Replies !
Explode My Array
i have a loop which stores the values in the array $store...i am using the following script after storing the values in $store now if $store[0 or whatever]="john doe" i want the $link as "john_doe" i tried ereg_replace but that also dont seem to work..so please guide in either cases Code:
View Replies !
Using Explode, On An Array?
Our website has a spanish and english version, the data for both languages are pulled using an arrays of arrays. I have one file that contains all of the arrays, here is an example of one $products = array{ "dirtbike"=>array("dirtbike_eng","dirtbike_sp"), } A session variable is set (0 for English, 1 for Spanish) and the appropiate translation is pulled out of the array based on that. It works great, easy to update and change things on our site...but now we want to take the task of translating and updating the page out of the hands of our web designer (me) and let operations do it. They want a simple text file that they can edit and just upload to the web server for the changes to take effect...here is an example of what I have given them (translation.txt) ID ------ English ----- Spanish dirtbike | dirtbike_eng | dirtbike_sp I can read the translation.txt file fine using $data=file(translation.txt); Now it puts each line into an array. What I want to do, is use explode on each line (using a ' | ' delimiter) and output each line into it's own array of arrays. Code:
View Replies !
How To Explode Array From File?
contents of myfile.txt = 5035|9638742|11938 // (one line of text) $myfile = "/home/path/public_html/myfile.txt"; $totals = file($myfile); $var = explode("|", $totals[0]); $i = number_format($var[0]); $k = number_format($var[1]); For some reason this is acting funny. I am getting a null value for $k for some reason... Is there a better way to do this?
View Replies !
Array Explode Problem
I have the following: Code: $fontfind .= ཋ,' $fontfind .= ཕ,' $fontfind .= ཟ,' $found = explode(",", $fontfind); When I do: Code: print_r(explode(",",$fontfind)); It prints Array ( [0] => 15 [1] => 25 [2] => 35) but print $found[0]; It prints 1 What's going on?
View Replies !
Explode Into Array Question
I have a 4 character string which can be any 4 characters in any position within the string itself and I want to explode it into an array. Normally I would explode it using a delimeter such as a comma or a space but how do I do it when there is no delimeter??
View Replies !
Have An Array Created But I Need To Search It
I have an array created but I need to search it. The array is created from a mysql call. Code as follows: function respond_check($qid) { include "db.inc.php"; $postchk2 = mysql_query("SELECT * FROM `answers` , `users` WHERE `qid` = $qid AND `a_uid` = `userid` LIMIT 0, 30",$db); while ($userx=mysql_fetch_array($postchk2)) { $poster2 = $u_row22['username']; I've tried including various *if* statements to search for $X in the array (which does exist) but I keep failing miserably.
View Replies !
Explode Array Counting?
I am trying to count the number of pieces of an explode so that I can run a foreach command. My code goes out and grabs work order codes from the database. Then runs and explode command to separate those codes. But I have to count how many codes there are to be able to run a foreach. Any ideas? Code:
View Replies !
Automatic Array Keys
My array keys are behaving strangely. In the manual it says calling an array like $array[] = "something" should make the value be put into the next available integer key available. However, my values are being placed with whole sets of blank keys inbetween. Here's what part of the print_r() of the array returns: Code:
View Replies !
How To Get The Number Of Values In An Array After I Use Explode
I have the following variable which has three areacodes in it: $areacode = "305,954,786" $array = explode(",", $areacode); after i explode $areacode, How to get the info. how many areacodes are there in($areacode). in the above example it is 3 area codes (305, 954, 786) $array[0] equals 305 $array[1] equals 954 $array[2] equals 786
View Replies !
Array Created Based On The Selection Of The Box.
I have a list of e-mail addresses in a selection box (dynamically creatd from a table). Currently the user chooses an e-mail address and sends an e-mail to the appropriate person. How can I make it where they select multiple addresses and send them, I tried just adding the word multiple and of course that didn't work. Do I need to have an array created based on the selection of the box.
View Replies !
Smarty - 'explode' String Into An Array Or 2 Variables
I've a smarty var, like this: {$var} that is equal to: ttttttttttttt|zzzzzzzzzzzz It can be any size, the important is that they are separated by the '|' How do I 'explode' that into an array or 2 variables in the smarty template ? So I can just use: {$var[0]} = ttttttttttttt; {$var[0]} = zzzzzzzzzzzz;
View Replies !
Explode Text Area Into Array For MySQL Table
On my site, I have a textarea where users enter their sites' information in the following format (tab-delimited): Site Name <tab> Site Description <tab> URL <newline> Site Name <tab> Site Description <tab> URL <newline> (and so on...) I know I have to use the "Explode function and a loop to get the data into an array and enter it into a MySQL table. Would anyone have a snippet of code off-hand that could show me how to do this?
View Replies !
Variable To Search An Array Created By MySQL Query
I'm trying to use a variable to search an array created by MySQL query. Let's say I have this query: PHP Code: mysql_select_db($database_myconn, $myconn); $query_rs_cars = "SELECT car_id, car_make FROM cars"; $rs_cars = mysql_query($query_rs_cars, $myconn) or die(mysql_error()); $row_rs_cars = mysql_fetch_assoc($rs_cars); $totalRows_rs_cars = mysql_num_rows($rs_cars);
View Replies !
Create A Comma Delimited String That I Can Convert To An Array Using Explode();.
I am attempting to create a comma delimited string that I can convert to an array using explode();. I have the following code: Code: $cPath_new_a = tep_get_path($categories['categories_id']); $cPath_new_b = str_replace("cPath=", "", "$cPath_new_a"); $cPath_new_c = str_replace("_", "", "$cPath_new_b"); $cPath_new_d = substr("$cPath_new_c", -2, 2); $cPath_new_e = $cPath_new_d . ","; echo $cPath_new_e; This outputs 23,53, I need it to output 23,53 Using the substr($cPath_new_e, 0, -1); and/or rtrim() method to remove the last character for some reason removes all of the commas. Am I approaching this the right way?
View Replies !
Check If The Dates In The Array $holi[] Matches $evalday (not In An Array)
What I'm trying to do is check if the dates in the array $holi[] matches $evalday (not in an array) but with limited success the data in the array is: $holi[1] = 2006-06-20; $holi[2] = 2006-06-28; $holi[3] = 2006-07-19; $holi[4] = 2008-06-24; $holi[5] = 2006-06-30; $evalday = date("Y-m-d", strtotime("+ 1 days"); if($evalday == $holi) { echo("For the purpose of this excercise, $evalday is a holiday"); }
View Replies !
Selecting Array Rows Based On An Array Of Field=>values
Here's the code i have: $db = array(); //main array $db['main'] = array(); //table `main` $db['main'][] = array('id'=>0, 'username'=>'joshfrench','userpass'=>'password','userlevel'=>'admin'); //row $db['main'][] = array('id'=>1, 'username'=>'bob_smith','userpass'=>'psswrd','userlevel'=>'user'); //row function trim_value(&$value){ $value = strtolower(trim($value)); } function fdb_select($fields = "*", $table, $where = false, $limit = false){ global $db; $retv = $db[$table]; //LIMIT RESULTS TO FIELDS if(strrpos($fields, ",") === false){$field=array($fields);}else{$field = explode(",", $fields);}; array_walk($field, "trim_value"); $all_fields = array_keys($retv[0]); if($fields == '*'|| is_null($fields)){$field = $all_fields;}; $field = array_uintersect($all_fields, $field, "strcasecmp"); $retvi = array(); foreach($retv as $num =$row){ foreach($row as $fname =$fvalue){ $fname1 = strtolower($fname); if(in_array($fname1, $field)){ $retvi[$num][$fname]=$fvalue; }; }; }; //DONE LIMITING TO FIELDS if($where != false){ //UP TO HERE IT WORKS FINE //LIMIT TO WHERE CLAUSE $retvj = array(); foreach($retvi as $num =$row){ //IN THE ROW foreach($where as $k =$v){ //IF ROW MEETS ALL REQUIREMENTS if($row[$k] == $v){ $retvj[] = $retvi[$num]; //Keep that row }; }; }; //DONE LIMITING TO WHERE CLAUSE }; $retvi = (isset($retvj)) ? $retvj:$retvi; //SEND A FINAL ASSOCIATIVE ARRAY return $retvi; }; print_r(fdb_select("*", "main", array('id'=>"1", "username"=>"joshfrench"))); the previous code outputs this: Array ( [0] =Array ( [id] =0 [username] =joshfrench [userpass] =password [userlevel] =admin ) [1] =Array ( [id] =1 [username] =bob_smith [userpass] =psswrd [userlevel] =user ) )
View Replies !
Multidimensional Array Syntaxt For Preg_split Array Using Submitted Textarea
I have two textarea's called textarea1 and textarea2. What i did was use preg_split to split the textarea into seperate rows that i could use for analysis like so $array = preg_split("/[ ]?[ ]/", $_POST['textarea2'], -1,PREG_SPLIT_NO_EMPTY); $array2 = preg_split("/[ ]?[ ]/", $_POST['textarea1'], -1, PREG_SPLIT_NO_EMPTY); then i used a function created for me by a nice guy on here called get_ending($filepath) which basically stripped off of each peice of text the "http://www" from every URL i put into the textareas finishing off i wanted to create an array that would input an expression such as: echo "<a href='$array[1]' target='_blank'><img src='$array2[1]' border='0'>"; into a text area on the next page. ALL of this works, except that i dont know how to properly loop throught he array so it'll properly insert the text into the textarea on the next page. For now, all it does is place the array information in the page, rather statically, i would like to run some kind of if then statement that says "while $array has some kind of text in it, keep running this operation of echoing the following expression adding 1 to the end of the array number" I hope that makes sense Any ideas? Code:
View Replies !
Take The Entire Array And Apply The Htmlentities Function To Each Array Element
what I want to do is take the entire array and apply the htmlentities function to each array element. So here is what I am doing: $result = $dbconn->query("select * from tablename"); while($case = fetch_array_html($result)) Here is the function: function fetch_array_html($result) { $arr = mysql_fetch_array($result); foreach($arr as $key=>$val) { $arr[$key] = htmlentities($val); } return $arr; } Here is the error I get: Warning: Invalid argument supplied for foreach() on line 129 What exactly am I doing wrong here? I can usually fix my own errors, but this one is dominating me.
View Replies !
Finding Elements Of An Array In Another Multi-dimensional Array
So I have 2 arrays: one contains userids. It may look like: user_id[0] =12, user_id[1] =30, user_id[2] =43 The other is a multi-dimensional array with fields like: user_info [0] = Array ( [user_id] =13 [user_flag] =1 [url] =http://www.example.com?index,0 ) user_info[1] =Array { Array ( [user_id] =120 [user_flag] =1 [address] =1234 Main St, Anytown, USA [url] =http://www.yahoo.com ) user_info[2] =Array { Array ( [user_id] =130 [user_flag] =1 [address] =134 Main St, Anytown, USA [url] =http://www.google.com )
View Replies !
Array Element Disappears And The Whole Array Collapses And Renumbers Itself
checkbox arrays do not hold their respective positions as do other field arrays that are established to store the contents of selected records. The problem is that when you "uncheck" one of the elements and then repost, that array element disappears and the whole array collapses and renumbers itself with only the remaining "checked" ones -- simply useless when dealing with records in a table where all the field array elements should be in sync. I consider it a bug -- why does the array have to collapse? Why can't it just hold a null value and keep its position? The only fix is to never hold checkbox values in an array. Store them in individual variables numbered like an array, then do an "eval" to retrieve the contents. Here's the whole thing in action: Code:
View Replies !
Sorting An Array Problem, (array/string Manipulation)
I have a function that I found on google, that goes through a directory and spits out a directory tree in an array, with arrays inside of arrays, and more and more arrays inside of arrays, depending on how many subdirectories are there..... Here is a sample "directory tree" it spits out. My problem is wanting to be able to create a new array containing only the paths to all the subdirectories it finds from this array, so it is with array manipulation... Not all paths are located in the topmost array's values, but some directories are within other subdirectories and so forth.. I'm stumped on how exactly to sort through this array, whether recursively or not to grab all the 'paths' in the array period. Code:
View Replies !
Sorting Sql Results In An Array Using Things Outside The Array To Sort Them?
I'm using PHP to connect to a database full of place names, and their associated post codes, latitude and longitude on the earth. A user types his or her post code into a form, plus a distance (in miles), and the script looks for other area codes (first half of the post code) within that distance. I can do that fine, but the results are ordered alphabetically by postcode, and I want to order it by the distance. Problem here, is that the distance isn't part of the array taken from the database, it takes the latitude and longitude of the area it's on in the while loop, and calculates the distance between the user's post code and that one, but it's not part of the array, so I can't sort the array using it. How can I sort it? Can I add the calculated distances to the array, and sort it using sort()? If yes, how do I add them to the array? Here's the page, just to explain better what the script does: http://navimaker.org/ed/owain/results.php?postcode=gl15&distance=34 Mess around with the postcode and distance vars. I'd put the source on here too but I'm doing all this over SSH and can't copy & paste very easily .
View Replies !
Accessing Array Values Inside A Multidimensional Array
I need to access a value which is inside in a multidimensional array... Suppose there is a form and where I takes all the form values inside an array named access..and I post the values into a different array. $registrationArray = $_POST['access']; Now I need to loop through this registration array and get values printed to the new variables... Suppose this is the array $access = array (fname,lname,date,year,month) How do I print the date,year,month values into different variable by looping through registration array..
View Replies !
Trying To Compare Array Values To A Value, Then Create Boolean Array
I have an array that returns data like this (see array below) The array[0] contains all the votes for one story as well as information that pertains to that vote, and the array[1] contains vote information for another story. What I need to do is check is a user_id stored in a session, is in the array, and then create a boolean array from that. For example, here I want to check if the user_id is featured in the first set of data, let's say my user_id is 74. Ok, so it is in the first set of data, so we set the boolean variable to true. Next I need to search the second data field for my user_id, 73. It is not featured here so the value in my array is set to 0. I need an array that returns values either 1, for true, your user_id was found in the table, or false, for your user_id was not featured in the table. They need to be in order of the story too. I'm having trouble one comparing the two variables against eachother, since the array is very deep, and two, creating the array on the fly. Code:
View Replies !
Adding To $_POST Array Is Not Reflected In $_REQUEST Array
Recently I wrote this: <? echo 'action = '.$_REQUEST['action'].' '; ?> <?php $_POST['action'] = 'newmember_split'; echo 'action = '.$_REQUEST['action'].' '; ?> My expected output was: action = action = newmember_split However my output was: action = action = How is this so. As far as I know $_REQUEST is supposed to reflect both $_POST and $_GET. I tried to find some information on it but failed.
View Replies !
Sort Array - [2] => Array( [id] => 232, [zone] => C, [color] => Yellow )
Array: => Array( [id] => 64, [zone] => a, [color] => blue )[1] => Array( [id] => 17, [zone] => b, [color] => grey ) Â Â [2] => Array( [id] => 232, [zone] => c, [color] => yellow ) Â Â [3] => Array( [id] => 36, [zone] => a, [color] => red ) Â Â [4] => Array( [id] => 178, [zone] => a, [color] => white ) Â Â [5] => Array( [id] => 10, [zone] => c, [color] => blue ) Â Â [6] => Array( [id] => 115, [zone] => a, [color] => orange ) Â Â [7] => Array( [id] => 345, [zone] => a, [color] => black ) Â Â [8] => Array( [id] => 19, [zone] => b, [color] => green ) Â Â [9] => Array( [id] => 206, [zone] => a, [color] => red )
View Replies !
2D Array - Count The Size Of Each Array Element
What i want is something like for ($i=0;$i<$var;$i++) { create 2D array if (condition) { push element in array // } } ie..1=>a,b,c,d 2=>e,f,g etc then i want to count the size of each array element eg. for array1 : no of elements 4 for array2 : no of elements 3 etc Not done 2D arrays before.
View Replies !
Array Checking For Page Content In A Array
i am using a function which gets all the links from a page, from that i have a function that gathers the html in the page from each links..this part works great.. i then need to check and see if it has a applet, flashmovie etc tag in the page. if there is a tag like this in the page ineed to return true if there isnt i need to return false i have provided code thats shows how i gather the links echo them out and checks to see if certain html tags are in a page using a function $snoopy->fetchlinks($url); $results=$snoopy->results; $results=array_unique($results); sort($results); for($i=0;$i < $numberoflinks; $i++) { $results[$i] = preg_replace( "/(?<!")((http|ftp)+(s)?://[^<>s]+)/i", "", $results[$i] ); if ($results[$i] == '') { } else { ?> <a href="index.php?mode=2&url=<? echo $url.$results[$i];?>"><? echo $results[$i];?></a> <? $snoopy->fetchform("$url.$results[$i]"); $test=$snoopy->results; / if($test != "") { echo "problems seen<br>"; }else{ echo "no problems<br>"; } } }
View Replies !
Array Sorting Blues....2 Dimensional Array
I have a 2 dimensional array where one col contains a Unix style date and the other contains a filename. I read through a directory to get this info. I can load the array and print it out just fine, but I can't seem to sort it using any of the sort functions! I want to reverse sort them, since they are news stories that I want to display in the correct order by date. When I try to sort I get a "Warning: Cannot compare arrays or objects" message. I have the feeling it's something stupid on my part but can't find any good examples anywhere. Anyone have an idea about this? P.S. I know a database would solve this but sadly it's not an option for me right now...
View Replies !
Turn Array Into Multi-dimensional Array?
How can I take an existing array and turn it into a multi-dimensional array? I want to get a single row out of a MySQL table and have it in an array, but have along side the values the true field (column) length for each of the fields from the table. Here's my stab at it: $userDetails = mysql_fetch_array($resultID); $i = 0; while ($i < mysql_num_fields($resultID)) { $userDetails[$i][2] = mysql_field_len($resultID, $i); $i++; } And I guess the follow up question (if this is indeed possible) is will this work with a multi-row return from the DB also?
View Replies !
Setting Variable To Array Within Mysql_query() Array
I found your site while attempting to solve the problem of utilizing JOIN statements in a PhP mysql_query. Your timely and well-structured assistance to LemonFizz was very helpful, and enabled me to develop the query below. I have now queried multiple tables to produce an associative array of all of a particular student's data after selecting the student's unique ID from a drop-down. This required the use of JOIN statements in my query. The query section of the file is as follows:
View Replies !
Create Array From Members Of An Array Of Objects
I have the following situation: $list[] is an array of MyElement objects. MyElement has two members: MyElement->member1; MyElement->member2; What I want is to get the following: $newlist[] so that: $newlist[0]=$list[0]->member2; $newlist[1]=$list[1]->member2; $newlist[2]=$list[2]->member2; .... I need to do this using A SINGLE LINE OF CODE. Is it possible? How to do this? I tried with: array_walk($list,create_function('$a,$b,$result',' $result[] = $a->member2;'), &$result); but I get the following error: Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of array_walk(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer.
View Replies !
Trying To Calculate An Average Array From An 2d Data Array
I'm trying to calculate an average array from an 2d data array. my problem is that my function does not generate an average array but just one value. but i do not understand why it does not work. code: ----- function calculateAverageRating($rating_matrix) { $average_rating = array(); foreach($rating_matrix as $cus_id => $ratings) { $counter = 0; $sum = 0; foreach($ratings as $pro_id => $item_rate) { $counter++; $sum += $item_rate; } echo $cus_id."->".($sum/$counter)."<br>"; $average_rating[$cus_id] = ($sum / $counter); } echo "<hr>"; echo "<table> <tr>"; foreach($average_rating as $key => $value); { echo "<td>$key::$value</td>"; } echo "</tr></table>"; return $average_rating; } wanted: input -> output 1 1 1 1 2 2 2 2 4 4 4 4 but my functions just does: input -> output 1 1 1 2 2 2 4 4 4 4
View Replies !
Passing Array Of Multidimensional Array To Function
How do I pass a part of a multidimensional array to a function. I have the following (in pseudo): $arrays[m][n]; // filled with values $chosen = 3; function doSomething($array) { // do something with 1D array } This fails: doSomething($arrays[$chosen]); Can someone explain to me why? And how to do this right?
View Replies !
Searching An Array - Search The Second One With The First Ones Array Keys?
I've been banging my head against this one for awhile. One array has numbers like awz001 for keys and the count of how many times they appear as values. There's about 300 key, value pairs in this array. So Array = ([awz001] => 7, etc...) and the other array is a multi-dimensional Array ( [0] => Array ( [number] => awz001 [0] => awz001 [Section] => Section one [1] => Section one ) Is there away to search the second one with the first ones array keys? Would you do it with two loops? First through the multi then the single?
View Replies !
[fopen()] Unzip Uploaded File To Created Newly Created Directory
Right i have already spent the best part of 8 hours trying to get this working. What i am trying to do: Upload zip file to directory. Check if upload is successfull and then unzip to directory created by name of zip file. I can upload the zip file and create the diectory but the damn files wont unzip to the newly created directory ....
View Replies !
|