0

I'm already getting below array results as follows. All I wanted is to add more entries in it. How do I do that?

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
                )

        )
)

If I wanted to add more entries to the array like

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

How do I accomplish that.

Thanks!!

Sapna Dorbi
  • 115
  • 2
  • 12
  • Use array_push() method of php. – Rabby Sep 29 '20 at 04:39
  • 1
    Does this answer your question? [PHP add elements to multidimensional array with array\_push](https://stackoverflow.com/questions/16308252/php-add-elements-to-multidimensional-array-with-array-push) – Hirumina Sep 29 '20 at 04:48

3 Answers3

1

You can try with array_push() function too.

Your existence array:

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

New array values

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

Push new array into existance array:

array_push($myArr['result'],$data);
BukhariBaBa
  • 171
  • 5
0

Considering you are storing above array in a variable named as $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;
Dark Knight
  • 6,116
  • 1
  • 15
  • 37
0

You can add another array data to your existing array

Keep your existing data into a variable $data.

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

Then add new result item as an array.

$data['result'][] = [
    'number' => 'AAA1225',
    'short_description' => 'Cable replacement on workstation 12',
    'state' => 'Closed'
];
Md. Salahuddin
  • 1,062
  • 15
  • 22