【问题标题】:Pushing values into multidimensional array [duplicate]将值推入多维数组[重复]
【发布时间】:2021-01-14 16:00:45
【问题描述】:

我已经低于数组结果,如下所示。我想要的只是在其中添加更多条目。 我该怎么做?

Array
(
    [result] => Array
        (
            [0] => Array
                (
                    [number] => AAA1222
                    [short_description] => User unable to print
                    [state] => Closed
                )

            [1] => Array
                (
                    [number] => AAA1223
                    [short_description] => Share drive not accessible
                    [state] => Closed
                )

            [2] => Array
                (
                    [number] => AAA1224
                    [short_description] => Network is down
                    [state] => Closed
                )

        )
)

如果我想向数组中添加更多条目,例如

[number] => AAA1225
[short_description] => Cable replacement on workstation 12
[state] => Closed

我如何做到这一点。

谢谢!!

【问题讨论】:

标签: php html arrays multidimensional-array


【解决方案1】:

您也可以尝试使用 array_push() 函数。

你的存在数组:

$myArr['result'] = [........]

新的数组值

$data['number'] = 'AAA1224';
$data['short_description'] = 'Network is down';
$data['state'] = 'Closed';

将新数组推入现有数组:

array_push($myArr['result'],$data);

【讨论】:

    【解决方案2】:

    考虑到您将上述数组存储在名为$arr 的变量中。

    $arr = [ ... ]; // contains your existing data.
    
    // To add new data
    $arrNewData = [
        'number' => 'AAA1225',
        'short_description' => 'Cable replacement on workstation 12',
        'state' => 'Closed' 
    ];
    
    // Push data to existing array.
    $arr['result'][] = $arrNewData;
    

    【讨论】:

      【解决方案3】:

      您可以将另一个数组数据添加到现有数组中

      将现有数据保存到变量 $data 中。

      $data = [ ... ]; // here you have result index and array data
      

      然后将新的结果项添加为数组。

      $data['result'][] = [
          'number' => 'AAA1225',
          'short_description' => 'Cable replacement on workstation 12',
          'state' => 'Closed'
      ];
      

      【讨论】:

        猜你喜欢
        • 2013-05-17
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        • 2020-10-22
        • 2023-04-11
        • 2019-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多