taith Posted June 1, 2007 Share Posted June 1, 2007 anybody know an efficient way of appending to an array? $array[]='test'; $array[]='test'; $array[]='test'; $array[]='test6'; $array[]='test'; say... i want to move test6 to the #2 position... and increase the other keys under it... by 1...? i know i could make a function to do this... just didnt know if php had a built in function like that :-) Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/ Share on other sites More sharing options...
chigley Posted June 1, 2007 Share Posted June 1, 2007 All credits to frost110 for this, I remember reading this post the other day: <?php function insertArray($array, $addDataArr, $pos) { $newKey = -1; foreach ($array as $key => $val) { if ($key == $pos) { foreach ($addDataArr as $val) { $newArray[$key++] = $val; } $newKey = $key; }else { if (isset($newKey) && $newKey != -1) { $newArray[$newKey++] = $val; }else { $newArray[$key] = $val; } } } } ?> Probably not the best way but I think it works, I also think there is something like www.php.net/array_merge But yea, I think the above will work, untested. Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266243 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 All credits to frost110 for this, I remember reading this post the other day: <?php function insertArray($array, $addDataArr, $pos) { $newKey = -1; foreach ($array as $key => $val) { if ($key == $pos) { foreach ($addDataArr as $val) { $newArray[$key++] = $val; } $newKey = $key; }else { if (isset($newKey) && $newKey != -1) { $newArray[$newKey++] = $val; }else { $newArray[$key] = $val; } } } } ?> Probably not the best way but I think it works, I also think there is something like www.php.net/array_merge But yea, I think the above will work, untested. Go me. Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266246 Share on other sites More sharing options...
taith Posted June 1, 2007 Author Share Posted June 1, 2007 aah... so... no prebuilt way... i made this one hwile i was waiting... lol $array[]='1'; $array[]='2'; $array[]='3'; $array[]='4'; $array[]='5'; $array[]='6'; $array[]='7'; function array_resort($array, $value, $key){ $out=array(); for($i=0; $i<count($array); $i++){ if($i<$key) $out[$i]=$array[$i]; elseif($i==$key&&!$done){ $done=true; $out[$key]=$value; $i--; }else $out[$i+1]=$array[$i]; } return $out; } $array=array_resort($array,'test','2'); print_r($array); Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266247 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 well just for fun I updated my function to takin a single string, or a whole array. Fun stuff. <?php function array_insert($array, $add_data, $pos) { $new_key = -1; foreach ($array as $key => $val) { if ($key == $pos) { if (is_array($add_data)) { foreach ($add_data as $val2) { $new_array[$key++] = $val2; } }else { $new_array[$key++] = $add_data; } $new_key = $key; }else { $index = ($new_key!=-1)?$new_key++:$key; $new_array[$index] = $val; } } } ?> Remember this is still un-tested. Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266272 Share on other sites More sharing options...
chigley Posted June 1, 2007 Share Posted June 1, 2007 I'm glad I'm not the only one who finds challenges and random coding fun Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266273 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Nope, what would be cool is to create this for the core of php as a .dll =) Now that would be the fun part. Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266279 Share on other sites More sharing options...
chigley Posted June 1, 2007 Share Posted June 1, 2007 On that note, in the PHP source where abouts can you find the function sources? Would be interesting to have a nosy around.. Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266280 Share on other sites More sharing options...
taith Posted June 1, 2007 Author Share Posted June 1, 2007 http://www.php.net/downloads.php Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266284 Share on other sites More sharing options...
chigley Posted June 1, 2007 Share Posted June 1, 2007 Yes but where in the source are the function source codes? Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266286 Share on other sites More sharing options...
taith Posted June 1, 2007 Author Share Posted June 1, 2007 sorry... havent looked at the source in ages... dont know offhand :-) Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266289 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 If you want to know, goto Core Hacking and ask, I am sure they know exactly where to go. Link to comment https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/#findComment-266291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.