poe Posted November 13, 2006 Share Posted November 13, 2006 i have this bit of code that someone replied to an earlier post:[code]for ($i=7; $i<count($myarray); $i+=7) { $people[] = array( $myarray[0] => $myarray[$i+0], $myarray[1] => $myarray[$i+1], $myarray[2] => $myarray[$i+2], $myarray[3] => $myarray[$i+3], $myarray[4] => $myarray[$i+4], $myarray[5] => $myarray[$i+5], $myarray[6] => $myarray[$i+6] );[/code]is there a way to build the array dynamiclly without hard coding the array incrimentsie..for ($i=7; $i<count($myarray); $i+=7) { $people[] = array( for ($x=0; $x<7; $x++) { $myarray[$x] => $myarray[$i+$x], } );} Link to comment https://forums.phpfreaks.com/topic/27133-build-dynamic-arrays/ Share on other sites More sharing options...
Barand Posted November 13, 2006 Share Posted November 13, 2006 If I understand correctly,[code]<?php$myarray = range(1,70);$people = array_chunk($myarray, 7);echo '<pre>', print_r($people, true), '</pre>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/27133-build-dynamic-arrays/#findComment-124038 Share on other sites More sharing options...
poe Posted November 13, 2006 Author Share Posted November 13, 2006 ok this is my full code:, just a shrotened version of what i have now.[code]<?php$mystring = "[name][age][pet][mike][22][dog][john][25][cat][kim][26][bird][pam][18][fish]";$mystring = replace("][",",",$mystring);$mystring = replace("[","",$mystring);$mystring = replace("]","",$mystring);$myarray = explode(",",$mystring);for ($i=3; $i<count($myarray); $i+=3) { $people[] = array( $myarray[0] => $myarray[$i+0], $myarray[1] => $myarray[$i+1], $myarray[2] => $myarray[$i+2] );}?>[/code]this is the array it produces:[quote]Array( [0] => Array ( [name] => mike [age] => 22 [pet] => dog ) [1] => Array ( [name] => john [age] => 25 [pet] => cat ) [2] => Array ( [name] => kim [age] => 26 [pet] => bird ) [3] => Array ( [name] => pam [age] => 18 [pet] => fish ))[/quote]what i want to do is:lets say my array has 18 keys i dont want to repeat typing out ** $myarray[0] => $myarray[$i+0] **etc... - 18 times or whateveri was planning on making this into a little function that would split any string into an array the same way. but each string would have different number of keys.i also want to keep my key names & values intact.i was looking for a shortcut that i could go:for($x=1; $x<18; $x++)$myarray[$x] => $myarray[$i+$x];...or somethingwhen i refer back to the array i want to use : $key[2]['name'] Link to comment https://forums.phpfreaks.com/topic/27133-build-dynamic-arrays/#findComment-124154 Share on other sites More sharing options...
Barand Posted November 13, 2006 Share Posted November 13, 2006 try[code]<?phpfunction makeArray ($str, $numkeys) { $res = array(); $str = trim($str, '[]'); // remove outer [ and ] $tmp = array_chunk(explode('][', $str), $numkeys); $k = count($tmp); for ($i=1; $i<$k; $i++) { for ($j=0; $j<$numkeys; $j++) { $res[$i-1][$tmp[0][$j]] = $tmp[$i][$j]; } } return $res;}$mystring = "[name][age][pet][mike][22][dog][john][25][cat][kim][26][bird][pam][18][fish]";$data = makeArray($mystring, 3);echo '<pre>', print_r($data, true), '</pre>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/27133-build-dynamic-arrays/#findComment-124174 Share on other sites More sharing options...
Psycho Posted November 14, 2006 Share Posted November 14, 2006 If you just want to indicate how many "keys" there are you could do this:[code]<?php$keys = 3; //enter the number of keys there are$mystring = "[name][age][pet][mike][22][dog][john][25][cat][kim][26][bird][pam][18][fish]";$mystring = substr($mystring, 1, strlen($mystring)-2);$myarray = explode("][",$mystring);for ($i=1; $i*$keys<count($myarray); $i++) { for ($j=0; $j<$keys; $j++) { $people[$i-1][$myarray[$j]] = $myarray[($i*$keys)+$j]; }}echo "<pre>";print_r($people);echo "</pre>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/27133-build-dynamic-arrays/#findComment-124176 Share on other sites More sharing options...
doni49 Posted November 14, 2006 Share Posted November 14, 2006 Do you mean 18 People? Or 18 keys (name, age, pet being three of the keys)? If 18 people then the same code that you have should do it.And if you have control over this string, can you format it a little differently? Link to comment https://forums.phpfreaks.com/topic/27133-build-dynamic-arrays/#findComment-124208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.