Getting array information out of a function on into another array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tek9_AK

    Getting array information out of a function on into another array



    I need to find a way to transfer all the values of an array inside a
    function out of the fuction into another array. IE

    function splice($filenam e){
    if(file_exists( $filename)){
    $value=array("0 "); // clear the array.
    $rows=file($fil ename); // break text file into a rows array
    for($num=0;$num < count($rows); ++$num){
    $column=split(" ;",$rows[$num]);
    $value[$num]=$column;}}
    return $value;
    }

    $array1 = splice(file.txt )


    Example file data would be

    data1;an1;bc1
    data2;an2;bc2
    data3;an3;bc3

    How do I go about getting the function to pass all it's data to the variable
    $array1?
    Thanks in advance.


  • Reply Via Newsgroup

    #2
    Re: Getting array information out of a function on into another array


    // Note the & in &$value when the function is declared below
    // It means pass by reference - Later, we call splice function
    // with $array1 which has its value passed to $value. Splice
    // changes $value and the & returns the changes to your call
    function splice($filenam e, &$value){
    if(file_exists( $filename)){
    // Your declaration below does *not* clear the array - use
    // unset if you need to - see what I have done just before
    // the call to splice near the bottom of this post
    // $value=array("0 "); // clear the array.
    $rows=file($fil ename); // break text file into a rows array
    for($num=0;$num < count($rows); ++$num){
    $column=split(" ;",$rows[$num]);
    $value[$num]=$column;}}
    return $value;
    }

    // Splice file.txt - pass the function $array1
    // $array1 should be empty and have no previous value
    // hence why we make sure using an 'unset()'
    unset($array1); // Clear $array1 from having *any* value
    splice(file.txt , $array1)



    Note your method of reading $rows could perhaps be done alot better...
    Have you entertained the idea of using explode(), implode() or even
    fgetcsv() ????

    The latter is bound to be of help and be more friendly on your systems
    resouces... see http://www.php.net/fgetcsv for more info

    laters
    randelld

    Comment

    • Tek9_AK

      #3
      Re: Getting array information out of a function on into another array

      Thanks appreciate the help,

      I'm fairly new to php so I will definatly look at the fgetcsv which would
      probably easier to use than a text file.

      -Patrick-

      "Reply Via Newsgroup" <reply-to-newsgroup@pleas e.com> wrote in message
      news:Z46hc.1751 86$Pk3.135529@p d7tw1no...[color=blue]
      >
      > // Note the & in &$value when the function is declared below
      > // It means pass by reference - Later, we call splice function
      > // with $array1 which has its value passed to $value. Splice
      > // changes $value and the & returns the changes to your call
      > function splice($filenam e, &$value){
      > if(file_exists( $filename)){
      > // Your declaration below does *not* clear the array - use
      > // unset if you need to - see what I have done just before
      > // the call to splice near the bottom of this post
      > // $value=array("0 "); // clear the array.
      > $rows=file($fil ename); // break text file into a rows array
      > for($num=0;$num < count($rows); ++$num){
      > $column=split(" ;",$rows[$num]);
      > $value[$num]=$column;}}
      > return $value;
      > }
      >
      > // Splice file.txt - pass the function $array1
      > // $array1 should be empty and have no previous value
      > // hence why we make sure using an 'unset()'
      > unset($array1); // Clear $array1 from having *any* value
      > splice(file.txt , $array1)
      >
      >
      >
      > Note your method of reading $rows could perhaps be done alot better...
      > Have you entertained the idea of using explode(), implode() or even
      > fgetcsv() ????
      >
      > The latter is bound to be of help and be more friendly on your systems
      > resouces... see http://www.php.net/fgetcsv for more info
      >
      > laters
      > randelld
      >[/color]


      Comment

      Working...