-1

I have an array of values that I am trying to output to a string using the following code:

$arrayINS = explode(", ", $arraystring);
foreach ($arrayINS as &$array1INS) {
    $array1INS = "(" . $arrayINS . ", 'Some Text Here')";
}
$arrayvaluesINS = implode(', ', $arrayINS);

Now, let's say that the $arraystring = 25145, 25064, 24812. I would expect echo $arrayvaluesINS to be

(25145, 'Some text here'), (25064, 'Some text here'), (24812, 'Some text here')

But instead what I get is:

(Array, 'Some text here'), (Array, 'Some text here'), (Array, 'Some text here')

What am I doing wrong?

2 Answers 2

5

$arrayINS is the array.

$array1INS = "(" . $arrayINS . ", 'Some Text Here')";

should be

$array1INS = "(" . $array1INS . ", 'Some Text Here')";

Next time use meaningful variable name.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, it should be: $array1INS[] = "(" . $array1INS . ", 'Some Text Here')";. ;)
Right, I have not noticed &. Sorry for noise.
Stoopid error! How did I not see that?! Been staring at this for 2 hours trying to solve it! Thank you, @xdazz.
-1

You're using $array1INS as the iteration variable in the for, but then in the next line, you use $arrayINS in the assignment (which is an Array) and overwrite what you had put in $array1INS. Try this:

foreach ($arrayINS as &$item) {
   $array1INS = "(" . $item . ", 'Some Text Here')";
}

4 Comments

Undefined variable $array1INS -1. Next time make sure you post working sample. :)
I certainly don't agree with your downvote: $array1INS gets defined when assigning to it.
Did you actually test the link posted? it works. You downvoted my suggestion without even using common sense or knowledge of PHP to know $array1INS can't be undefined there. Anyway, this is not the place to fight, and I'm stopping here.
No, it doesn't. Reread OP's question. Here's what you actually had to do. And nobody is fighting. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.