Jump to content

[SOLVED] Recursive - like variables problem


sw0o0sh

Recommended Posts

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

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.

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.