拆分/子串控阵列 - PHP

时间:2015-09-01 08:29:53

标签: php arrays variables split

过去几天我一直在粉碎我的脑袋,因为我无法为我的问题创造解决方案。

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

[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": 

make和数组看起来像这样:

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
)

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

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

有没有人能解决我的问题?

3 个答案:

答案 0 :(得分:0)

将您的数据拆分为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'];
}

答案 1 :(得分:0)

试试这个:

     $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);

答案 2 :(得分:0)

应该简单:

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