sw0o0sh Posted April 25, 2007 Share Posted April 25, 2007 Say I want a variable to equal a part on an array til all the way to the end of the array or from the point i specify to the beginning.. Anybody know how to do this? $_this = $array[5] . (all the way down to array[0]); or $_this = $array[5] . (all the way to the end of the array, end($array); Thanks in advance... Link to comment https://forums.phpfreaks.com/topic/48708-solved-recursive-like-variables-problem/ Share on other sites More sharing options...
Glyde Posted April 25, 2007 Share Posted April 25, 2007 Well, I believe this is planned for PHP 6 using a syntax such as $array[5,], but obviously that's no use to you. This function should do it <?php function subArray($arrayItem, $startIndex, $endIndex=false) { $_string = ""; if ($endIndex === FALSE) $endIndex = count($arrayItem) - 1; for ($i = $startIndex; $i <= $endIndex; $i++) $_string .= $arrayItem[$i]; return $_string; } ?> $_this = subArray($array, 5); Keep in mind this only works on numerically-indexed arrays, not associative arrays. Link to comment https://forums.phpfreaks.com/topic/48708-solved-recursive-like-variables-problem/#findComment-238629 Share on other sites More sharing options...
Barand Posted April 25, 2007 Share Posted April 25, 2007 <?php $ar = range (1,10); $a = array_slice ($ar, 0, 5); $b = array_slice ($ar, 5); echo '<pre> a ', print_r($a, true), '</pre>'; echo '<pre> b ', print_r($b, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/48708-solved-recursive-like-variables-problem/#findComment-238634 Share on other sites More sharing options...
Glyde Posted April 25, 2007 Share Posted April 25, 2007 Hmm, thought he wanted it as a string. Well, Barand has it down pretty well then. Link to comment https://forums.phpfreaks.com/topic/48708-solved-recursive-like-variables-problem/#findComment-238645 Share on other sites More sharing options...
sw0o0sh Posted April 26, 2007 Author Share Posted April 26, 2007 Thanks. Link to comment https://forums.phpfreaks.com/topic/48708-solved-recursive-like-variables-problem/#findComment-238656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.