moldboy
Sep 9 2005, 12:36 AM
| | When you have a normal variable say $string and you want to add more to it you would use the .= construct, but how does one go about doing that with an array, I have an IF structure that determins what options the user has selected, so I have something like: CODE $array = NULL; IF (isset($_POST['op1])){ $array .= array(1, 2, 3, 4); } IF (isset($_POST['op2'])){ $array .= array(5, 6, 7, 8); }
So if there is a value for op1 then the array contains 1,2,3,4 is there is a value for op2 then the array contains 5,6,7,8 and if both are set the array contains 1,2,3,4,5,6,7,8, or atleaset that's the plan, you see if I try to preform any array specific functions like random select, it tells me that the first entry is required to be an array, or something like that, so how can I create an array and then add more to it? |
Reply
truefusion
Sep 9 2005, 02:49 AM
Try this: CODE <?
$array = array(1, 2, 3, 4, 5, 6, 7, 8);
IF (isset($_POST['op1])){ $array = array_slice($array, 0, 4); } IF (isset($_POST['op2'])){ $array = array_slice($array, 4, 4); } IF (isset($_POST['op3'])){ $array }
?>
I haven't tested it, but i'm sure it'll work to your purpose. Modify where needed.
Reply
Spectre
Sep 9 2005, 03:22 AM
PHP has very easy to use array handling - much easier than most other programming languages I know of (take C, for instance). An array is created with the array() function, as you are obviously already aware. The array_merge() function allows you to merge one array with another. You could probably use this to get around your problem: CODE $array = array(); if( isset($_POST['op1']) ) { $array = array(1,2,3,4); } if( isset($_POST['op2']) ) { $array = array_merge($array,array(5,6,7,8)); } This would mean that if the POST variable 'op1' is set, then the array would be assigned the value of { 1,2,3,4 } - and if 'op2' is set, then the current array will be merged with an array consisting of { 5,6,7,8 } (and if only 'op2' is set, the array will only equal { 5,6,7,8 }). There are a lot of different ways to fiddle with arrays in PHP. This is just one example. Hope it helps.
Reply
moldboy
Sep 12 2005, 12:33 AM
I'd love to say that worked but it appears to still think the variable isn't an array.
Reply
Spectre
Sep 12 2005, 02:51 AM
Did you try the method I mentioned? You can't use '.' (a full stop) to append to an array, only to a string. In the code I posted, $array is being initialized as an array - and then assigned the values of an array later. Meaning that it should be considered an array. Try using is_array($array); to determine if it is an array and use print_r($array); to recursively print the values in the array.
Reply
Similar Topics
Keywords : appending arrays- Arrays Outside A Function
- Need to have arrays available to all functions. (3)
- Problem With Global Arrays
- (2)
I've got a problem with global variables. /sad.gif" style="vertical-align:middle" emoid=":("
border="0" alt="sad.gif" /> I've read and re-read the stuff about that on php.net and did that
a few times more, and I'm still not able to make it work. Could somebody please fix my code?
This is the primary page: include "getLvlFromHigh.func"; $user = $_GET ; $skill =
$_GET ; $goal = $_GET ; $userXp = getLvlFromHigh($user); $xpTable =
array( 31 => 14833, 32 => 16456, 33 => 18247, 34 => 20224, 35 => 22406, 36 =>...
Basic Tutorial On Arrays
- (3)
In php u can declare arrays by two methods 1. Using the Array function 2. Using the Array
Identifier Array Function To define an array consisting of three elements using the array
function we write the following code CODE $arrayname=
array("basic","array","tutorial"); This declares an array with
the name $arrayname containing 3 elements. To access these elements u have to use the array
name along with the index in square brackets. ** Note: php arrays start from 0 index To print
the elements of the above array...
Converting Characters In A Variable To Individual Values In An Array
- turning variables into arrays (2)
Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
$nav_item = 'Home'; How would I make so that $nav_item was an array and
had the following contents? CODE $nav_item = array ('h', 'o',
'm', 'e'); With the case changing (ie H would become h and U
would become u ) EDIT: Okay found out that I could change the case with
array_change_key_case ($nav_item, CASE_LOWER); ...
Alternative To File()?
- file stored in arrays (4)
Hey guys. I was wondering if there is an alternative to using file(). I'm trying to read a file
and store it into an array without using the file function. Before, I was doing something like this:
CODE <?php $file = file("http://yahoo.com"); ?> Simple.
It gets the contents of index.html at yahoo.com and store each line in an array. Well, I was
wondering if there is another code that could do that without using the file function. Thanks for
helping!...
Looking for appending, arrays
|
|
Searching Video's for appending, arrays
|
advertisement
|
|