Trying to figure out how to pull values for a specific index in an array, manipulate it, then replace the value back into the array in its original position.
I've got an array from a form POST that uploaded a CSV:
if(isset($_POST['submit'])){
$csvFile = $_FILES['csv']['tmp_name'];
$handle = fopen($csvFile,"r");
$fileop = fgetcsv($handle,1000,",");
}
Uploaded CSV file usually has 20 to 100 rows with 5 columns (indexed keys). Here's an example of only 2 records from a print_r($fileop)
while loop.
HTML Display:
Array
(
[0] => Chevy
[1] => Sedan
[2] => Malibu
[3] => <a href="#"><img href="http://www.domain.co/logo.png" alt="Pic" /></a>
[4] => 8000.00
)
Array
(
[0] => Chevy
[1] => Pickup
[2] => 2500
[3] => <a href="#"><img href="http://www.domain.co/logo.png" alt="Pic" /></a>
[4] => 18000.00
)
Here's some examples of what I'm trying to accomplish.
EDIT #1:
$imageLink = stripslashes($fileop[3]);
str_replace("http://www.domain.co/logo.png","/images/logo.png",$imageLink);
EDIT #2:
$model = stripslashes($fileop[2]);
$preHTML = <div id="model" style="clear:both"><strong>;
$postHTML = </strong></div>;
$fullHTML = $preHTML . $model . $postHTML;
NEW HTML Display:
Array
(
[0] => Chevy
[1] => Sedan
[2] => <div id="model" style="clear:both"><strong>Malibu</strong></div>
[3] => <a href="#"><img href="/images/logo.png" alt="Pic" /></a>
[4] => 8000.00
)
Array
(
[0] => Chevy
[1] => Pickup
[2] => <div id="model" style="clear:both"><strong>2500</strong></div>
[3] => <a href="#"><img href="/images/logo.png" alt="Pic" /></a>
[4] => 18000.00
)
BIG QUESTION: How do I return these manipulated values back to the array?
I've been searching for a few hours on this and I'm overloaded now and more confused. Any help is much appreciated!
UPDATE:
I've been playing with some answers provided below and haven't had much luck so far. It does work as far as updating a single array in the "stacked" arrays that are retrieved from the CSV file but I can't get it to update all the arrays for a specific key and retain the "stacked" arrays.
I'm not sure I'm using the right terminology for "stacked arrays" but basically if I loop thru the stacked arrays from the CSV I get many print outs of individual arrays so when it comes time to INSERT into MySQL DB, I can loop thru the arrays and write them in as individual records.
I tried the following and it lost all the other arrays and only kept the 1st one that it modified:
while(($fileop = fgetcsv($handle,1000,",")) != false){
$model = stripslashes($fileop[2]);
$fileop[2] = '<div id="model" style="clear:both"><strong>'.$model.'</strong></div>';
}
Now when doing the following print I don't get all the arrays:
<pre>
<?php
while(($fileop = fgetcsv($handle,1000,",")) != false){
print_r($fileop);
}
?>
</pre>
HTML Result (Only 1 array, all other "stacked arrays" are lost and don't print):
Array
(
[0] => Chevy
[1] => Sedan
[2] => Malibu
[3] => <a href="#"><img href="http://www.domain.co/logo.png" alt="Pic" /></a>
[4] => 8000.00
)