note 97558 deleted from function.array-key-exists by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 20:43:01 +0000 |
| Subject: | note 97558 deleted from function.array-key-exists by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218764@lists.php.net to get a copy of this message | ||
Note Submitter: moandsimon at btinternet dot com
----
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;
}
}
}
?>