Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-28-2007, 04:21 PM Pushing onto array
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Hi

This could be very obvious but im struggling to find an answer.

I want to push a new element onto an existing array consisting of a key/value pair. How can i do this?

To add elements when creating an array is fine..

PHP Code:
$arr = array("key"=>"value"); 
but once the array is created i dont know how to add a new pair- i can add a single value and use the default key (i.e. [1]) but not a specific one.

Any help is appreciated.

Cheers
Stoot
stoot98 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-28-2007, 05:01 PM Re: Pushing onto array
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
PHP Code:
//equivalent to $array=array($index=>$value);
$array[$index]=$value
Or, if the index is numeric and you don't want to force it but let PHP increase it:
PHP Code:
//equivalent to $array=array($value);
$array[]=$value
Simply put, you can mix alphanumeric and/or numeric values, and if the index (whatever his type is) don't exists, the array is automatically extended. You don't have to take care of anything.
http://www.php.net/manual/en/language.types.array.php
Quote:
A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices.
Quote:
If you do not specify a key for a given value, then the maximum of the integer indices is taken, and the new key will be that maximum value + 1. If you specify a key that already has a value assigned to it, that value will be overwritten.
Yes, this is strange, when you come from C or java....
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 08-28-2007 at 05:07 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-29-2007, 04:46 AM Re: Pushing onto array
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Thanks for the reply Tripy, i had tried this and presumed it was wrong since it didnt work! However, it still doesnt work, but at least now i know it should work. The situation im using it in is a little more complicated which may be the reason, but ill try and explain below...

I have a form with repeatable section which means that fields are sent through with a suffix of '_X' where X is the number of the section. (i.e. field1_0, field2_0, field1_1, field2_1 etc) im trying to add these related fields (i.e. all the 0s, all the 1s) into an array of that name.

Below is the code im using which, in my mind, should work!

PHP Code:
$testArr = array("one_0"=>"oneVal","two_0"=>"twoVal","three_0"=>"threeVal","one_1"=>"oneVal2");
$prevNumber "";
$currentNumber "";
$repeatCount 0;

foreach( 
$testArr as $name => $val ){

                          
//get suffix number
        
$currentNumber substr($name, (strpos($name"_")+1));
                         
//if first field of that number
        
if( $currentNumber != $prevNumber ){

            
//increment repeatCount because this is new batch of fields
            
$repeatCount++;
            
$prevNumber $currentNumber;
            
            
//create new array with name = number
            
$$currentNumber = array($name => $val);
        } else {
                                      
//add field to array
            
$$currentNumber[$name] = $val;

        }
        

The $testArr is standing in for POST values while im testing.

Output should be:

an array called $0 with ("one_0"=>"oneVal","two_0"=>"twoVal","three_0"=>"t hreeVal")
and $1 with ("one_1"=>"oneVal2")

Can anyone see where this is going wrong? assuming you understand it!

Cheers
Stoot
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 08-29-2007, 06:51 AM Re: Pushing onto array
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You have 1 flaw in your logic...
Quote:
A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
So, you r $$currentNumber is invalid, as it tries to create a variable $1, $2, $3...
Try to replace them with letters, rather than numbers, or create an 2 dimension array:
PHP Code:
$ary[$currentNumber]=array($name=>$val); 


I don't exactly get what you want to achieve here, but just 1 tip about forms and arrays.
If you name your input fileds with an [] at the end, every fields that have the same name will be an array in the post values
example;
HTML Code:
<form name="frmxxx" action="" method="post">
    <input type="text" name="myInp[]" value="one"/>
    <input type="text" name="myInp[]" value="two"/>
    <input type="text" name="myInp[]" value="three"/>
    <input type="text" name="myInp[]" value="four"/>
</form>
This will result by an array named $myInp in thepost, that will hold all the values you are looking for.

And just for the beauty of the gesture (hrm...:shame: )

PHP Code:
$currentNumber substr($name, (strpos($name"_")+1)); 
This can be written with:
PHP Code:
list($junk,$currentNumber)=explode("_",$name); 
Numerous strpos() can be rather heavy on the system, and that can do a difference on a busy server, but otherwise, it's just my 2 cents.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Pushing onto array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.61643 seconds with 12 queries