【问题标题】:How to filter an array to form another array [closed]如何过滤一个数组以形成另一个数组[关闭]
【发布时间】:2021-02-09 13:44:54
【问题描述】:

对于标题描述不佳,我深表歉意,这是我有这个数组所面临的问题。

data=[
      {name: "jhon", subject:"bio", total:40},
      {name: "jhon", subject:"chem", total:80},
      {name: "jhon", subject:"geo", total:50},
      {name: "jhon", subject:"art", total:100},
      {name: "jane", subject:"bio", total:40},
      {name: "jane", subject:"chem", total:80},
      {name: "jane", subject:"geo", total:50},
      {name: "jane", subject:"art", total:100}
      ]

我想过滤它,以便我得到这个数组并使用 php,

result=[
        {name: "jhon", bio:40, chem:80, geo:50,art:100},
        {name: "jane", bio:40, chem:80, geo:50,art:100}
       ]

【问题讨论】:

  • 解码json,使用array_filter
  • 我该怎么做?提前谢谢你
  • 请展示自己的努力。
  • “我该怎么做?” - 你先做一些研究/阅读上面提到的函数是如何工作的,然后你尝试一些东西。如果你没有成功,那么你在这里报告,并提供正确的问题描述 (How to Ask) 和 minimal reproducible example 你所做的事情。

标签: php arrays filter


【解决方案1】:

你可以这样做:

$data = '[
      {"name":"jhon","subject":"bio","total":40},
      {"name":"jhon","subject":"chem","total":80},
      {"name":"jhon","subject":"geo","total":50},
      {"name":"jhon","subject":"art","total":100},
      {"name":"jane","subject":"bio","total":40},
      {"name":"jane","subject":"chem","total":80},
      {"name":"jane","subject":"geo","total":50},
      {"name":"jane","subject":"art","total":100}
      ]';

// Setting the second parameter to `true` means it decodes to an
// associative array...
$array = json_decode($data, true);

// Initialise the transformation array (empty array)
$transformArray = [];

foreach($array as $item){
    $transformArray[$item["name"]]["name"]           = $item["name"];
    $transformArray[$item["name"]][$item["subject"]] = $item["total"];
}

// Re-index the array to numeric and convert to JSON string
$output = json_encode( array_values($transformArray) );

echo $output;

输出:

[
    {"name":"jhon","bio":40,"chem":80,"geo":50,"art":100}, 
    {"name":"jane","bio":40,"chem":80,"geo":50,"art":100}
]

【讨论】:

  • 美观又简单。
【解决方案2】:

如果您使用特殊的 PHP 类,这也很容易。

require 'class/tableArray.php';
//https://github.com/jspit-de/tableArray

$jsonData = '[
      {"name": "jhon", "subject":"bio", "total":40},
      {"name": "jhon", "subject":"chem", "total":80},
      {"name": "jhon", "subject":"geo", "total":50},
      {"name": "jhon", "subject":"art", "total":100},
      {"name": "jane", "subject":"bio", "total":40},
      {"name": "jane", "subject":"chem", "total":80},
      {"name": "jane", "subject":"geo", "total":50},
      {"name": "jane", "subject":"art", "total":100}
      ]';

$newJson = tableArray::createFromJson($jsonData)
  ->pivot('name','total','subject')
  ->fetchAllAsJson(JSON_PRETTY_PRINT)
;

//test output
echo '<pre>';
echo $newJson;

结果中使用的键与问题中要求的键不同。我认为这些键更易于理解和方便地按名称访问值。

{
    "jhon": {
        "name": "jhon",
        "total.bio": 40,
        "total.chem": 80,
        "total.geo": 50,
        "total.art": 100
    },
    "jane": {
        "name": "jane",
        "total.bio": 40,
        "total.chem": 80,
        "total.geo": 50,
        "total.art": 100
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多