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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.