moldboy 0 Report post Posted September 9, 2005 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: $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? Share this post Link to post Share on other sites
truefusion 3 Report post Posted September 9, 2005 Try this: <?$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. Share this post Link to post Share on other sites
Spectre 0 Report post Posted September 9, 2005 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: $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. Share this post Link to post Share on other sites
moldboy 0 Report post Posted September 12, 2005 I'd love to say that worked but it appears to still think the variable isn't an array. Share this post Link to post Share on other sites
Spectre 0 Report post Posted September 12, 2005 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. Share this post Link to post Share on other sites