Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange error. Need to push numbers to array with quotes. Seems simple

Status
Not open for further replies.

Zen20

Programmer
Jul 30, 2008
5
0
0
US
Hello,

Summary:

I push numbers to array, then when I use implode and it works fine
But if I add quotes to these numbers (which I need), implode will throw zeros instead of my numbers

More details:

I have an array of numbers (initially when I get some info from DB, it is without any quotes, when I push values to "$values" array):

array_push ($values, $number); // $number is one element from DB

Array will look like this (no quotes):

array(123, 2765, 143, 187);

So when I use - echo implode(',',$values); it looks like this: "123, 2765, 143, 187"

I need to add single quotes around these numbers (but this is not the problem)

array_push($values, "'" . $number . "'"); // Adding quotes, also tried adding quotes before pushing to array

Now, if I print each element of array, it will be with quotes as I want it to be:

'123'
'2765'
...

But here is a problem:

echo implode(',',$values);

Relult is all zeros

0,0,0,0,0,0,.....

I've tried many different combination and nothing helped.

Here are some examples:
=======
$number = "'" . $row['value'] . "'";
array_push($values, $str); // added quotes and then pushed
=======
array_push($names, "'" . $number . "'"); // added single quotes
=======
$values[] = "\' . $number . \'"; // tried to escape quotes, since it might confuse implode (',',$numbers);
=======
$values[] = "{$number}"; // This did not help either
=======

Please help!!!

Here is all together:
=======
while ($row = mysql_fetch_array($result)){
array_push($values, "'" . $number . "'");
}
implode(',',$values);
=======
Output:
0,0,0,0,0
 
Its a little confusing, but taking the follwing coide to be the one you are using:

Code:
while ($row = mysql_fetch_array($result)){
        array_push($values, "'" . $number . "'");
        }
implode(',',$values);

In this code you never assign any value to $number at all. You get your rows from the database in $row, but then do nothing with it.

Perhaps:

Code:
while ($row = mysql_fetch_array($result)){
[red]$number=$row['value'];[/red]
        array_push($values, "'" . $number . "'");
        }
implode(',',$values);


If your actual code does include the assignment from $row to $number then I would suggest you copy and post your code exactly so we don't have to guess what it is you are doing.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Looks like Phil hit the nail on the head there as to why you were getting zeros, though there is another way to get quotes around all your numbers.
Code:
$final = "'" . implode("','", $values) . "'";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top