John Davidson

laravel - how to add an array inside another in PHP?

0 comments
Message:


I am using laravel.
How can I create a new array and have the properties of another.
Example the carriage array can have 3 rows that are of the "info" properties, but the brand is added.


    $info = [
['id' => 1, 'color'=> 'blue'],
['id' => 2, 'color'=> 'red'],
['id' => 3, 'color'=> 'yellow'],
];

$car = [
['id' => 'id_info', 'brand'=> 'toyota', 'color' => 'color_info']
];


My Answer:
You can add an array inside another array in PHP using the following syntax:

php
$parentArray = [
'key1' => 'value1',
'key2' => 'value2',
'childArray' => [
'childKey1' => 'childValue1',
'childKey2' => 'childValue2'
]
];


In Laravel, you can create a new array and add it as a property of another array like this:

php
$parentArray = [
'key1' => 'value1',
'key2' => 'value2',
'childArray' => [
'childKey1' => 'childValue1',
'childKey2' => 'childValue2'
]
];

// Adding a new array as a property of the childArray
$newChildArray = [
'newChildKey1' => 'newChildValue1',
'newChildKey2' => 'newChildValue2'
];

$parentArray['childArray']['newChildArray'] = $newChildArray;

// Output the parentArray
dd($parentArray);


This will result in a parent array with a child array that contains another array as a property.

Rate this post

3 of 5 based on 9487 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us