note 103844 deleted from function.array-push by danbrown

From: Date: Fri, 06 May 2011 13:59:16 +0000
Subject: note 103844 deleted from function.array-push by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179486@lists.php.net to get a copy of this message
Note Submitter: Alex ---- Push item associative into every array item using foreach: $arr = array('a','b','c'); class classsss { private $arr = array(); function __construct() { $this->add('B1',12,4); $this->add('C3',7,1); $this->insert_sum(); $this->print_array($this->arr); } function add($name,$x,$y) { $i = count($this->arr); //$this->arr[]['name'] = $name; - doesnot work $this->arr[$i]['name'] = $name; $this->arr[$i]['X_val'] = $x; $this->arr[$i]['Y_val'] = $y; } function insert_sum() { foreach($this->arr as &$act) //___&___ { $act['SUM'] = $act['X_val']+$act['Y_val']; } } function print_array($a){echo('<br/><pre>');print_r($a);echo('</pre>');} } $c = new classsss(); OUTPUT: Array ( [0] => Array ( [name] => B1 [X_val] => 12 [Y_val] => 4 [SUM] => 16 ) [1] => Array ( [name] => C3 [X_val] => 7 [Y_val] => 1 [SUM] => 8 ) )

« previous php.notes (#179486) next »