将数组转换为JSON-PHP [英] Transforming array to JSON - PHP

查看:119
本文介绍了将数组转换为JSON-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从这样的数据库中获取结果.

I am going to get results from database like this.

 array(
         [0] => array( 
                       [type]   => orange,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 
         [1] => array( 
                       [type]   => mango,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 
         [1] => array( 
                       [type]   => apple,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 
         [3] => array( 
                       [type]   => mango,
                       [number] => 10,
                       [size]   => 10c,
                       [weight] => 1l
                     ) 

     )

基本上,我需要将其转换为以下JSON格式:

Basically, I need to convert this to the following JSON Format:

         "mango" : [{ 
                       "number" : "10",
                       "size"   : "10c",
                       "weight" : "1l"
                     },
                     { 
                       "number" : "12",
                       "size"   : "14c",
                       "weight" : "12"
                     }
                   ],
         "orange" : [{ 
                       "number" : "12",
                       "size"   : "10c",
                       "weight" : "1l"
                     },
                     { 
                       "number" : "12",
                       "size"   : "14c",
                       "weight" : "11"
                     }
                   ],
         "apple" : [{ 
                       "number" : "10",
                       "size"   : "10c",
                       "weight" : "1l"
                     },
                     { 
                   ]

忽略数字,大小和重量.这些只是随机数. 但是数组中的第一个元素包含JSON标签.应该将其从数组中删除,但应作为JSON中对象数组的标签(您可以看到).

Ignore the number, size and weight. Those are just random numbers. But the first element in the array contains the JSON tag. That should be removed from the array, but taken as tag for array of objects in JSON (you can see that).

我知道我们可以编写带有循环,条件等的代码.是否有一种聪明而又快速的方法,可以使用现有的内置php函数(具有更少的循环和条件)来做到这一点?

I know we can write with loops, conditions, etc. Is there a smart and quick way to do this with existing built in php functions with less looping and conditions?

我想这些是步骤:

  1. 根据类型过滤数组的个性.大概在这里3次才能得到3个水果.哪个功能对您有帮助?根据价值进行过滤.或理想的键值对.
  2. 您将获得3个数组,每个数组用于一种水果.分别弹出所有数组中的type元素.
  3. 浏览类型,然后将数组放入每种类型中,以类型值作为索引.

还有其他更好的方法吗?

Any other better ways?

推荐答案

将您的旧数组重构为这样的新数组:

Restructure your old array to a new one like this :

$new = array();
foreach($old as $vec){
    $new[$vec['type']] = array(
        'number' => $vec['number'],
        'size' => $vec['size'],
        'weight' => $vec['weight']      
    );
}
echo json_encode($new);

这篇关于将数组转换为JSON-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆