使用for循环在PHP中创建JSON对象

使用for循环在PHP中创建JSON对象,php,json,rest,Php,Json,Rest,我正在尝试创建一个JSON对象,如下所示: "results": [ {"key":"1","trackname":"graham@insideoutrenovations.com.au"}, {"key":"1","trackname":"sjwindsor@westnet.com.au"}, ] 0: "{"key":"1","trackname":"graham@insideoutrenovations.com.au"}" 1: "{"key":"1","trackname":"sjwi

我正在尝试创建一个JSON对象,如下所示:

"results": [
{"key":"1","trackname":"graham@insideoutrenovations.com.au"},
{"key":"1","trackname":"sjwindsor@westnet.com.au"},
]
0: "{"key":"1","trackname":"graham@insideoutrenovations.com.au"}"
1: "{"key":"1","trackname":"sjwindsor@westnet.com.au"}"
然而,我的PHP代码正在为每次迭代创建一个新对象,json如下所示:

"results": [
{"key":"1","trackname":"graham@insideoutrenovations.com.au"},
{"key":"1","trackname":"sjwindsor@westnet.com.au"},
]
0: "{"key":"1","trackname":"graham@insideoutrenovations.com.au"}"
1: "{"key":"1","trackname":"sjwindsor@westnet.com.au"}"
以下是我的php:

$results = array();

function json_response($trackName) {
return json_encode(array(
    'key' => '1',
    'trackname' => $trackName
));
}

//There is 3 reg expressions for each primary array so we need to iterate by 3, otherwise we will get 3 lots of the same email address
for ($i = 0; $i < $numMatches; $i = $i + $patternNum) {
    $results[] = json_response($matches[0][$i]);
}
//$results = call_user_func_array('array_merge',$results);
echo json_encode($results);
$results=array();
函数json_响应($trackName){
返回json_编码(数组(
“键”=>“1”,
“trackname”=>$trackname
));
}
//每个主数组有3个reg表达式,所以我们需要迭代3次,否则我们将得到3个相同的电子邮件地址
对于($i=0;$i<$numMatches;$i=$i+$patternNum){
$results[]=json_响应($matches[0][$i]);
}
//$results=调用用户函数数组('array\u merge',$results);
echo json_编码($results);

在final
echo
中,您用json编码了一个已经编码的数组


json\u response
函数中删除
json\u encode

在final
echo
中,您在json中编码一个已经编码的数组


json\u response
函数中删除
json\u encode

您需要删除第一个json\u encode函数

您可以尝试创建关联数组或使用function compact,这将为您完成这项工作

//There is 3 reg expressions for each primary array so we need to iterate by   3, otherwise we will get 3 lots of the same email address
  for ($i = 0; $i < $numMatches; $i = $i + $patternNum) {
    $results[] = $matches[0][$i]; // json_encode removed
}

echo json_encode(array('results' => $results));
// or
echo json_encode(compact('results'));
//每个主数组有3个reg表达式,因此我们需要迭代3次,否则我们将得到3个相同的电子邮件地址
对于($i=0;$i<$numMatches;$i=$i+$patternNum){
$results[]=$matches[0][$i];//删除json_编码
}
echo json_编码(数组('results'=>$results));
//或
echo json_编码(压缩(“结果”);

您需要删除第一个json_encode函数

您可以尝试创建关联数组或使用function compact,这将为您完成这项工作

//There is 3 reg expressions for each primary array so we need to iterate by   3, otherwise we will get 3 lots of the same email address
  for ($i = 0; $i < $numMatches; $i = $i + $patternNum) {
    $results[] = $matches[0][$i]; // json_encode removed
}

echo json_encode(array('results' => $results));
// or
echo json_encode(compact('results'));
//每个主数组有3个reg表达式,因此我们需要迭代3次,否则我们将得到3个相同的电子邮件地址
对于($i=0;$i<$numMatches;$i=$i+$patternNum){
$results[]=$matches[0][$i];//删除json_编码
}
echo json_编码(数组('results'=>$results));
//或
echo json_编码(压缩(“结果”);

你有什么问题?你有什么问题?