Jump to content

build dynamic arrays


poe

Recommended Posts

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 incriments

ie..

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

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 whatever

i 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 something


when 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

try
[code]
<?php
function 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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.