PHP - How To Connect Values And Keys And Then Sort Them?
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'); Similar TutorialsSay 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!! hey 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 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. 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.. 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. 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. Hi there. I am trying to sort values in a combo box stored in an array. The array is written in t his way. Here this is in proper sorting: $WeightArray = array('' => '', '100gOrLess' => '100g or Less', '101g-250g' => '101g to 250g', '251g-500g' => '251g to 500g', '501g-1kg' => '501g to 1kg', '1kg-2kg' => '1kg to 2kg', '2kg-3kg' => '2kg to 3kg', '3kg-4kg' => '3kg to 4kg', '4kg-5kg' => '4kg to 5kg', '5kg-6kg' => '5kg to 6kg', '6kg-7kg' => '6kg to 7kg', '7kg-8kg' => '7kg to 8kg', '8kg-9kg' => '8kg to 9kg', '9kg-10kg' => '9kg to 10kg', '10kg-11kg' => '10kg to 11kg', '11kg-12kg' => '11kg to 12kg' ); Now when I call this array and put in a combo it is displayed as shown in the Screenshot. It is displaying as, 10kg, 11kg 1kg ... in the incorrect order. I tried few sort() functions but I am not able to sort the array as shown in the screenshot attached. Kindly let me know how I can sort the values in the Combo box. Thank you!! hi, also i have an array with this values: $a=array('111-aa-zz-4','222-aa-zz-6','333-aa-zz-5'); and i want to sort(); this array, but it must ignore the numbers behind -aa-zz- also 4,6,5 how should i do this? best regards, tastro 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. Hi All i am wanting a column list like this http://extensions.joomla.org/extensions basically the database is set up as (id, category, parent) I want the parent to group the category section and list like the the joomla example in three columns search various threads throughout the internet but none seem to cover this entirely can any one please help????? 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 Hello, I posted here as although it's in regards to mysql the thing i want to ask is in regards to php itself. I have two tables below as follows: Code: [Select] CREATE TABLE `users` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `status` char(10) NOT NULL, `username` varchar(15) NOT NULL, `email` varchar(50) NOT NULL, `password` char(32) NOT NULL, `reg_date` int(11) NOT NULL, `ip` varchar(39) DEFAULT NULL, PRIMARY KEY (`uid`), UNIQUE KEY `username` (`username`,`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `users_profiles` ( `uid` int(11) NOT NULL, `first_name` varchar(40) DEFAULT NULL, `last_name` varchar(50) DEFAULT NULL, `gender` char(6) DEFAULT NULL, `website` varchar(100) DEFAULT NULL, `msn` varchar(60) DEFAULT NULL, `aim` varchar(60) DEFAULT NULL, `yim` varchar(60) DEFAULT NULL, `twitter` varchar(15) DEFAULT NULL, UNIQUE KEY `uid` (`uid`), CONSTRAINT `users_profiles_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; From the above you can see the uid in the users_profiles table is a foreign key and references the uid in the users table to link both tables together. My scenario is this: A user fills out a registration form that asks for username, email and a password. After validating the form data etc it is inserted into the users table. Now at this stage they have no profile in the users_profiles table as they only just signed up. They login and xxx data is stored in a session along with there uid from users table. They now visit there profile, they fill in the profile form fields and submit the form. Now can someone tell me if what i think i should be doing is correct, if not please tell me what i should do or a better way: After validating the data that was submitted on profile form etc i could do something like a simple if else statement which will first check to see if the uid in session which is from users table matched a uid in the users_profiles table. If there's no match which in this case it won't be as they are a new user and don't have a profile record in users_profiles table then do an INSERT query into users_profiles table which would insert the submitted values from the form and the uid from session, on success that would now link the user from users table to there newly created profile in the users_profiles table. But if the uid in session matched a uid in user_profiles then obviously this means the user already has a profile ie.. submitted profile info previously so do an UPDATE query instead. Am i rite in thinking this is how i would do it? example below, note it's not valid code just to try an illustrate what i am saying; Hopefully! Code: [Select] <?php if($_SESSION['uid'] == user_profiles.uid) { /* If the uid in session which is the uid from users table * (we got it on successful login) * matches a users uid in user_profiles table then profile already exists * for user so do an UPDATE query here */ } else { /* $_SESSION['uid'] does not match a uid in users_profiles table * so instead do a INSERT QUERY here. This means no profile exists for user. * The uid from session is inserted into users_profiles table (column uid) * which links the profile to user. */ } ?> I know if i delete a user it will also delete there profile if they have one like it should do to obviously not leave redundant data in the database and to ensure data intergity (sorry i think that's what it's called) Thanks for any help. PHPFAN How can you print an individual key in the $_POST array? I've tried... Code: [Select] echo $_POST[0];But with no luck. There is data in the array, not all keys have values. i am trying to convert all keys to lowercase and i was wondering if it is really possible to do using array_map to convert all keys to lowercase or using array_reduce.
what i am trying to do is to gather details from keys and display it on the page as links that redirect to the details page, i managed to convert it into lowercase, however it did not gather the records mainly due to keys being capitalised and me trying to gather those records using lowercase.
to put things into perspective here is the code: <?php $cakesmade = array(); $cakesmade = array( "kate" => array( "cake" => array( "cakeingredients" => "egg, flour"), "Lovely Chocolate Cake" => array( "cakeingredients" => "chocolate, eggs, flour"), "amazing cake" => array( "cakeingredients" => "lemons, flour") ), ); ?>//links <?php foreach($cakesmade as $id => $donecake) foreach($donecake as $bakedcake => $description) { echo "<a href='/user/$id/baked/$bakedcake'>$bakedcake</a>" } ?> 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? 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 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 For example: Code: [Select] DELETE FROM user_question_answer WHERE user_id = 1 AND question_id = 7 AND `answer_id` = 3 This sql query has no error, but reports deleting 0 rows. user_id and question_id fields are both foreign keys in this table. Could that be why it's not working, yet not reporting an error? Thank you for your time. |