Adding Object into Array PHP

Adding Object into Array PHP

Adding Object into Array PHP, sometimes you might want to do that. For example , you have a lists of  item assign in the array call stock. inside the Stock array you might have different type of products . When there is a new product deliver to be stock , you might want your program to be able to add in this new type of product.

In order to do that, you can either assign the new Object to an Empty Array or  to an Array dedicated  with a specific name.

Check out the Code below

 


<?php


$stock = array("hammer","saw","screwdriver","nail");
$stock['motorised']= "chainsaw";

foreach ($stock as $number => $value){
	
	echo $number.' = '.$value.'<BR>';
}
	
	var_dump($stock);



	
?> 

Output after Adding Array

The Output of the code shown above , should resemble what is shown on the picture below.

Adding Object into Array PHP
 

Where to find PHP Cheat Sheet

We are humans, sometimes we can’t remember all the libraries , functions ,classes etc.The Important things is that we understand the core concept on how things works, how to apply it in the right situation. That will be good enough.
To Refer the PHP Cheat Sheet just in case you forgot something, click here
Check out Array in PHP here

Leave a Comment

14 − 5 =