【问题标题】:Converting PHP Array to Json Object将 PHP 数组转换为 Json 对象
【发布时间】:2014-11-09 21:20:54
【问题描述】:

我有以下问题:

我有一个如下所示的 php 数组:

Array (
 [0] => Array (
    [0] => Array ( [sales_count] => 2 )
    [1] => Array ( [customer_id] => 1 )
 ) 
[1] => Array (
    [0] => Array ( [sales_count] => 3 )
    [1] => Array ( [customer_id] => 2 ) 
 ) 
) 

现在如果我在这个数组上使用 json_encode,我会得到以下结果:

[[{"sales_count":"2"},{"customer_id":"1"}],[{"sales_count":"3"},{"customer_id":"2"}]] 

但是,我正在尝试获得以下输出:

 [{"sales_count":"2","customer_id":"1"},{"sales_count":"3","customer_id":"2"}]

我怎样才能做到这一点? 我已经阅读了很多类似的问题,我使用他们的答案来达到这一点,但是我没有找到这个输出的解决方案。

提前非常感谢!

【问题讨论】:

  • 您有二维数组并想输出一维数组。所以你显然首先需要将你的数组转换为一维,然后 json_encode 会得到预期的结果
  • 创建数组的代码在哪里...展平它
  • 不,你不想要你描述的结果。我想不出任何有用的场景,即您在同一数组中的相同字段名称中有重复数据。这可能是我想象的失败,但我仍然认为你的解决方案可能行不通,或者至少会造成比其价值更多的麻烦。
  • @TuncayGöncüoğlu 你显然不会使用 JSON API 做太多前端 AJAX 工作
  • 对不起,我真的很陌生,但我想我确实想要这个结果。我很确定像你提到的那样做这不是最好的,但我希望它至少能够工作并稍后清理它。不,我真的不怎么做 AJAX,但我下一步是尝试并学习如何去做。

标签: php arrays json object


【解决方案1】:

这是因为原始数组中有两个数组,索引为 0 和 1

你需要做这样的事情

$masterArray = Array (
 [0] => Array (
    [0] => Array ( [sales_count] => 2 )
    [1] => Array ( [customer_id] => 1 )
 ) 
[1] => Array (
    [0] => Array ( [sales_count] => 3 )
    [1] => Array ( [customer_id] => 2 ) 
 ) 
);

$json_array = array_merge($masterArray[0], $masterArray[1]);

echo json_encode($json_array);

$masterArray 的语法可能有误,但请遵循概念。

【讨论】:

  • 感谢您的回答。 $json_array = array_merge($masterarray[0][0], $masterarray[0][1]);给我我需要的东西。如果我不知道每个外部和内部数组有多少条目,我将如何循环?
  • 使用以$i = 0 开头的循环,直到$i 到达sizeof($masterarray[0])。你想让我也为此编写代码吗?
  • 我试试,如果不行,我会再问你是否可以?
  • 哇哦我做到了!非常感谢!
【解决方案2】:

你的数组应该是:

$data = array(
  array("sales_count" => 2),
  array("customer_id" => 1),
  array("sales_count" => 2),
  array("customer_id" => 1),
 );
json_encode($data);

为您实现预期的输出。

如果您的数组正确,您可以通过

访问您的 json 对象
var data = [
   [
    {"sales_count":"2"},
    {"customer_id":"1"}
   ],
    [
     {"sales_count":"3"},
     {"customer_id":"2"}
    ]
 ];

data[0][0].sales_count will access sales_count = 2 on your 1st array.

【讨论】:

    【解决方案3】:

    嗯,你可以重组它们并将它们放入一个新的。示例:

    $new_array = array();
    array_walk_recursive($array, function($val, $key) use (&$new_array) {
        $new_array[] = array($key => $val);
    });
    
    $new_array = json_encode($new_array);
    echo '<pre>';
    print_r($new_array);
    // [{"sales_count":2},{"customer_id":1},{"sales_count":3},{"customer_id":2}]
    

    或者只是一个简单的循环,只是简单地将它们推入:

    $new_array = array();
    foreach($array as $values) {
        foreach($values as $val) {
            $new_array[] = $val;
        }
    }
    
    echo json_encode($new_array);
    

    示例输出如上。

    【讨论】:

      【解决方案4】:

      我来自越南。我的英语不好。所以,我写了这段代码。希望对您有所帮助。

       $arr = array(
         0 => array(0 => array('sales_count'=>2),1 => array('customer_id' => 1)),
         1 => array(0 => array('sales_count'=>3),1 => array('customer_id' => 2)),
      );
      $new_arr = array();
      foreach($arr as $key => $value){
        foreach($value as $kvalue => $vvalue){
           $new_arr[] = $vvalue;
        }
      }
      print_r(json_encode($new_arr));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 2013-08-28
        • 1970-01-01
        • 1970-01-01
        • 2018-10-09
        • 2012-07-21
        • 2020-10-02
        相关资源
        最近更新 更多