-1

By getting query from MySQL, I have 2 arrays like this: for example: Sig:{a,b,c,d,e} T:{1,2,3,4,5}

Now, in PHP, I want to merge these two arrays and have an array like: {(a,1),(b,2),(c,3),(d,4),(e,5)} then, show it as a JSON in order to draw chart by the Google visualize API.

How can I do this?

    $data = array();
    $data['cols'] = array(
                    array('id' => 'Sig','label' => 'Sig', 'type' => 'string'),
                    array('id' => 'T','label' => 'Time', 'type' => 'string')
                    );

   $rows = array();
   while($r = mysql_fetch_assoc($result)) {
   $temp = array();

//changing the string (data in MySQL) into array of float for column Sig
   $csvdata = $r['Sig'];
   $a = explode(',', $csvdata);

    for($i=0;$i<sizeof($a);$i++)
    {
        $floa[]=floatval($a[$i]);
                }

//changing the string (data in MySQL) into array of float for column T
   $csvdata = $r['T'];
   $s = explode(',', $csvdata);
   for($i=0;$i<sizeof($s);$i++)
    {
        $flos[]=floatval($s[$i]);
                }

   function toArr(){
   return func_get_args();
   }

    $c = array_map ('toArr',$floa,$flos);
    $temp[] = array('v' => $c);

    $rows[]= array('c' => $temp);
    }

  $data['rows'] = $rows;
  echo json_encode($data); 

This is the output:

{"cols":[{"id":"Sig","label":"Sig","type":"string"},{"id":"T","label":"Time",‌​"type":"string"}],"rows":[{"c":[{"v":[[0,0],[1.2490004e-5,2.0545915e-5],[2.497876‌​8e-5,4.108994e-5],[3.7465044e-5,6.16302e-5]]}]}]}

but I need it to be like this:

   {"cols":[{"id":"Sig","label":"Sig","type":"string"},{"id":"T","label":‌"Time",‌"type":"string"}],"rows":[{"c":[{"v":0},{"v":0}]},{"c":[{"v":1.2490004e-5},{"v":2.0545915e-5}]},‌{"c":[{"v":2.497876‌8e-5},{"v":4.108994e-5}]},{"c":[{"v":3.7465044e-5},{"v":6.16302e-5}]}]}
nazanin
  • 73
  • 2
  • 14
  • Your desired output is not valid JSON. It's apparently an object since it's in curly braces (`{`...`}`), but it has no keys. It contains pairs of numbers in parentheses, which is not valid for anything in JSON. Could you please post a valid goal output? – Mark Reed Nov 12 '14 at 02:27
  • This question is not terribly clear because there is no sample input / [mcve]. – mickmackusa Sep 11 '22 at 00:18

4 Answers4

1

This should do the trick for any number of arrays:

<?php
function toArr(){
    return func_get_args();
}

$a = array ('a','b','c','d','e');
$b = array(1,2,3,4,5);
$c = array_map ('toArr',$a,$b);
var_dump(json_encode($c));
?>

Edit: I forgot about the json encode part. You can read more about it here.

iLot
  • 98
  • 1
  • 7
  • thanks but how can I have an array of smaller arrays. for instance: I want to have these arrays (a,1) (b,2) ... – nazanin Nov 12 '14 at 01:31
  • I want to draw chart by these spots as the X and Y – nazanin Nov 12 '14 at 01:33
  • @nazanin the result of the above code is `[["a",1],["b",2],["c",3],["d",4],["e",5]]`. Isn't that what you want? – iLot Nov 12 '14 at 01:38
  • this is fine, but this is the json of this code {"cols":[{"id":"Sig","label":"Signal","type":"string"},{"id":"T","label":"Time","type":"string"}],"rows":[{"c":[{"v":[[0,0],[1.2490004e-5,2.0545915e-5],[2.4978768e-5,4.108994e-5],[3.7465044e-5,6.16302e-5]]}]}]} – nazanin Nov 12 '14 at 01:55
1

Ruby and Underscore refer to that array operation as zip. PHP can zip arrays via array_map. Use json_encode to convert the zipped array to JSON.

function zipEncode($a, $b) {
  return json_encode(array_map(null, $a, $b));
}

For example zipEncode(['a', 'b'], [1, 2]) returns '[["a",1],["b",2]]'

A standalone zip function accepting any number of arrays could be:

function zip() {
  return call_user_func_array('array_map', array_merge([null], func_get_args()));
}
Community
  • 1
  • 1
ryanve
  • 50,076
  • 30
  • 102
  • 137
0

When you say you want {(a,1),(b,2),(c,3),(d,4),(e,5)}, I assume, since you asked for JSON, what you really want is this: [[a,1],[b,2],[c,3],[d,4],[e,5]], where a..e are hopefully numbers.

You can use array_map with a null function to zip arrays together in this fashion. Here's an example (with strings for the letters):

 $letters = array('a','b','c','d','e');
 $numbers = array(1,2,3,4,5);
 print(json_encode(array_map(null, $letters, $numbers)));
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
-1

Edit

Oh, sorry. Try this array_merge and this json_encode

portrait
  • 51
  • 8