PHP - Filling Array With Both Keys And Values In Loop
Similar Tutorialshey guys im trying to result an array key and value if it has one...the code and results are below if anyone can help with this simple problem...thanks alot Code: [Select] <?php $columns = array('news_id' => 'id', 'news'); foreach ($columns as $column => $column_alias) { echo $column . ' ' . $column_alias; } ?> the result im getting is: news_id id 0 news but im just after the result of just result the column name and alias (if it has one) so im looking for the result news_id id news Hi there, Basic question, but I can't seem to find a page in the PHP docs that answers (though I'm sure such a one exists, I just can't seem to think up the right search terms - anyway) My question is: is there a short-hand way to use a value from an associative array as the key in another associative array? So for example, given this array: $the_array = array(0 => array('name': 'the_name', 'value' : 'the_value'), 1 => etc....); then this kind of foreach loop doesn't work Code: [Select] $new_data = array(); foreach($the_array as $element){ $new_data[$element['name']] = $element['value']; } Obviously I can just save out the values, like $name = $element['name'] --- but I was just wondering if there was a more efficient way to do it? Cheers, PS. I tried encapsulating with {} - i.e. $new_data[{$element['name']}] --- but that also fails. This is really blowing my mind... the code is as follows the table, the column names everything is right and contains information but the variables aren't being filled... I don't get it... Anyone have any ideas or options I might not have thought of for debugging? $item_name = ("SELECT * FROM items WHERE id = '$grab_item'"); $que_items = mysql_query($item_name); while ($item_todel = mysql_fetch_array($que_items, MYSQL_BOTH)) { $name = $item_todel['name']; $desc = $item_todel['desc']; } echo ($name . "<br>" . $desc . "<br>" . $grab_item); the only variable thats set is grab_item, fetch_array throws no errors, if I plug in the query straight to the sql I get all the proper information... It's just not filling the two variables.... I need some help converting my captured array values into keys and later add arrays as the values. This might help explain what I'm looking for: I currently have Code: [Select] Array ( [0] => Meat [1] => Veg ) But I'm after Code: [Select] Array ( Meat => Veg => ) My end result will be: Code: [Select] [Veg] => Array ( [Potatoe] => 12 [Cabbage] => 24 [Cucumber] => 56 ) [Meat] => Array ( [Chicken] => 23 [Beef] => 90 ) I think either array_values() may help but I'm not too sure how to use it. What I'm trying to do is take two arrays and combine them using array_combine. After they are combined I want to take each key and value pair and use them in a string to build a MySQL query. See below for some pseudo-code =D <?php $fields = array('first_name', 'last_name'); //the fields that will be set with an UPDATE query. $newvals = array('Joe', 'Blow');//The values that will go into $fields $arr = array_combine($fields, $newvals);//Each value now has a key of the field it will update. /** This is where I want to return my SET string with something like 'SET ' . $arr['first_name'] ' = ' . $arr['first_name_value'] ', etc etc. */ There it is, I suppose. Are there any easier ways of doing what I'm trying to accomplish, that is, am I on the right track or just making things harder for myself? Both arrays will have dynamic values throughout my application, so I need to be able to get both each field and value for each query. Any links to some constructs, etc, etc? That's all I really need and I can post the solution when I've figured it out. Say I have this array: Code: [Select] <?php $array = array('one' => array(), 'two' => array(), '_three' => array()); ?> How could I sort that array so it ends up like so: Code: [Select] Array ( [_three] => Array ( ) [one] => Array ( ) [two] => Array ( ) ) Thanks!!
hi, i’m currently facing a new problem (i managed to use array_unique function successfully but i face this problem)with my php loop. i couldn’t seem to increment in my foreach loop to get the value of an array. $update_id1=Array ( [0] => 398 [1] => 397 [2] => 393 [3] => 391 [8] => ); //$i=0; foreach ($update_id1 as $i => $v) { $ex= explode(",", $v); $unique=array_unique($ex); //f$ketch username from update table in db and inject it to the feed query. $imp_id= implode(',', $ids); //$id1=$ids[$i]; // $x=0; // for($x=0;$x<count(unique);$x++){ echo 'ids:- '.$i; print_r($unique[$i]); echo '<br>'; //} $totalUpdates=$project->totalUpdates($ids[$i],$_SESSION['uname'] ,$unique[$i],$load); //$i++; } i tried every method nothing seems to work can anyone help me in this regard please. Edited December 25, 2019 by narutofanHi I am new this forum and looking for some much appreciated help with something that has me stumped. I have extracted regions from image names in a directory. example: 'edinburgh_castle_(Edinburgh).jpg' (Extracted Edinburgh from in-between the brackets) So I have a list of Regions, extracted from all the various image names in the directory which I want to populate into a dynamic drop down menu for filtering purposes. Here's my issue... I want there to be only one 'Edinburgh' option appearing in my drop down menu. I don't want duplicates. Here is my code so far. php: [Select] Please see the following code:
$colors = ["red hello","green this","blue lovely","yellow morning"]; foreach ($colors as $towns) { $hello = (explode(" ", $towns)); }If I do print_r($hello[1]); within the loop I get all the values of index [1] in the browser although not sure why there are no spaces between the words: HelloThisLovelyMorning However if I do the same print outside the loop I only get this displaying in the browser: Morning That's no good to me because I want all the values with the index of [1] so I can use them anywhere I want. Any help would be much appreciated. Hi all, I am not sure if this is possible but I am trying to provide people a link to a page (not mine btw) with values already filled in. So for them it's faster to submit the form. Problem is the Form is using method post. what i tried was the following but it didn't work. there is an input field: Code: [Select] <input type="text" value="" maxlength="255" size="10" id="numpoints" name="numpoints" class="inputbox required">So i thought if I just make an url like : http://godzillaeatsbananas.com/index.php?numpoints=100 it would fill in the field with a value of 100 but it doesn't Anyone might have an idea on how to accomplish this. Thanks! I have a database query set up that returns an array. I then cycle through the rows in a foreach statement that wraps each value in a <td> tag to output it to a table. It works really great, but now I need to access two of the values and add some info to them. The first field returned is an image filename and I want to wrap it in an image tag that also concatenates the image path location to the value. All the images are stored in the same location, so basically I want to take the first value form the array and add the path to where the images are and wrap the whole thing in an <img> tag. The other field I need to modify is the last value in the array which is actually an email address. For that field I want to make them an active email link so I need to wrap an <a>href=mailto: </a> tag around the value. How could I modify this code so I could add the necessary tags to these elements of the array? Here is the code I am running: Code: [Select] $query = "Select member_image as 'Image', member_name as 'Name', city as 'City', phone as 'Phone', email as 'Email' FROM directory"; //connect to database $conn=db_connect(); //call function do_query to run the query and output the table do_query($conn, $querey); The functions called are as follows: Code: [Select] function db_connect() { $conn = new mysqli("localhost", "username", "password", "databasename"); } function do_query($conn, $query); { $result = mysqli_query($conn, $query); WHILE ($row = mysqli_fetch_assoc($result)) { If (!$heading) //only do if header row hasn't been output yet { $heading = TRUE; //so we only do it once echo "<table>\n<tr<>\n"; foreach ($row as $key => $val) { echo "<th>$key</th>"; } echo "</tr>\n"; } echo "<tr>"; foreach ($row as $val) { echo "<td> $val </td>"; } echo "</tr>\n"; } //close the while echo "</table>\n"; } Hi all, I have a situation where I need to remember what check boxes where checked over pagination, I have managed to do this via the use of this: http://jamesfunk.com/wordpress/?p=65 The problem is that as the code stood: Code: [Select] <input type="checkbox" name="compare" value="<?php echo $list['jobseeker_id'];?>" class="remember_cb"/> It was treating one ticked checkbox as them all because they all have the same name and are in the a while loop! I countered this by changing the code to: Code: [Select] <input type="checkbox" name="compare<?php echo $list['jobseeker_id']?>" value="<?php echo $list['jobseeker_id'];?>" class="remember_cb"/> Which effectively now makes the checkbox name unique... i.e. compare{id}. The problem with this is that I can now no longer process it. This is my processing code: $jobseekers = $_POST['compare']; $i = 0; for($i; $i<count($jobseekers); $i++){ $query = "SELECT * FROM jobseekers WHERE jobseeker_id = '$jobseekers[$i]'"; $result = mysql_query($query) or die (mysql_error()); while ($row = mysql_fetch_array($result)) { // Spit out the required data } } As you can see I am trying to get the data from $_POST['compare'], which will obviously now be blank as I have had to make the name unique.... the trouble is I'm not sure how to actually process this. Can anyone help me out here? any help or advice would be greatly appreciated! many thanks, Greens85 This: Code: [Select] echo "<pre>"; print_r($torrent->content()); echo "</pre>"; Returns: Code: [Select] Array ( [Eureka.S05E01.HDTV.XviD-ASAP\Eureka.S05E01.HDTV.XviD-ASAP.avi] => 367011826 [Eureka.S05E01.HDTV.XviD-ASAP\eureka.s05e01.hdtv.xvid-asap.nfo] => 4798 [Eureka.S05E01.HDTV.XviD-ASAP\Sample\eureka.s05e01.hdtv.xvid-asap.sample.avi] => 7883092 ) Needs to be inserted to MySQL like: In other words, the array $torrent->content() needs to be inserted to MySQL as new lines at columns "filename" and "size". Please help me.. how to connect juice and 1 and milk and 3 and cola and 2? and then sort it by the numbers from high to low? $a=array('juice','milk','cola'); $a2=array('1','3','2'); atm i have this in a function: Code: [Select] $returnInfo = array(); $returnInfo["totalWords"] = $totalWords; $returnInfo["uniqueWords"] = count($uniqueWords); $returnInfo["positiveWords"] = $positiveWords; $returnInfo["negativeWords"] = $negativeWords; $returnInfo["rating"] = $mappedrating; return $returnInfo; If i recursive print it it says Array instand of my expected values. p.s., i have found examples where the array get's created in 1 line with all there values but i prefer not to do that since it will be a very long line. Is it possible to create a PHP array that contains only keys? array 1 3 5 6 7
I have a query that returns row ID's that I want to put into an array to do some work. There is no real concept of a "value" - I just need to store a list of the database table rows that i want and that's it! Edited February 16 by SaranacLake Hi .. i have an array format like (mysql result array) $new_array=Array ( [0] => Array ( [quantity] => 5 ) [1] => Array ( [quantity] => 25 ) [2] => Array ( [quantity] => 20 ) ) I have to convert this array into the following format $new_array['quantity'][0]=5 $new_array['quantity'][1]=25 $new_array['quantity'][2]=20 How to convert this array format using any standard array method. is there a way to reset the array keys? to 0,1,2,3,... also for example that i have something like this: $array['dog']='dog'; $array['mouse']='mouse'; $array['cat']='cat'; and i want it like this: $array[0]='dog'; $array[1]='mouse'; $array[2]='cat'; is there a function for that already in php? Hello I am having some trouble with array_keys I have an array for which I want to return all the keys based on value X This is my code: Code: [Select] $depth = array_keys(X', $Array); echo print_r ($depth,1); I have checked the content of my array $Array and it contains at least 1 instance of 'X' but I am getting nothing when I do the print_r Any suggestions would be appreciated Thanks I have an array of type Code: [Select] Array ( [2] => Array ( [name] => Shelby Baptist Medical Ctr [city] => Alabaster ) [1] => Array ( [name] => Andalusia Regional Hospital [city] => Andalusia ) [0] => Array ( [name] => Long Term Hospital of Anniston [city] => Anniston )) How can sort the keys of an array but there content remains the same so that they are like Code: [Select] Array ( [0] => Array ( [name] => Shelby Baptist Medical Ctr [city] => Alabaster ) [1] => Array ( [name] => Andalusia Regional Hospital [city] => Andalusia ) [2] => Array ( [name] => Long Term Hospital of Anniston [city] => Anniston )) Can someone please point me if there is any func. for this |