Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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,我目前有一个包含键值对的数组,如下所示,例如: Array( (int) 250 => 'blue', (int) 252 => 'green', (int) 289 => 'yellow' ) 这就是我调试它时得到的格式 I need it to be formatted as follows: ['key':250,'value':'blue'], ['key':252,'value':'green'], ['key':289,'value':'yellow'

我目前有一个包含键值对的数组,如下所示,例如:

Array(
  (int) 250 => 'blue',
  (int) 252 => 'green',
  (int) 289 => 'yellow'
)
这就是我调试它时得到的格式

I need it to be formatted as follows:
['key':250,'value':'blue'],
['key':252,'value':'green'],
['key':289,'value':'yellow']
所以基本上我需要它是key=value&value=value

然后将该数组序列化为json

关于如何做到这一点,您有什么想法吗?

在控制器中:

在您的routers.php中

例如:

结果

结果


正确的json应该类似于{250:blue,252:green,289:yellow}。为什么是奇怪的中间数据格式?我真的不知道怎么做。当我尝试按原样序列化数组时,它失败了。你建议我怎么做?我不太懂PHP,但根据你所拥有的数组进行json_编码就可以了。
if($this->RequestHandler->responseType() == 'json'){
    $data = array(
       (int) 250 => 'blue',
       (int) 252 => 'green',
       (int) 289 => 'yellow'
     );
    $this->set('data', $data);
    $this->set('_serialize', 'data');
}
Router::parseExtensions('json');
{"250":"blue","252":"green","289":"yellow"}
$data= array(
   (int) 250 => 'blue',
   (int) 252 => 'green',
   (int) 289 => 'yellow'
 );
 foreach ($data as $key => $value) {
     $data[] = array('key'=> $key,'value'=> $value);
 } 
$this->set('data', $data);
$this->set('_serialize', 'data');
[{"key":250,"value":"blue"},{"key":252,"value":"green"},{"key":289,"value":"yellow"}]