hi

just got another problem here that i can not seem to fiqure out

i have a for loop that returns xxx(1,3, 5 or whatever) amount of elemtents

after these elements are returned in the for loop, how can i turn all these elements combined into a single varible to be called later in my script

heres a code snippet

Parse stock quotes info from post array

$post = parse_array($post_array, $open_tag="<td", $close_tag="</td>");
for($xx=0; $xx<count($post); $xx++)
{ $body[$xx] = return_between($post[$xx], "<!-- data -->", "<!-- / data -->", $type=EXCL); }

Echo data to validate the download and parse

for($xx=0; $xx<count($body); $xx++)
echo substr($body[$xx], 34, -5);

the echo substr($body[$xx], 34, -5); , could return one element or it could return 10 or 20
ie , if it returns one time there only 1 element, if it returns 6 times there six elements

how can i then make all these elements become one varibable , like

$stockdata=$body[$xx];

that does not seem to work as listed above, placed directly under the above code snippet, should there be numbers added to $body element, like $body[1][$xx] or similar, i just need to get all returned elements in the loop returned together into a single varible i can use later in my script, after the for loop has run

thanks again for looking, any help with this whould be very helpfull, this one has me stumpped again, thanks in advance

    I'm not familiar with your functions (parse_array & return_between) and I can't seem to find info on them in Google, so I'm just going to suggest a different method altogether! Hopefully, my lack of understanding won't be too off-base.

    If you want to combine all the array values into a single variable, why not just step through the array and build a string? I use this method in a lot of pages and load times are almost instantaneous:

    $value_string = "";
    foreach($post as $key => $value)
      {
      $value = substr($value, 34, -5);
      $value_string .= $value;
      }
    echo $value_string;
    
      Watcher32;10889997 wrote:

      i just need to get all returned elements in the loop returned together into a single varible i can use later in my script

      Have you tried using implode?

        i am a slight newbie to coding, sorry for the confusing post

        sorry i should have mentioned the script i using is a curl based script that scrapes stock quotes and returns them so i can then use them on my site

        the problem i was having if say 4 sets of quotes where returned, between the data tags, i whould then some how need to merge them all together so i can the either pass them off to another page or post them of to another site

        the for loop runs returning whatever is there, could be 1 or more quotes, i just some how need to combine them together into a single varibale so i can do futher processcess with it

        i will look into your solutions more to try to get all the returned resluts form the for loop merged together

        heres the custom fuctions you mentioned, the script i am using is based around retiving content via curl, these fuctions are supplied for the book/website, webots, spiders and screen scrapers.

        /***********************************************************************

        $value = return_between($string, $start, $end, $type)

        DESCRIPTION:

        Returns a substring of $string delineated by $start and $end

        The parse is not case sensitive, but the case of the parsed

        string is not effected.

        INPUT:

        $string Input string to parse

        $start Defines the beginning of the sub string

        $end Defines the end of the sub string

        $type INCL: include delineators in parsed string

        EXCL: exclude delineators in parsed string

        ***********************************************************************/
        function return_between($string, $start, $stop, $type)
        {
        $temp = split_string($string, $start, AFTER, $type);
        return split_string($temp, $stop, BEFORE, $type);
        }

        /***********************************************************************

        $array = parse_array($string, $open_tag, $close_tag)

        DESCRIPTION:

        Returns an array of strings that exists repeatedly in $string.

        This function is usful for returning an array that contains

        links, images, tables or any other data that appears more than

        once.

        INPUT:

        $string String that contains the tags

        $open_tag Name of the open tag (i.e. "<a>")

        $close_tag Name of the closing tag (i.e. "</title>")

        **********************************************************************/
        function parse_array($string, $beg_tag, $close_tag)
        {
        preg_match_all("($beg_tag(.
        )$close_tag)siU", $string, $matching_data);
        return $matching_data[0];
        }

        /***********************************************************************

          # Echo data to validate the download and parse
          for($xx=0; $xx<count($body); $xx++) 
          $stockdata[] = substr($body[$xx], 34, -5);
          

          $stockdata is now an array of elements which you can use foreach to loop through as ixalmida showed you or use the implode() function now that the excess data has been stripped out.

            Combining b321 and ixalmida's samples:

            foreach($body as $value)
                $stockdata[] = substr($value, 34, -5);
            

            There's also

            $stock_data = array_map(create_function('$value', 'return substr($value,34,-5);'), $body);
            

            Or the substring call can be put in its own function....

            function trim_cruft($value)
            {
                return substr($value, 35, -5);
            }
            
            // Then making a new array of the trimmed down bodies is done with:
            $stockdata = array_map('trim_cruft', $body);
            
              Write a Reply...