PHP:如何将此数组拆分为两个单独的数组或值?

时间:2018-03-24 16:58:18

标签: php arrays

 var myVariable = GetComponent(BasketballSceneChange) as myBaseClass;

 myVariable.enabled = false;

我希望能够将这两个值拉成两个单独的值,例如," 1519106711000" &安培; " 1008.48",日期和费率。

3 个答案:

答案 0 :(得分:1)

你在这里:

$json = json_decode('["1519106711000 1008.48","1519107011000 1000.28","1519107311000 1009.89","1519107611000 1000","1519107910000 1006.52","1519108211000 985.68","1519108511000 1009.16","1519108812000 1003.5","1519109111000 998.09"]');

$data = [];
foreach ($json as $row) {
    $row = explode(' ', $row);
    $data[] = ['date' => $row[0], 'rate' => $row[1]];
}

// you can use $data however you want
// it will give you something like this :
/*
[{"date":"1519106711000","rate":"1008.48"},{"date":"1519107011000","rate":"1000.28"},{"date":"1519107311000","rate":"1009.89"},{"date":"1519107611000","rate":"1000"},{"date":"1519107910000","rate":"1006.52"},{"date":"1519108211000","rate":"985.68"},{"date":"1519108511000","rate":"1009.16"},{"date":"1519108812000","rate":"1003.5"},{"date":"1519109111000","rate":"998.09"}]
*/

答案 1 :(得分:0)

您可以将以下regexpreg_match_all一起使用,以实现您的目标,

$json='{  
   "cae7fc":[  
      "1519106711000 1008.48",
      "1519107011000 1000.28",
      "1519107311000 1009.89",
      "1519107611000 1000",
      "1519107910000 1006.52",
      "1519108211000 985.68",
      "1519108511000 1009.16",
      "1519108812000 1003.5",
      "1519109111000 998.09"
   ]
}';

preg_match_all('/(?<=")\d+/', $json, $date);
preg_match_all('/[\d\.]+(?=")/', $json, $rate);

这将为您提供$date$rate数组,如下所示

<强> $date

Array
(
    [0] => Array
        (
            [0] => 1519106711000
            [1] => 1519107011000
            [2] => 1519107311000
            [3] => 1519107611000
            [4] => 1519107910000
            [5] => 1519108211000
            [6] => 1519108511000
            [7] => 1519108812000
            [8] => 1519109111000
        )

)

<强> $rate

Array
(
    [0] => Array
        (
            [0] => 1008.48
            [1] => 1000.28
            [2] => 1009.89
            [3] => 1000
            [4] => 1006.52
            [5] => 985.68
            [6] => 1009.16
            [7] => 1003.5
            [8] => 998.09
        )

)

答案 2 :(得分:0)

要解决该问题,您可以使用以下功能:

  • explode
  • sscanf
  • preg_match
  • preg_split

使用sscanf的示例:

<?php

$json =
<<<EOF
{"cae7fc":
    ["1519106711000 1008.48","1519107011000 1000.28","1519107311000 1009.89","1519107611000 1000","1519107910000 1006.52","1519108211000 985.68","1519108511000 1009.16","1519108812000 1003.5","1519109111000 998.09"]}
EOF;

$data = array();
foreach((array)json_decode($json, true) as $key => $values) {
    $data[$key] = array();
    foreach ($values as $valuesPairStr) {
        sscanf($valuesPairStr, '%d %f', $date, $rate);
        $data[$key][] = array(
            'date' => $date,
            'rate' => $rate,
        );
    }
}

使用explodearray_walkarray_map的另一个例子:

<?php

$json =
<<<EOF
{"cae7fc":
    ["1519106711000 1008.48","1519107011000 1000.28","1519107311000 1009.89","1519107611000 1000","1519107910000 1006.52","1519108211000 985.68","1519108511000 1009.16","1519108812000 1003.5","1519109111000 998.09"]}
EOF;
$data = (array)json_decode($json, true);
array_walk(
    $data,
    function(&$item, $key) {
        $item = array_map(
            function($pairStr) {
                $parts = explode(' ', $pairStr);
                return array(
                    'date' => isset($parts[0]) ? (int)$parts[0] : null,
                    'rate' => isset($parts[1]) ? (float)$parts[1] : null,
                );
            },
            $item
        );
    }
);