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?