laravel 创建新的变量数组[已关闭]

qhhrdooz  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(134)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

8小时前关门了。
Improve this question
我需要从另一个数组变量中创建一个新的数组。所以我有3个变量
1.$交易

[
    {
        "id": 2,
        "member_id": 19,
        "date_start": "2023-03-14",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Lee Rosenbaum"
    },
    {
        "id": 3,
        "member_id": 14,
        "date_start": "2023-03-31",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Erik Spinka"
    },
    {
        "id": 4,
        "member_id": 15,
        "date_start": "2023-03-26",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Anabel Monahan"
    },
    {
        "id": 5,
        "member_id": 9,
        "date_start": "2023-02-21",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Anthony Wilkinson"
    },
    {
        "id": 6,
        "member_id": 15,
        "date_start": "2023-02-23",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Anabel Monahan"
    },
    {
        "id": 7,
        "member_id": 13,
        "date_start": "2023-04-08",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Pink Moen"
    },
    {
        "id": 9,
        "member_id": 10,
        "date_start": "2023-02-23",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Viva Wilkinson"
    }
]

1.$朱姆拉

[
    {
        "transaction_id": 2,
        "total": "5"
    },
    {
        "transaction_id": 3,
        "total": "7"
    },
    {
        "transaction_id": 4,
        "total": "2"
    },
    {
        "transaction_id": 5,
        "total": "4"
    },
    {
        "transaction_id": 6,
        "total": "4"
    },
    {
        "transaction_id": 7,
        "total": "2"
    },
    {
        "transaction_id": 9,
        "total": "2"
    }
]

1.$唐格尔

[
    {
        "transaction_id": 2,
        "tgl_kembali": "2023-05-25"
    },
    {
        "transaction_id": 3,
        "tgl_kembali": null
    },
    {
        "transaction_id": 4,
        "tgl_kembali": null
    },
    {
        "transaction_id": 5,
        "tgl_kembali": null
    },
    {
        "transaction_id": 6,
        "tgl_kembali": null
    },
    {
        "transaction_id": 7,
        "tgl_kembali": "2023-02-17"
    },
    {
        "transaction_id": 9,
        "tgl_kembali": "2023-04-21"
    }
]

我需要创建一个新变量,如$newData
我预计$newData的结果应该是这样的:

[
    {
        "id": 2,
        "member_id": 19,
        "date_start": "2023-03-14",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Lee Rosenbaum",
        "tgl_kembali": "2023-05-25",
        "total": "5"
    },
    {
        "id": 3,
        "member_id": 14,
        "date_start": "2023-03-31",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Erik Spinka",
        "tgl_kembali": null,
        "total": "7"
    },
    {
        "id": 4,
        "member_id": 15,
        "date_start": "2023-03-26",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Anabel Monahan",
        "tgl_kembali": null,
        "total": "2"
    },
    {
        "id": 5,
        "member_id": 9,
        "date_start": "2023-02-21",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Anthony Wilkinson",
        "tgl_kembali": null,
        "total": "4"
    },
    {
        "id": 6,
        "member_id": 15,
        "date_start": "2023-02-23",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Anabel Monahan",
        "tgl_kembali": null,
        "total": "4"
    },
    {
        "id": 7,
        "member_id": 13,
        "date_start": "2023-04-08",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Pink Moen",
        "tgl_kembali": "2023-02-17",
        "total": "2"
    },
    {
        "id": 9,
        "member_id": 10,
        "date_start": "2023-02-23",
        "created_at": "2023-02-12T10:03:46.000000Z",
        "updated_at": "2023-02-12T10:03:46.000000Z",
        "name": "Viva Wilkinson",
        "tgl_kembali": "2023-04-21",
        "total": "2"
    }
]

我能做些什么来解决这个问题?我在PHP(Laravel)上工作。由于某种原因,我一直无法在任何地方找到答案。我假设这是可能的,但我只需要确定一下。

6qftjkof

6qftjkof1#

const newData = [];

for (const transaction of transactions) {
  const jumlah = jumlahs.find(j => j.transaction_id === transaction.id);
  const tanggal = tanggals.find(t => t.transaction_id === transaction.id);
  
  newData.push({
    id: transaction.id,
    member_id: transaction.member_id,
    date_start: transaction.date_start,
    created_at: transaction.created_at,
    updated_at: transaction.updated_at,
    name: transaction.name,
    total: jumlah.total,
    tgl_kembali: tanggal.tgl_kembali
  });
}

相关问题