Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php输出转换为JSON_Php_Json - Fatal编程技术网

将php输出转换为JSON

将php输出转换为JSON,php,json,Php,Json,我从PHP脚本中获得以下输出: one@gmail.com test1 two@gmail.com test2 由以下内容生成: $i = 0; $header = array(); while (!feof($handle)) { $buffer = fgets($handle, $chunk_size); if (trim($buffer)!=''){ $obj = json_decode($buffer); echo $obj[0]."

我从PHP脚本中获得以下输出:

one@gmail.com test1
two@gmail.com test2
由以下内容生成:

$i = 0;
  $header = array();
  while (!feof($handle)) {
    $buffer = fgets($handle, $chunk_size);
   if (trim($buffer)!=''){
      $obj = json_decode($buffer);

       echo $obj[0]." ".$obj[2]."<br>";

      $i++;
    }
  }
fclose($handle);
该脚本取自Mailchimp API,该API列出了列表的订阅者。 以下是供参考的脚本:

<?php
$apikey = '1234-us7';
$list_id = '1234';
$chunk_size = 4096; //in bytes
$url = 'http://us7.api.mailchimp.com/export/1.0/list?apikey='.$apikey.'&id='.$list_id.'&output=json';


/** a more robust client can be built using fsockopen **/
$handle = @fopen($url,'r');
if (!$handle) {
  echo "failed to access url\n";
} else {
  $i = 0;
  $header = array();
  while (!feof($handle)) {
    $buffer = fgets($handle, $chunk_size);
   if (trim($buffer)!=''){
      $obj = json_decode($buffer);
      if ($i==0){
        //store the header row
        $header = $obj;
      } else {
        //echo, write to a file, queue a job, etc.
       echo $obj[0]." ".$obj[2]."<br>";


       }
      $i++;
    }
  }

  fclose($handle);
}
?>


谢谢大家!

它似乎已经是JSON格式的,因为您正在使用
JSON\u decode
获取该输出。所以就。。。停止在上面使用json解码。

$i=0;
$i = 0;
$result = array();  //create a new array
  $header = array();
  while (!feof($handle)) {
    $buffer = fgets($handle, $chunk_size);
   if (trim($buffer)!=''){
      $obj = json_decode($buffer);

       //echo $obj[0]." ".$obj[2]."<br>";  //comment out this line
       $result[] = array('email' => $obj[0], 'option' => $obj[2]);  //push new obj to the array

      $i++;
    }
  }
fclose($handle);
echo json_encode(array('emails' => $result)); // convert to json format
$result=array()//创建一个新数组 $header=array(); 而(!feof($handle)){ $buffer=fgets($handle,$chunk\u size); 如果(修剪($buffer)!=''){ $obj=json_解码($buffer); //echo$obj[0]。“”.$obj[2]。“
”;//注释掉这一行 $result[]=array('email'=>$obj[0],'option'=>$obj[2]);//将新obj推送到数组中 $i++; } } fclose($handle); echo json_encode(数组('emails'=>$result));//转换为json格式
正如@jessica已经提到的,$buffer似乎以JSON的形式出现在您面前,因为您正在它上面运行
JSON\u decode(&buffer)

$i = 0;
$result = array();  //create a new array
  $header = array();
  while (!feof($handle)) {
    $buffer = fgets($handle, $chunk_size);
   if (trim($buffer)!=''){
      $obj = json_decode($buffer);

       //echo $obj[0]." ".$obj[2]."<br>";  //comment out this line
       $result[] = array('email' => $obj[0], 'option' => $obj[2]);  //push new obj to the array

      $i++;
    }
  }
fclose($handle);
echo json_encode(array('emails' => $result)); // convert to json format
但是,如果要执行某些操作,请排列数据,以便构建如下数组:

$myArray = array(
    'emails' => array(
         array('email' => 'one@gmail.com','option' => 'test1'),
         array('email' => 'two@gmail.com','option' => 'test2'),
    )
);
然后:

使用您的supllied代码,它将是这样的(未经测试):


你只是想通过mailchimp API响应吗?是的,这在一秒钟内就完成了!非常感谢。我能接受的时候就接受。
echo json_encode(myArray);
$myArray = array();
$i = 0;
$header = array();
while (!feof($handle)) {

    $buffer = fgets($handle, $chunk_size);

    if (trim($buffer)!='') {

        $obj = json_decode($buffer);

        $myArray['emails'][] = array('email' => $obj[0],'option' => $obj[2]);

        $i++;
    }
}

fclose($handle);

echo json_encode($myArray);