0

Consider this array of objects in PHP:

 array:2 [
      0 => array:4 [
        "Row_Id" => 256
        "Start_Date" => "2020-05-16"
        "account_code" => ""
        "caller_number" => "452"
        ]
    
      1 => array:4 [
        "Row_Id" => 257
        "Start_Date" => "2020-05-16"
        "account_code" => ""
        "caller_number" => "42"
        ]

      2 => array:4 [
        "Row_Id" => 258
        "Start_Date" => "2020-05-16"
        "account_code" => ""
        "caller_number" => "428"
        ]
    ]

I want to add "callee_number:100" in every array so my output should look like these:

     array:2 [
          0 => array:5 [
            "Row_Id" => 256
            "Start_Date" => "2020-05-16"
            "account_code" => ""
            "caller_number" => "452"
            "callee_number" => "100"
            ]
        
          1 => array:5 [
            "Row_Id" => 257
            "Start_Date" => "2020-05-16"
            "account_code" => ""
            "caller_number" => "42"
            "callee_number" => "100"

            ]

          2 => array:5 [
            "Row_Id" => 258
            "Start_Date" => "2020-05-16"
            "account_code" => ""
            "caller_number" => "428"
            "callee_number" => "100"
            ]
        ]

I have taken the above input array in $get variable. Now I am calling array_push to append callee_number to every array:

  array_push($get,[
   'callee_number':'100'
    ]);

Also tried using array_merge but callee_number is not getting appended. How can I achieve that ?

2 Answers 2

4

Given the following array:

$array = [
    [
        "Row_Id" => 256,
        "Start_Date" => "2020-05-16",
        "account_code" => "",
        "caller_number" => "452",
    ],
    [
        "Row_Id" => 257,
        "Start_Date" => "2020-05-16",
        "account_code" => "",
        "caller_number" => "42",
    ],
    [
        "Row_Id" => 258,
        "Start_Date" => "2020-05-16",
        "account_code" => "",
        "caller_number" => "428",
    ],
];

Native PHP

$array = array_map(function ($item) { return $item + ['callee_number' => 100]; }, $array);

Using collections

$array = collect($array)->map(function ($item) { return $item + ['callee_number' => 100]; })->toArray();

Using PHP 7.4 shorthands

$array = array_map(fn($item) => $item + ['callee_number' => 100], $array);
// Or
$array = collect($array)->map(fn($item) => $item + ['callee_number' => 100])->toArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted. You're missing the assignment parts of the array_map versions, though (it doesn't modify the array in place).
0

To add or modify an element in each sub-array you would do this:


foreach ($get as &$g) {
  $g["callee_number"] = 100;
}

Or this:

for ($c = 0; $c < count($get); $c++) {
$get[$c]["callee_number"] = 100;
}

4 Comments

not sure what you're saying with the text but the example is right.
In OP's second example they're showing the impossible situation of having two "callee_number" elements in each object.
@joshstrike - i don't have two things called "callee_number" in the same array. Read the question again.
Ah. Apologies. My eyesight is not so good. I saw "caller_number" and "callee_number" as the same. The code in my answer will work. I'll remove the other part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.