Skip to main content

How can I add a variable to an array?



How can I add a variable to an array? Let say I have variable named $new_values :







$new_values=",543,432,888"







And now I would like to add $new_values to function. I tried in that way:







phpfunction1(array(114,763 .$new_values. ), $test);







but I got an error Parse error: syntax error, unexpected T_VARIABLE, expecting ')'





How my code should look if I would like to have array(114,763,543,432,888) ?


Comments

  1. $new_values=",543,432,888";


    should be converted to an array:

    $new_values= explode(',', "543,432,888");


    and merged to existing values with:

    array_merge(array(114,763), $new_values);


    Whole code should looks like:

    $new_values = explode(',', "543,432,888");
    $values = array(114,763);
    $values = array_merge($values, $new_values);
    phpfunction1($values, $test);


    If you pass to explode a string that is starting with , you will get first empty element, so avoid it.

    ReplyDelete
  2. if you have an array already i.e.

    $values = array(543,432,888);


    You can add to them by : $values[]=114; $values[]=763;

    Apologies if I missed the point there...

    ReplyDelete
  3. In your example, $new_values is a string, but, since it's comma delimited, you can create an array directly from it. Use $new_array = explode(',', $new_values); to create an array from the string.

    ReplyDelete
  4. You need to convert the string into an array using the explode function and then use the array_merge function to merge the two arrays into one:

    $new_values=",543,432,888";

    $currentArray=array(114,763);

    $newArray=array_merge($currentArray,explode(',',$new_values));

    functionX($newArray...)


    But watch out for the empty array element because of the first comma.
    For that use "trim($new_values, ',')" - see answer from rajesh.

    ReplyDelete
  5. you can do like this.

    $old_values = array(122,555);
    $new_values=",543,432,888";
    $values = explode(',', trim($new_values, ','));
    $result = array_merge($old_values, $values);
    print_r($result);

    ReplyDelete
  6. try array merge

    looks like this

    phpfunction1(array_merge(array(114,763) ,$new_values), $test);

    and yes your first array is not an array
    change it to this

    $new_values=Array(543,432,888);

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...