1

Ok so I got a txt file that's a data feed, each line is a set of [tab] separated elements for a single item like:

name value1 value2 url 
name value1 value2 url

and so on.. I'm reading the file and making each line a single element in an array, and each element from the lines a single element in next sub-array.(fetching only 10 of them for testing, 1st line is headers that's why i'm starting on the 1st element).

$feed = file_get_contents("file.txt");
$lines = explode("\n", $feed);
$prods = array();

for ($i=1; $i < 11; $i++) {

    array_push($prods, explode("\t", $lines[$i])); 

}

so at this point $prods array looks like:

Array
(
    [0] => Array
        (
            [0] => name1
            [1] => somedata1
            [2] => somedata2
            [3] => http://someurl.com

        )
    [2] => Array 
        (
            [0] => name2
            [1] => somedata1
            [2] => somedata2
            [3] => http://someurl.com

        )
)

then comes the foreach loop to fetch the data from the URL and adds it as 4th element to those subarrays:

foreach ($prods as $item) {


    $url = $item[3];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    preg_match('@some regex here@is', $result, $match);

    if (sizeof($match) > 0) {

        $item[] = $match[1];




    } else {

        $item[] = 'empty';


    }   

    echo '<pre>';   //
    print_r($item); //  this gives me the nice output of 4 elements arrays with the fetched data where I want it to be
    echo '</pre>';  //

}   

and everything is ok here but if I print the $prods array after the loop outside it, it gives me back the same untouched array as before the foreach loop.

The question is, how do i get that changes to array in loop saved, so I can access the modified array outside of the loop?

  • You need to loop it by `&` reference. `foreach ($prods as &$item)` (or by index). See examples at https://stackoverflow.com/questions/10121483/php-modify-current-object-in-foreach-loop – Michael Berkowski May 31 '17 at 18:53
  • 1
    You might consider using `fgetcsv()` to handle the file read and split on `\t` all in one operation. http://php.net/manual/en/function.fgetcsv.php, or at least using `file()` instead of `file_get_contents()`, which will return an array of lines, avoiding the `explode()` on `\n`. – Michael Berkowski May 31 '17 at 18:54
  • Thanks a lot Michael, `&` did the trick :) any idea where I can read something about it so I can understand how does it work exactly? –  May 31 '17 at 18:57
  • If you want technical details: [How does foreach actually work](https://stackoverflow.com/questions/10057671/how-does-php-foreach-actually-work). If you are looking for official docs, it's just after the first **Note:** on the `foreach` manual page http://php.net/manual/en/control-structures.foreach.php – Michael Berkowski May 31 '17 at 19:01
  • thanks again :) I can't upvote your comments, is it because the thread is marked as a duplicate? or do I need 15 rep to upvote comments too? –  May 31 '17 at 19:09
  • You need 15 rep to upvote comments (https://stackoverflow.com/help/privileges) votes on comments don't affect scores anyway. – Michael Berkowski May 31 '17 at 19:52

0 Answers0