Max And Array Keys
I am trying to find the key value of the largest number in a array. so basically if my array is
$thing = array (3, 4, 5,10, 1) I would like to return 3, I figure I need to use Max and Key values, can someone please point me in the right direction?
View Complete Forum Thread with Replies
Sponsored Links:
Related Messages:
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 !
View Related
Using Max() On Array Keys
i am using an array to store contact data within a session. it looks like this: $_SESSION['contact'][$key][<content>] $key counts the set of records, so i have $_SESSION['contact'][Ɔ'] $_SESSION['contact'][Ƈ'] $_SESSION['contact'][ƈ'] and so forth. [<content>] stands for the fields containing the actual set of data, which are stored within the [$key]-arrays. at one point i need to find out what the highest value of $key is. running max($_SESSION['contact']) simply leaves me with "Array()" as the result. what i am doing wrong, and how do i correct it?
View Replies !
View Related
Array With Fes Different Keys
I have an array with fes different keys: $job_name = array (༽/07/2005' => 'name1', ༾/07/2005' => 'name2', ༽/07/2005' => 'name3'); Now I want to print only the ༽/07/2005' job names. Do I need to create a loop or there is another way?
View Replies !
View Related
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 !
View Related
Print Out Array Keys Only
Hi, there are lots of examples that show you how to print array values given a key. Do you know how to print keys for a given array. example $myArray = array('a'=>1, 'b'=>2, 'c'=>3, 'd'=>4); How do you print 'a', 'b', 'c','d' only without using any loops?
View Replies !
View Related
Printing Out Array Keys
i'm trying to find a simple way of printing out the keys in an array generated by mysql_fetch_array which returns a single row. i.e. while ($row=mysql_fetch_array($result) ) { print out the $row keys and values } is this possible?
View Replies !
View Related
Reverse Array Keys
See the following example: [4] => 'value1' [0] => 'value2' [1] => 'value3' [3] => 'value4' [2] => 'value5' should become [0] => 'value1' [4] => 'value2' [3] => 'value3' [1] => 'value4' [2] => 'value5' As you can see, the keys have been reversed such that the lowest key value is now the highest.
View Replies !
View Related
How To Get Two Keys From Array Loop?
I have an array that stores ids, each of which is unique. Is there a way to run a for loop and get two keys at a time instead of only one? Example $id = array(1, 2, 3, 4, 5, 6); for loop.... echo key 1 echo key 2 next loop echo key 3 echo key 4 and so on.... Each key must not appear twice so once a key is displayed, it will be removed from any further loops. I need it to display two keys at a time because I will be pairing them up for the next part of the script.
View Replies !
View Related
Recreate The Numeric Array Keys 0
Is there a php function that will recreate the numeric array keys 0 to what ever is in the array or should I just render a new temp array with a while or foreach loop? I'm trying to do some array manipulation and not having the array keys in sequence does not work. Partly I'm trying to create an SQL query with ID='value' OR with a loop but cannot have an OR after the last entry, so I'm ctrying to count() the array and use the condition of 'count() - $key > 1' to insert the OR so that when the last on is reach it does not because it is = to 1. When the array not in sequence the that last key and count value will not work together properly.
View Replies !
View Related
Foreach Loop With Array Keys
I am unable to get the following piece of code to work as desired. The fields valuable seems to contain all values however the vaules variable only contains the first assignied with $values = ''' . $arry[$keys[0]] . ''' $keys = array_keys($arry); $fields = '`' . $keys[0] . '`' $values = ''' . $arry[$keys[0]] . ''' foreach($keys as $key) { if($key != $keys[0]) { $fields .= ', `' . $key . '`' $vaules .= ', '' . $arry[$key] . ''' } }
View Replies !
View Related
Prepend Array Keys With A String
I have looked on uk.php.net for the function I need but can't find it. Does anyone know what the function is called who's purpose is: to recurse through an array and assign values to keys whilst prepending the key name with a string specified by one of the function parameters?
View Replies !
View Related
Getting Deep Array Value Without Knowing Keys?
I've racked my brain for the last few hours trying to figure this one out. I have an array of language strings such as : $lang = array(); $lang['en']['prompt']['hello'] = 'hello' $lang['en']['prompt']['bye'] = 'goodbye' $lang['en']['number']['first'] = Ƈst' This is being used by an ajax routine, so I don't know which string will be requested at runtime. So, I've got $lang and a request ('en_prompt_hello'), and tried a few things such as: $request = explode ($request); $found_string = array_intersect_assoc ($lang, $request); to no avail. Short of having to do a complicated recursion searching at every available depth, is there an easy way to do this?
View Replies !
View Related
Sort An Array Using 2nd Dimension Keys
I need to sort an array (configured as shown) based on one of the second dimension keys. [1] ['File_Name'] => test.doc; [1] ['File_Type'] => MS Word; [1]['Size'] => 50; [2]['File_Name'] => test.pdf; [2]['File_Type'] => Adobe PDF [2]['Size'] => 150 [3]['File_Name'] => test.xls; [4]['File_Type'] => MS Excel; [5]['Size'] => 60; For example, I'd like the order of the 1st dimension keys (integer indices) to reflect the order if the array is sorted by the "File_Type" key.
View Replies !
View Related
Resetting The Keys Of An Array After Unset ?
I'm trying to delete an element of an array and then reset the keys. $colors = array('red','blue','green','yellow','purple','gold','silver'); $colors_key = 0; //It won't always be 0, it could be 3 or 4, etc. foreach($colors as $key => $value) { unset($colors [$colors_key]); } $new_array = array_values($colors); And then print out the array, I'll get this: Array ([1] => blue [2] => green [3] =>yellow [4] =>purple [5] => gold [6]=>silver); What I would like to get is this: Array ([0] => blue [1] => green [2] =>yellow [3] =>purple [4] => gold [5]=>silver); Anyone know how I can do this?
View Replies !
View Related
Array Keys - Check Boxes
I have a bunch of checkboxes and then on the next pg i check which boxes were ticked. Here is my code $i = 0; $array = ""; foreach(array_keys($_POST) as $myKey) { echo "ee"; if (ereg('value', $myKey)) { echo "hi"; $id = ereg_replace("value='checkbox'", "", $myKey); $array[$i] = $id; $i = $i+1; } } If none of the boxes are ticked i get errors. I want to check if boxes were not ticked and if none was ticked then send a user to another page But ive tried every way i can think of and cant get it to work.
View Replies !
View Related
Building An Array Using Stored Keys
If I have a list of keys and need to create an array then add a value to the final element, how would I do that? For instance, if I have the string "red" and if I have the stored keys: array(1, 'element', 0, 'property') And I want to build the array: $an_array[1]['element'][0]['property'] = "red"; How could I go about doing that?
View Replies !
View Related
Wild Cards For Array Keys?
I have a pretty complex array that stores information for fields. The following array element is a boolean: $_POST['fields'][$i]['listing'] Where $i starts a 1 and can go on for however long the user inputs into the form. So lets say the users input 20 fields. How exactly would I count how many of those array elements are equal to 1, without doing a loop? Is it possible to insert a wild card in place of $i? I already have a lot of nesetd loops and the last thing I want to do is add another one.
View Replies !
View Related
Array Keys - Avoid Using Temp
I was wondering if there is a more gracious way to write the below code: //$input is an array .I just need its 0th and 2nd key-not the 1st.I want to avoid using temp $temp=array_keys($input); print $temp[0];print $temp[2];
View Replies !
View Related
Argument To Keys() Should Be An Array In Main.inc On Line 152
I am getting the following errors: Warning: Argument to keys() should be an array in main.inc on line 152 Warning: Non array argument supplied for foreach() in main.inc on line 152 I've developed the site on my linux box at home using PHP 4.0.3pl1. It works great, but when I upload it to my hosting service I get the above messages. I've tried all the different ways to go through an associative array. I've tried doing an implode(array_keys($arr), ',') then split on ','. It keeps coming back to the fact that it doesn't recognize the array I declare right before it is an array. The hosting service runs a BSD Bosx and uses PHP 4.0b2. I'm wondering if this was a problem with the second beta release or if it is something screwy between platforms. Why does it work great on my box but when I upload it it craps out?
View Replies !
View Related
Calling Array Keys With Embedded Variables
I'm in the middle of modifying a script and I've come across a problem. I have a form that gets repeated x number of times and each field in the form gets the name $item_number_ . $business_item_index ._(the specific field name). These varaibles get reposted to the same page as $_POST variables, so that the forms save the users settings. What I'm trying to do is parse through those fields that the user did not set, and if they are null, to unset them and decrement the counter($business_item_index) so that one less form gets written to the screen the next time the user submits. Code:
View Replies !
View Related
Assign Column Names As Array Keys
Want to create an array that has db column names for the key names and field values as the array values. Is there an easy way to do this? If not possible directly, is there a way in php/mysql to get a list of column names in a table such that I could use those values as the keys?
View Replies !
View Related
Getting Keys/values From POST Array In An Orderly Fashion..
I've got this javascript that results in the following being posted; weight_1 = 30 cost_1 = 20 amount_1 = 32 weight_3 = 65 cost_3 = 23 amount_3 = 12 weight_7 = 43 cost_7 = 9 amount_7 = 12 etc.., with the numbers appended to the keys (here 1, 3, and 7) ascending but not sequential. I need my PHP recieving these POST keys/values to present them like this: #1 Weight: 30 Cost: 20 Amount: 30 #2 Weight: 65 Cost: 23 Amount: 12 #1 Weight: 43 Cost: 9 Amount: 12 Whats' the right way to do this? I've been messing around with array functions but haven't quite figured out which to use..
View Replies !
View Related
UNSET'ing Multidim Array Elements And Reseting Keys
I have a script that calls code plugins that return associative arrays: $aData[0]... $aData[1]... $aData[2]... $aData[3]... $aData[4]... $aData[5]... $aData[6]... $aData[7]... I would like to run a search on the array data and UNSET the array rows that do not meet my criteria (this part I know how to do). This means I end up with: $aData[0]... $aData[1]... $aData[3]... $aData[5]... Once this is done, I'd like to reset the first level key names to be consecutive like this (this is where I need help; I'm hoping for a existing builtin function but so far have not been able to find one): $aData[0]... $aData[1]... $aData[2]... $aData[3]... I'll find out soon enough once this is working but does anyone know is UNSET eats up alot of time? on large arrays?
View Replies !
View Related
Primary Keys Vs Index Keys
I have a categories table of which is structured like so: category_id INT [primary key] access_url VARCHAR [index key] When sorting the records via my application (of which will be used in a high traffic environment) does it make any difference whether a record is selected via it's index key vs the primary key? Is there any significant speed/memory differences from accessing records via a VARCHAR index key as opposed to an INT primary key?
View Replies !
View Related
Keys
I've been coding with PHP for quite some time now, but still I have never had to use keys in my databases, so I've never learned how too.. Now I realize that it is more efficient to call users in a db with a 'uid' field instead of their user name. -How do I go about setting this up? -How do I enter a new row into my db - do I need to set a value for the 'uid' field or will it increment itself? (that seems to be the point anyways)? -Any helpful articles / posts you can point me to regarding this?
View Replies !
View Related
Primary Keys
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using an existing username. I have a database full of the same usernames!
View Replies !
View Related
Shortcut Keys
I would be liek to be able to set up a webpage with shortcut keys so that when certain buttons are presses, a pop up window will open with a designated further page displayed. Specifically I would like the shortcut keys to be a couple of the function keys, ie f10, f11 and f12.
View Replies !
View Related
Foreign Keys
Normally, my tables have a primary key, and usually it's an auto-incrementing integer - unless, occasionally, it's an alpha string like a UserName or something. then, I may have a "dependent" table with a foreign key field which references the primary key of the "parent" table. Now, I know it's possible to have a primary key made up of multiple columns - I call that a compound primary key myself. I do that occasionally. However, is it possible to have a "compound foreign key" ? I.e. a foreign key of two columns, which refers to the two columns for the primary key? I realize you can do that by just creating the tables that way, of course, but can you set referential integrity on that? And how would you do that?
View Replies !
View Related
Numeric Keys
<?php // What could possibly go wrong? $test_array = array( "3" => "three", "3.5" => "three and a half", "4" => "four"); $numeric_key = 3.5; echo($test_array[$numeric_key]); // wrong echo '<br>' $string_key = "3.5"; echo($test_array[$string_key]); //right echo '<br>' $numeric_key = 3.5; echo($test_array[(string) $numeric_key]); //right :) ?> Well, at least I had the opportunity to learn that typecasting can be done right inside of the array key brackets.
View Replies !
View Related
$_GET[] Keys
Say if $_GET[1] = "on", how do i find out what the key is. In this cae i know what the key is but i need php to dynamically know what the key is. I've looked at Function key but i couldn't make sense of it.
View Replies !
View Related
Return Keys
I have an associative array but i wnat to do a reverse lookup. So if I have a value that is within one of the elements of my array and i wish to return the key that of that element. How can i do this. // $value is the variable to lookup foreach ($Arr as $element) { if ($element==value) return $key }
View Replies !
View Related
PHP & MySQ + Unique Keys
I'm trying to write a system thats used for about 50 clients that uses html forms and php to log details. The problem is that when a client loads the form page it's given a value which is the last record in a table +1 (i.e. so its the next record). The problem with that is that a client could sit on that page for 20 mins (or whatever length of time) and a different client could use that record number and there what be an error "duplicate key....". I could use an MySQL UPDATE to lock the record when its issued to each client and maybe use a timestamp and check if the timestamp is over 10 mins and delete any records that are over 10mins but there is the possibility of redundant data (or really useless data, a unique record and a timestamp) in the table sitting there for 10mins at a time. Is there an easy way of 'gauranteeing' a client a unique key? So you don't have to lock the record when the page loads (this also means that when you refresh the page that the unique key is incremented as you locked the last record and didn't put anything into the table, so the refreshed page is issued the next key +1 again).
View Replies !
View Related
Associative Arrays Keys
I have a some text that i have to sort according to their year. The text is in this format.. A Short Story (2000) A long time ago (1999) A list (2004) Before Time (1999) Car parts (2004) so basically i want the output to be.. A long time ago (1999) Before Time (1999) A Short Story (2000) A list (2004) Car parts (2004) I thought the way to go about this would be to extract the years and put the whole thing in an associative array with the years as keys, but of course the keys have to be unique so in this case values override each other. PHP Code:
View Replies !
View Related
Getting MySQL Generated Keys
I am a Java/JSP developer by trade but am currently developing a MySQL/PHP solution. In JSP when I run an INSERT statement on the db I am able to automatically get back a list of any generated keys (without having to do some sort of subsequent query to get the information). Is there a PHP equivilent of this?
View Replies !
View Related
ActiveRecord And Foreign Keys
I am having trouble wrapping my mind around the ActiveRecord pattern and foreign keys. I understand that for each table, there should be a class, and each row is is effectively an instance. But, my problem arises when I look at foreign keys and advanced relationships. For basic foreign keys, I can just automatically reference to the object. Example: Table: invoice id, notes Table: items id, cost, invoice_id When working with an instance with items, I can easily load a new object called "invoice." But, what if I am already working with invoice, and I want to load items? I can't think of any way to abstractly relate the two. Also, what if I want to two things that are both a "has many" relationship? For example, if I take users that can have multiple qualities, I would need three tables: one for users, one for the qualities, and one to link. Table: users id, username Table: link id, user_id, quality_id table: qualities id, quality_name
View Replies !
View Related
PHP MYSQL And Primary Keys
Say I have an ID field in my database that is a primary key and an UNSIGNED AUTO INCREMENTING TINYINT. It stores range of 0-255. Ok I add a few items to the database, say 240. At a later date I delete a few records, say rows 33, 44, 55, 66, 77, and 240. So the last record in the database now has an ID of 239, I add another record and it gets assigned an ID of 241?. I then add a few more and it finally reaches ID 255. Now all the ones I deleted previously do not exist. So I try to add another record, will it just get added and assigned the ID of a record that doesent exist such as 33, 44, 55, 66, 77 or 240? If not is there a way around this? I am pretty new to MySQL and I dont know the answer to this but I know that it could prove to be pretty inefficient if an ID field can hold 255 records but only holds 240. Do you understand what I mean and can anyone answer my question?
View Replies !
View Related
Foreach Is Returning Value Of First Key For All Keys
I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first key for all the keys. My code is shown below and the problem areas are marked with comments. In case you're wondering, this script is for generating sticky checkboxes that include an event handler. <form> <?php if (! is_array($like)) { $like = array(); } function make_checkbox_click ($name, $query, $options, $onClick) { foreach ($options as $value => $label) { printf('<label><input type="checkbox" name="%s[]" value="%s" ', $name, $value); # This is the instance of foreach that's not working correctly. # It's returning the value of the first key for all the keys. foreach ($onClick as $key => $event) { printf ('onClick="%s" ', $event); } if (in_array($value, $query)) { echo "checked "; } echo "/> $label</label><br /> "; } } # End of user defined function. $characteristics = array( 'personality' => ' I love their personalities.', 'minds' => ' I admire their minds.', ); # This is the array that foreach isn't working correctly with. $charClick = array ( 'personality' => "alert('Aren't they the greatest?');", 'minds' => "alert('They're smarter than most people their age.');", ); make_checkbox_click (posPoints, $posPoints, $characteristics, $charClick); ?> </form>
View Replies !
View Related
Mysql: Get Foreign Keys
there is a function to get the foreign keys of a table? i have this function to get all table infos: $fields = mysql_list_fields($V['db']['name'],$table) or die (mysql_error()); $columnsCount= mysql_num_fields($fields) or die (mysql_error()); for ($i = 0; $i < $columnsCount; $i++) { $columns[$i]['name']=mysql_field_name($fields, $i) ; $columns[$i]['length']=mysql_field_len($fields, $i) ; $columns[$i]['type']=mysql_field_type($fields, $i) ; $columns[$i]['flags']=mysql_field_flags($fields, $i); } but i need alo to know info about foreign keys.
View Replies !
View Related
|