0

I'm sitting on this for a long time and can't find a solution:

By using the function json_ encode in php file (script below) i receives data from mysql as group of arrays e.g. ["9,8","15,14","18,17","29,40,10,9"],in the main file by use $.parseJSON function(script below),

receives a array with indexed object as ["9,8","15,14","18,17","29,40,10,9"....] instead [9,8,15,14,18,17,29,40,10,9],

How merge all this object together??

I tried to use $.merge function, but doesn't work.Help??;-)

/////receive.php file//////
$ask = mysql_query("SELECT numbers FROM bying");
if(!ask)
{
    die('incorrect ask'.mysql_error());
}
else{
    $tab = array();
    while ($row = mysql_fetch_assoc($ask)) 
    {

    $data=$row['numbers'];
    array_push($tab,$data);

}

echo json_encode($tab);
mysql_free_result($ask);
}

html file with $.parseJSON function

$.post('receive.php', function(data) 
                {

                 var table1=[];
                 table1 = $.parseJSON(data);
                  var numbers=$.merge([],table1);)
                  for(var i=0;i<numbers.length;i++)
                  {
                   alert(numbers[i]); 

                  }
}  
JvdBerg
  • 21,777
  • 8
  • 38
  • 55
martin
  • 13
  • 3
  • Martin, if you like the answer by Krycke you must accept his answer by clicking on the check box. You can read more on Meta: http://meta.stackexchange.com/a/5235/195067 – Ilia Ross Sep 13 '12 at 06:30
  • Using `mysql_*` is not secure and safe, and makes your code vulnerable to SQL injections, please read more here: http://stackoverflow.com/questions/11939226/sql-injections-and-adodb-library-general-php-website-security-with-examples – Ilia Ross Sep 13 '12 at 06:32

2 Answers2

1

It looks as $data is a comma separated string of numbers, if so, you can slit it on commas and then merge the two arrays:

$ask = mysql_query("SELECT numbers FROM bying");
if(!ask)
{
    die('incorrect ask'.mysql_error());
}
else {
    $tab = array();
    while ($row = mysql_fetch_assoc($ask)) {
        $data = explode( ',', $row['numbers'] );
        $tab = array_merge( $tab, $data );
    }

    echo json_encode($tab);
    mysql_free_result($ask);
}
Krycke
  • 3,106
  • 1
  • 17
  • 21
0

Why not do it the 'obvious' way and iterate through the array yourself splitting each string on ',' and appending each parsed number to an array you construct as you go?

e.g.

  var newNums = [];
  for (var i=0; i<table1.length; i++) {
      newNums = newNums.concat(table1[i].split(','));
  }
drobert
  • 1,230
  • 8
  • 21