sorting - PHP Table Creation (Multidimensional Arrays) -
i have 4 arrays first beingness table headings there six:
array(6) { [0]=> string(3) "who", [1]=> string(4) "what", [2]=> string(4) "when", [3]=> string(5) "where", [4]=> string(3) "why", [5]=> string(3) "how" }
and 3 other arrays contain coinciding values. want take values of first array create them keys of array value or array
so:
array(6) { ["who"]=> array(0) {}, ["what"]=> array(0) {}, ["when"]=> array(0) {}, ["where"]=> array(0) {}, ["why"]=> array(0) {}, ["how"]=> array(0) {} }
and populate arrays coinciding values much table. illustration of 1 of other arrays be:
array(6) { [0]=> string(3) "red", [1]=> string(4) "blue", [2]=> string(4) "green", [3]=> string(6) "yellow", [4]=> string(3) "black", [5]=> string(3) "white" }
for sake of simplicity going 3 arrays have exact same value 1 above.
in end want resulting array be:
array(6) { ["who"]=> array(3) { [0]=> string(3) "red", [1]=> string(3) "red", [2]=> string(3) "red" }, ["what"]=> array(3) { [0]=> string(4) "blue", [1]=> string(4) "blue", [2]=> string(4) "blue" }, ["when"]=> array(3) { [0]=> string(5) "green", [1]=> string(5) "green", [2]=> string(5) "green" }, ["where"]=> array(3) { [0]=> string(6) "yellow", [1]=> string(6) "yellow", [2]=> string(6) "yellow" }, ["why"]=> array(3) { [0]=> string(5) "black", [1]=> string(5) "black", [2]=> string(5) "black" }, ["how"]=> array(3) { [0]=> string(5) "white", [1]=> string(5) "white", [2]=> string(5) "white" } }
the code working follows:
... //tokens generated above (this in loop) foreach ($tokens $token) { if ($i == 0) { $table[$token] = array(); } else { foreach ($table $col) { $table[$col][$i] = $token; } } } $i = $i + 1;
all generates array(0) {}
it'd appreciated if point out flaw in logic.
edit:
here table like:
_________________________________________ | | | when | | why | how | ----------------------------------------- | reddish | bluish | green| yellow|black|white| ----------------------------------------- | reddish | bluish | green| yellow|black|white| ----------------------------------------- | reddish | bluish | green| yellow|black|white| -----------------------------------------
try array_fill_keys() function
$tokens= array('who', 'what', 'when', 'where', 'why', 'how'); $table = array('red', 'blue', 'green', 'yellow','black', 'white'); $a = array_fill_keys($tokens, $table); print_r($a);
php sorting multidimensional-array
No comments:
Post a Comment