簡體   English   中英

php 合並兩個單獨的 arrays

[英]php merge two separate arrays

基本連接兩個具有相同結構的 arrays 時遇到問題。 嘗試簡單地使用 array_push 或 array_merge 但沒有找到解決方案。 如何處理?

基本數組結構如下

array(2) {
  [0]=>
  array(5) {
    ["AccountNumber"]=>
    int(1010)
    ["AccountName"]=>
    string(19) "text"
    ["Balance"]=>
    float(0)
    ["AccountType"]=>
    int(0)
    ["AccountTypeDescription"]=>
    string(36) "text"
  }
  [1]=>
  array(5) {
    ["AccountNumber"]=>
    int(1011)
    ["AccountName"]=>
    string(50) "text"
    ["Balance"]=>
    float(0)
    ["AccountType"]=>
    int(0)
    ["AccountTypeDescription"]=>
    string(36) "text"
  }
}

如果我在 array_merge 下面的迭代中加入兩個 arrays 不起作用,則只會出現第一個數組。 有什么線索嗎? 我猜想繼續在第一個數組上構建是行不通的?

$array1 = ....;

for ($x = 1; $x <= $TotalNumberOfPages; $x++) { 
    $ch = curl_init('URL');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-type: application/json',
        'Authorization: Bearer '.$token.''
    ));
    $result2=curl_exec($ch);
    curl_close($ch);
    $array1 = array_merge($array1,json_decode($result2, true)["Data"]); 
}

這是一個例子

<?php

$array1 = [
    [
        "AccountNumber" => 1010,
        "AccountName" => 'text',
        "Balance" => 0,
        "AccountType" => 0,
        "AccountTypeDescription" => 'text'
    ],
    [
        "AccountNumber" => 2020,
        "AccountName" => 'text',
        "Balance" => 0,
        "AccountType" => 0,
        "AccountTypeDescription" => 'text'
    ]

];
$array2 = [
    [
        "AccountNumber" => 3030,
        "AccountName" => 'text',
        "Balance" => 0,
        "AccountType" => 0,
        "AccountTypeDescription" => 'text'
    ],
    [
        "AccountNumber" => 4040,
        "AccountName" => 'text',
        "Balance" => 0,
        "AccountType" => 0,
        "AccountTypeDescription" => 'text'
    ]

];
$array3 = array_merge($array1, $array2);
echo print_r($array3);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM