note 97558 modified in function.array-key-exists by danbrown
| From: | danbrown@php.net | Date: | Mon, 26 Apr 2010 14:30:36 +0000 |
| Subject: | note 97558 modified in function.array-key-exists by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168655@lists.php.net to get a copy of this message | ||
This uses array_key_exists.
You have 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 ) )
This can be used to create a table by iterating over the rows that looks like this:
field_wrkvolmin_value field_wrkvolmax_value
216 1000
27 216
when $rowsoriginal contain a fixed but unknown amount of values.
If you want to process this in an 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 ) )
This can be used to iterate over the rows of a table to create a table in the form of:
field_wrkvolmin_value 216 27
field_wrkvolmax_value 1000 216
To do this you can use the following looping and conditional structure, using array_key_exists():
<?php
$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;
}
}
}
?>
--was--
This uses array_key_exists.
You have 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 ) )
This can be used to create a table by iterating over the rows that looks like this:
field_wrkvolmin_value field_wrkvolmax_value
216 1000
27 216
when $rowsoriginal contain a fixed but unknown amount of values.
If you want to process this in an 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 ) )
This can be used to iterate over the rows of a table to create a table in the form of:
field_wrkvolmin_value 216 27
field_wrkvolmax_value 1000 216
To do this you can use the following looping and conditional structure, using 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;
}
}
}
http://php.net/manual/en/function.array-key-exists.php