note 97544 added to function.array-push
| From: | moandsimon at btinternet dot com | Date: | Sun, 25 Apr 2010 13:46:45 +0000 |
| Subject: | note 97544 added to function.array-push | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168618@lists.php.net to get a copy of this message | ||
If you want to convert a multidimensional array of the form:
$rowsoriginal[] = array('field_wrkvolmin_value' => 216,
'field_wrkvolmax_value' => 1000);
$rowsoriginal[] = array('field_wrkvolmin_value' => 27,
'field_wrkvolmax_value' => 216);
Using print_r this will look like:
Array ( [0] => Array ( [field_wrkvolmin_value] => 216 [field_wrkvolmax_value] => 1000 ) [1]
=> Array ( [field_wrkvolmin_value] => 27 [field_wrkvolmax_value] => 216 ) )
when $rowsoriginal contain a fixed but unknown amount of values which you want to process in a
automatic way without knowing the keys etc, into a multidimensional array of the form:
$rowstemp = array('field_wrkvolmin_value' => array(216, 27),
'field_wrkvolmax_value' => array(1000, 216));
Using print_r this will look like:
Array ( [field_wrkvolmin_value] => Array ( [0] => 216 [1] => 27 ) [field_wrkvolmax_value]
=> Array ( [0] => 1000 [1] => 216 ) )
you can use the following looping and conditional structure, using array_push() and
array_key_exists():
$rowstemp = array();
foreach ($rowsoriginal as $row) {
foreach ($row as $key => $value) {
if (array_key_exists($key, $rowstemp)) {
$rowstemp[$key][] = $value;
}
else {
$valuestemp = array($value);
$rowstemp[$key] = $valuestemp;
}
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 86.134.220.97
----
Manual Page -- http://www.php.net/manual/en/function.array-push.php
Edit -- https://master.php.net/note/edit/97544
Del: integrated -- https://master.php.net/note/delete/97544/integrated
Del: useless -- https://master.php.net/note/delete/97544/useless
Del: bad code -- https://master.php.net/note/delete/97544/bad+code
Del: spam -- https://master.php.net/note/delete/97544/spam
Del: non-english -- https://master.php.net/note/delete/97544/non-english
Del: in docs -- https://master.php.net/note/delete/97544/in+docs
Del: other reasons-- https://master.php.net/note/delete/97544
Reject -- https://master.php.net/note/reject/97544
Search -- https://master.php.net/manage/user-notes.php