Jul 25, 2008

Appending Arrays

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Appending Arrays

moldboy
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
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
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
I'd love to say that worked but it appears to still think the variable isn't an array.

Reply

Spectre
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



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : appending arrays

  1. Arrays Outside A Function - Need to have arrays available to all functions. (3)
  2. 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 =>...
  3. 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...
  4. 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); ...
  5. 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



Appending Arrays



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE