doushuichong2589 2017-09-03 17:50
浏览 132
已采纳

如何将PHP数组作为JSON数组传递给Javascript

Currently I am doing the POST Operation to PHP file from the Javascript to perform the operation in PHP and get the response, But in the php file I am having the Array and I wanted to receive it to JavaScript as JSON Array How Can I do that

Java Script

$scope.details = function() {
  $http({
    method: 'POST',
    url: 'myjson.json'
  }).then(function(response) 
  })

PHP Code

 <?php

header('Content-Type: application/json');
// index.php
// Run the parallel get and print the total time
$s = microtime(true);
// Define the URLs
$urls = array(
  "url1",
  "url2",
  "url3"
);
$pg = new ParallelGet($urls);
//print "<br />total time: ".round(microtime(true) - $s, 4)." seconds";

// Class to run parallel GET requests and return the transfer
class ParallelGet
{
  function __construct($urls)
  {
    // Create get requests for each URL
    $mh = curl_multi_init();
    $headers = array(
    'authorization: Basic xyz',
    'cache-control' => 'no-cache',
    'Accept:application/json'
  );
    foreach($urls as $i => $url)
    {
      $ch[$i] = curl_init($url);
      curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch[$i], CURLOPT_HTTPHEADER, $headers);
      curl_multi_add_handle($mh, $ch[$i]);
    }

    // Start performing the request
    do {
        $execReturnValue = curl_multi_exec($mh, $runningHandles);
    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
    // Loop and continue processing the request
    while ($runningHandles && $execReturnValue == CURLM_OK) {
      // Wait forever for network
      $numberReady = curl_multi_select($mh);
      if ($numberReady != -1) {
        // Pull in any new data, or at least handle timeouts
        do {
          $execReturnValue = curl_multi_exec($mh, $runningHandles);
        } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
      }
    }

    // Check for any errors
    if ($execReturnValue != CURLM_OK) {
      trigger_error("Curl multi read error $execReturnValue
", E_USER_WARNING);
    }

    // Extract the content
    foreach($urls as $i => $url)
    {
      // Check for errors
      $curlError = curl_error($ch[$i]);
      if($curlError == "") {
        $res[$i] = curl_multi_getcontent($ch[$i]);
        //$finalResponse[] = $res[$i]->load();
      } else {
        print "Curl error on handle $i: $curlError
";
      }
      // Remove and close the handle
      curl_multi_remove_handle($mh, $ch[$i]);
      curl_close($ch[$i]);

    }
    // Clean up the curl_multi handle
    curl_multi_close($mh);
      //print_r($res);
    $responseArray = $res;
    // Print the response data

  $responseMSO = json_encode($res);
//print_r(utf8_encode(json_encode($res)));
  //echo $finalResponse[1];
  //echo $responseMSO;
  }

}
?>

Now I want to send these to Javascript as JSONArray

I have seen the suggestion to use json_encode in PHP but when I use that I am getting response as utf-encoded.

Any one please help here

I have referred all the below but didn't help much

Link 1

Link 2

  • 写回答

3条回答 默认 最新

  • dpdp42233 2017-09-03 18:18
    关注

    Try this:

    <?php 
    
    $myarray = array('{ "name":"中", "age":30, "car":null }','{ "name":"Mike", "age":32, "car":null }');
    foreach ($myarray as $array) {
        $arrays[] = json_decode($array);
    }
    
    echo json_encode($arrays, JSON_UNESCAPED_UNICODE);
    echo '<br>';
    echo json_encode($arrays);
    
    ?>
    

    As you can see the first sample has unescaped unicode

    OUTPUT:

    [{"name":"中","age":30,"car":null},{"name":"Mike","age":32,"car":null}] [{"name":"\u4e2d","age":30,"car":null},{"name":"Mike","age":32,"car":null}]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏