note 103844 added to function.array-push
| From: | Alex at osu1 dot php dot net | Date: | Fri, 06 May 2011 11:17:24 +0000 |
| Subject: | note 103844 added to function.array-push | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179485@lists.php.net to get a copy of this message | ||
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
)
)
----
Server IP: 69.147.83.197
Probable Submitter: 188.36.13.134
----
Manual Page -- http://www.php.net/manual/en/function.array-push.php
Edit -- https://master.php.net/note/edit/103844
Del: integrated -- https://master.php.net/note/delete/103844/integrated
Del: useless -- https://master.php.net/note/delete/103844/useless
Del: bad code -- https://master.php.net/note/delete/103844/bad+code
Del: spam -- https://master.php.net/note/delete/103844/spam
Del: non-english -- https://master.php.net/note/delete/103844/non-english
Del: in docs -- https://master.php.net/note/delete/103844/in+docs
Del: other reasons-- https://master.php.net/note/delete/103844
Reject -- https://master.php.net/note/reject/103844
Search -- https://master.php.net/manage/user-notes.php