Concatenate variable with array in PHP

Asked

Viewed 294 times

-1

I have a variable nome, and a array.

How to put this variable in array? Example:

['dia'].$nome;

When I do this, returns error.

The array dia contains a string, as an example: "Domingo", and nome the value 1.

Code: $name = '-bla'; $test. $_FILES['file'].= $name;

the array returns : Array ( [name] => A91126.pdf [type] => application/pdf [tmp_name] => C: xampp tmp php3BA0.tmp [error] => 0 [size] => 21710 )

  • Put more details in your question, especially why you want to do this.

  • 1

    First we need to understand what you want, the question is very vague. First of all you need to know clearly what you want to do and communicate this so that we can see how to do it in code. In fact this code makes no sense and we can’t even guess the intention for it.

  • Simple I want to add a value of a variable in the array. Prem concatenating the array plus the error variable

  • 1

    It was understood that you cannot assign a value to an array, but you have not clearly explained the purpose of this code, or you just want to know how to assign the value of any variable to an index of an array?

  • That’s right I’d like to print what’s in the array along with the variable

  • Dear colleague, @Beginnerteee. Make a brief community tour to understand some features. Notice that I made an edition in your question formatting the code and etc, soon you made an edition that, besides adding content - which is super welcome -, broke part of the formatting I did. Just take a look in those reviews. Hugging! ;)

Show 1 more comment

2 answers

4

To add to the array you enter the key and simply assign its variable:

$dias['dia'] = $nome;

But if you already have a value inside and just want to concatenate with the existing content you use:

$dias['dia'] .= $nome;

Example: We have the following array

$dias = array(
    'dia' => 'Segunda'
);

When he shows off he’ll be like this:

    Array
(
    [dia] => Segunda
)

We want to add the 'fair' at the end of the day, soon:

   $nome = '-feira';    

   $dias['dia'] .= $nome;

Will display like this:

Array
(
    [dia] => Segunda-feira
)

If the example became too easy or did not answer your question just comment that I leave another code.

  • It gives the following error : Notice: Array to string Conversion. It would not have to convert the variable into array?

  • Is there a way to put the code you are running? Write the array that is easier to understand.

  • Yes, right now

-3

Original array $_FILES['file']['name'];

$_FILES['file']['name'] = "test";

The exit is the name test as I wanted, much simpler and answered

Browser other questions tagged

You are not signed in. Login or sign up in order to post.