How To Add A Column To An Existing Multidimensional Php Array And ...
Maybe your like
Published: 7 years ago How to add a column to an existing multidimensional php array and then change value in one of it’s columns
If you want to add column to an existing PHP array you can do in accessing it by reference.
In following example there is an array containing records of months numbers and names. We'd like to add a new column called 'temperature' containing our opinion about it. Then we'd like to change only one record of the array to express our preferences.
The months array can be created like this:
$months = [['nr' => '1', 'name' => 'Jan'],['nr' =>'2', 'name' => 'Feb'],['nr' =>'3', 'name' => 'Mar'],['nr' =>'4', 'name' => 'Apr'],['nr' =>'5', 'name' => 'May'],['nr' =>'6', 'name' => 'Jun'],['nr' =>'7', 'name' => 'Jul'],['nr' =>'8', 'name' => 'Aug'],['nr' =>'9', 'name' => 'Sep'],['nr' =>'10', 'name' => 'Oct'],['nr' =>'11', 'name' => 'Nov'],['nr' =>'12', 'name' => 'Dec'],];
Then let's add one column using reference:
foreach ($months as &$record) {
$record['temperature'] = '33C';
}
And finally let's change only one record:
Tags: Programming By using this site you agree to our Terms & Conditions, Cookie Policy and Privacy Policy. Yes, I agreeforeach ($months as &$record) {
if ($record['name'] === 'Sep') {
$record['temperature'] = '27C';
}
}
Tag » Add Column To Array Php
-
Adding Columns To Existing Php Arrays - Stack Overflow
-
Adding Column To Array Php Code Example - Code Grepper
-
How To Add Columns To An Existing Array - PHP
-
Array_column - Manual - PHP
-
PHP Array_column() Function - W3Schools
-
PHP Add Column To Two Dimentional Array
-
PHP Array Push: How To Add Elements To Array - AppDividend
-
Php. Adding New Column To An Array, But Is Doubling Up The Results
-
Adding Column To Array Php - Learn With IAP - IAcademyPlus
-
Add New Column/element To Collection - Laracasts
-
PHP Array_Column: Learn To Create Another Array From Array Column
-
Adding Multiple Columns To Array - PHP
-
Code For A Tutorial On Adding Columns To WordPress List Tables.
-
PHP Doctrine\DBAL\Schema Table::addColumn Examples