Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    PHP


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





Changing Array Keys


I've looked and looked and apparently I don't know what terms I'm supposed to be looking for. Code:




View Complete Forum Thread with Replies

Related Forum Messages:
Changing Array Keys After Using Explode
I have used explode to split a string into parts but the the resulting array keys are ints...is there a simple way of changing the keys from numbers to informative values?

View Replies !
Using Array_intersect() Without Keeyping Keys From The Array1, I.e. Re-number Intersect Array Keys.
I only use indexed arrays.

When I use array_intersect function, of $a3 = array_intersect( $a1,
$a2 ), I just need the vales of intersect array and let keys to be
re-numberred from 0.;

I found the intersect array $a3 have the correct values but keep the
keys of $a1. When I use
for loop from $index=0 to sizeof ($a3)-1, by $a3[$index], php reports
"undefined offset " problem.

How can I re-number the keys of $a3 from 0 to sizeof($a3)-1;



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 !
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'][&#390;']
$_SESSION['contact'][&#391;']
$_SESSION['contact'][&#392;']

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 !
Array With Fes Different Keys
I have an array with fes different keys:

$job_name = array (&#3901;/07/2005' => 'name1', &#3902;/07/2005' => 'name2', &#3901;/07/2005' => 'name3');

Now I want to print only the &#3901;/07/2005' job names. Do I need to create a loop or there is another way?

View Replies !
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 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 !
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 !
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 !
Write Array Keys To SQL
I have an array of data. I want to write each key in the array to its own row in SQL. what would be the best way to accomplish this?

View Replies !
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 !
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 !
Trouble With Moving Keys In An Array
OK i have the following array......

View Replies !
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 !
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 !
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 !
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'] = &#391;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 !
Multibyte Strings As Array Keys
Does this work? Something is breaking when I try to do this, and all I
can think of is that PHP does not allow multibyte array keys.

View Replies !
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 !
CLI- Reorder Array Keys After Using 'unset'
I'm having a problem finding a sort function that will reorder the keys in an array after I delete one of the array key/value pair. So if I have array: Code:

View Replies !
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 !
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 !
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 !
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 !
Extract The Keys Of An Asscociative Array
I have an array which get generated as follows:

while($row = mysql_fetch_assoc($res)){
    $dataArr[] = $row;
}

How do i extract the keys only from dataArr?

View Replies !
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 !
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 !
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 !
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 !
Init Missing Array Keys? (performance)
in my template engine i init every var being used with NULL to avoid those E_NOTICE messages if they were not provided by the script Code:

View Replies !
Sort Array Keys Based On Their Value Descending.
I sort array keys based on their value Descending.

Array ( [john] => .55 [doug] => .44 [fred] => 1.55 )

View Replies !
Manipulate Array Keys And Values For Query String
i'm creating a function where I can update my records ... but it doesnt work...I echoed my SQL query and it turned fine..but it didnt update my record! PHP Code:

View Replies !
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 !
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 !
Question About The Behavior Of String Associative Array Keys
I am trying to use an associative array to pass names and scores to a JPgraph but I believe I may misunderstand something about associative arrays. Say I have an array called $kidscores where:

$kidscores=(Johnny_Jones => 50, Sally_Smith => 100, Tommy_TheCat =>75)

When I pass the $kidscores variable to JPgraph, and try to....

PHP Code:

View Replies !
Whats The Most Efficient Way To Split An Associative Array At Defined Keys?
whats the most efficient way to split an associative array at defined keys? the main purpose of this is to delimit a $_POST request with key/value pairs that identify what form they belong to. Like: Code:

View Replies !
Using Both Numeric Keys And Associative Keys In Mysql_fetch_array
here is a query:
$query = mysql_query("SELECT * FROM table WHERE field='$this'");

now I make an array from the results:
$row = mysql_fetch_array($query);

Yes I realize you can use a while statement to use associate keys to call on each row of the result, and then do something, but I specifically DO NOT WANT TO DO THIS

Here I sum up the results: Code:

View Replies !
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 !
Changing An Array Name.
If I have an array called my_array1, how do I call it within a for cycle? Something like this:

for($a=1;$a<=5;++$a){
$my_array = 'my_array'.$a;
          //prints $my_array
}

I know that doesnt work, so, I tried something like the following:

for($a=1;$a<=5;++$a){
$my_array = 'my_array'.$a;
          //prints $$my_array
}

That does work with variables, but doesnt work with arrays. I know that in Java and ActionScript what I'm trying to acomplish is pretty simple, but I cant seem to find the correct syntax for php.

View Replies !
Changing A Value Of An Element In A Two Dimensional Array
I am having a problem with array manipulation.

in my php script i am reading from a csv file.
the content of file is like this:

View Replies !
Changing Object Data In An Array Of Objects
In this sample script I create an array of objects. Print out their data
with print_r().Update their data with a sub called v_set(). Print out data
showing the chnages using print_r(). I push the altered objects into a new
array and print them out again, still see the updated data. Then I iterate
over the original array again with print_r and the objects lose the updates
and have their original data.

<?
for ($i = 0; $i < 1; $i++) {
$listingObj = new Listing;
$listings[] = $listingObj;
}

foreach ($listings as $aListing) {
echo "<h3>THIS IS IN SCRIPT IN ORIG ARRAY BEFORE VARS SET</h3>";
f_array($aListing);
}

foreach ($listings as $aListing) {
$aListing->v_set(
array(
'name' => 'Jane',
'address' => Ïã Ohce Ln.'
)
);
// HERE IS BUG: WHY DO I NEED TO PUT IN NEW ARRAY?
echo "<h3>THIS IS IN SCRIPT IN ORIG ARRAY AFTER VARS SET</h3>";
f_array($aListing);
$newArray[] = $aListing;
}

foreach ($newArray as $aListing) {
echo "<h3>THIS IS IN NEW ARRAY</h3>";
f_array($aListing);
}

foreach ($listings as $aListing) {
echo "<h3>THIS IS IN ORIG ARRAY, APPARENTLY NOT
ALTERED/UPDATED?</h3>";
f_array($aListing);
}

class Listing {
var $changedVals = array();
var $name = 'Dick'
var $address = ì« Echo Ln.'

function v_set($data){
foreach ($data as $key => $value) {
$this->$key = $value;
$this->changedVals[] = $key;
}
}
}

function f_array($array) {
echo '<pre>'
print_r($array);
echo '</pre>'
}

?>

View Replies !
Changing Array Format From Result Of MySQL
here is what MySQL returns in a visual way..

game_id | team_id | stat_id
1 | 1 | 1
1 | 2 | 3
1 | 3 | 7
1 | 3 | 6
2 | 1 | 1
2 | 4 | 3

in records with the same game_id, there will be no repeating team_id
in records with the same team_id, there will be no repeating stat_id

so, after I got the query, I made them into this format

PHP Code:

View Replies !
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 !
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 !
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 !
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 !
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 !
$_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 !
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 !
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 !
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 !
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 !

Copyright © 2005-08 www.BigResource.com, All rights reserved