【问题标题】:Splitting/Substringing Array - PHP拆分/子串数组 - PHP
【发布时间】:2015-11-26 09:59:00
【问题描述】:

过去几天我一直在头疼,因为我无法为我的问题找到解决方案。

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

[0] => {"count": 1038, "previous": null, "results": [{"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "acbb6a8790604ac9916772f51729e69a", "reference": "2015-08-28-xxx@xxx.xx", "value": "0.0000", "member": "a7cab5fa045d44beb72da3ea26e6a49c", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"}
[1] => {"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "5ba77c689aba48f8ab845c9578b96d1d", "reference": "2015-08-28-xxxx@xxx.xx", "value": "0.0000", "member": "477039a270d841aaa87a5578ee034d60", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"}

我想要的是删除数组的第一部分

[0] => {"count": 1038, "previous": null, "results": 

然后制作和数组看起来像这样:

Array
(
[0] => Array
(
[data_source] => f31904c94a72490e8fa5750399a14e44
[uuid] => acbb6a8790604ac9916772f51729e69a
[reference] => 2015-08-28-xxx@xxx.xx
[value] => 0.0000
[member] => a7cab5fa045d44beb72da3ea26e6a49c
[HPSalesBudget] => 1.00
[team] => null
[date] => 2015-08-28T00:00:00
)

[1] => Array
(
[data_source] => f31904c94a72490e8fa5750399a14e44
[uuid] => acbb6a8790604ac9916772f51729e69a
[reference] => 2015-08-28-xxx@xxx.xx
[value] => 0.0000
[member] => a7cab5fa045d44beb72da3ea26e6a49c
[HPSalesBudget] => 1.00
[team] => null
[date] => 2015-08-28T00:00:00
)

我的数组看起来像这样的唯一原因是因为这是我获取数据的格式。

我试过用谷歌搜索这个问题,但没有任何运气。

有人能解决我的问题吗?

【问题讨论】:

  • 该数组是array 类型还是string?
  • 我认为你的数组字符串有问题,因为第一行末尾的括号后面应该有一个逗号,或者至少有另一个括号来关闭它...

标签: php arrays variables split


【解决方案1】:

使用explode("\n", $data); 之类的内容将数据分成几行

然后逐行处理数据,去掉每行前7个字符,这样你的数据就只有这个了:

{"count": 1038, "previous": null, "results": [{"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "acbb6a8790604ac9916772f51729e69a", "reference": "2015-08-28-xxx@xxx.xx", "value": "0.0000", "member": "a7cab5fa045d44beb72da3ea26e6a49c", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"}

然后在每一行使用json_decode。

从那里,很容易以您需要的格式获取数据:

$result = array();
$result[] = $json['results'];

我猜代码应该是这样的:

$result = array();
$lines = explode("\n", $data);
foreach ($lines as $line) {
    $line = substr($line, 7);
    $json = json_decode($line, true);
    $result[] = $json['results'];
}

【讨论】:

    【解决方案2】:

    试试这个:

         $result = json_decode((your_json), true); //decode your json to array
    
         $newArr = [];
        foreach($result['results'] as $key => $val) {
          $newArr[] = ['data_source'=> $val['data_source']
                        'uuid' => $val['uuid'], 
                        'reference' => $val['reference'],
                        'value' => $val['value'],
                        'member' => $val['member'],
                        'HPSalesBudget' => $val['HPSalesBudget'],
                         'team' => $val['team'],
                         'date'=> $val['date']] ;
         }
         print_r($newArr);
    

    【讨论】:

    • 感谢您的回答。我不是那么喜欢 Json,我的 json 字符串应该是什么样子?
    • 这是你的问题。这是一个 json 类型的数组。因此,如果您只想实现数组。解码。
    【解决方案3】:

    应该就这么简单:

    $myarray[0] = $myarray[0]["results"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多