PHP - Fill Aray With Keys And Values
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. 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 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'); 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.. 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 This is something used in CPUs Logical shift right I have a string $bin = "10011101"; // result should be "01001110" the function is function LogicalShiftRight($bin) { for($x = 7; $x == 0; $x--) { echo $x; $bin[$x+1] = $bin[$x]; } $bin[0] = 0; return $bin; } cant get the loop to work backwords. This is an 8 bit shift right putting a zero at the highest bit. 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. 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 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 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>" } ?> 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 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. 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. Code: [Select] array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 1 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 2 => array ( 'id' => '2', 't' => 'Automotive', ), ) how to sum it, to get: Code: [Select] array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', 'cnt' => '2' ), 1 => array ( 'id' => '2', 't' => 'Automotive', 'cnt' => '1' ), ) Hey guys so I've been trying to figure something out for a day now but I really don't know where to start, basically I'm trying to create some kind of function that goes through my array and checks if the keys value is less than 5, if it is then I want to unset the key removing it from the array,
I programmed this underneath as this is basically what I want to achieve however there's a syntax issue as the array_key_excists function doesn't seem to allow the if statement, if anyone knows anything about it it would be very helpful!
if (array_key_exists(($key > 5),$data)) { unset($data[$key]); }any help is very much appreciated, Thanks very much in advance! |