Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP remove value of specific index in array, manipulate value, then replace value to same array
    primarykey
    data
    text
    <p>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.</p> <p>I've got an array from a form POST that uploaded a CSV:</p> <pre><code>if(isset($_POST['submit'])){ $csvFile = $_FILES['csv']['tmp_name']; $handle = fopen($csvFile,"r"); $fileop = fgetcsv($handle,1000,","); } </code></pre> <p>Uploaded CSV file usually has 20 to 100 rows with 5 columns (indexed keys). Here's an example of only 2 records from a <code>print_r($fileop)</code> while loop.</p> <p>HTML Display:</p> <pre><code>Array ( [0] =&gt; Chevy [1] =&gt; Sedan [2] =&gt; Malibu [3] =&gt; &lt;a href="#"&gt;&lt;img href="http://www.domain.co/logo.png" alt="Pic" /&gt;&lt;/a&gt; [4] =&gt; 8000.00 ) Array ( [0] =&gt; Chevy [1] =&gt; Pickup [2] =&gt; 2500 [3] =&gt; &lt;a href="#"&gt;&lt;img href="http://www.domain.co/logo.png" alt="Pic" /&gt;&lt;/a&gt; [4] =&gt; 18000.00 ) </code></pre> <p>Here's some examples of what I'm trying to accomplish.</p> <p>EDIT #1:</p> <pre><code>$imageLink = stripslashes($fileop[3]); str_replace("http://www.domain.co/logo.png","/images/logo.png",$imageLink); </code></pre> <p>EDIT #2:</p> <pre><code>$model = stripslashes($fileop[2]); $preHTML = &lt;div id="model" style="clear:both"&gt;&lt;strong&gt;; $postHTML = &lt;/strong&gt;&lt;/div&gt;; $fullHTML = $preHTML . $model . $postHTML; </code></pre> <p>NEW HTML Display:</p> <pre><code>Array ( [0] =&gt; Chevy [1] =&gt; Sedan [2] =&gt; &lt;div id="model" style="clear:both"&gt;&lt;strong&gt;Malibu&lt;/strong&gt;&lt;/div&gt; [3] =&gt; &lt;a href="#"&gt;&lt;img href="/images/logo.png" alt="Pic" /&gt;&lt;/a&gt; [4] =&gt; 8000.00 ) Array ( [0] =&gt; Chevy [1] =&gt; Pickup [2] =&gt; &lt;div id="model" style="clear:both"&gt;&lt;strong&gt;2500&lt;/strong&gt;&lt;/div&gt; [3] =&gt; &lt;a href="#"&gt;&lt;img href="/images/logo.png" alt="Pic" /&gt;&lt;/a&gt; [4] =&gt; 18000.00 ) </code></pre> <p>BIG QUESTION: How do I return these manipulated values back to the array?</p> <p>I've been searching for a few hours on this and I'm overloaded now and more confused. Any help is much appreciated!</p> <p>UPDATE:</p> <p>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.</p> <p>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.</p> <p>I tried the following and it lost all the other arrays and only kept the 1st one that it modified:</p> <pre><code>while(($fileop = fgetcsv($handle,1000,",")) != false){ $model = stripslashes($fileop[2]); $fileop[2] = '&lt;div id="model" style="clear:both"&gt;&lt;strong&gt;'.$model.'&lt;/strong&gt;&lt;/div&gt;'; } </code></pre> <p>Now when doing the following print I don't get all the arrays:</p> <pre><code>&lt;pre&gt; &lt;?php while(($fileop = fgetcsv($handle,1000,",")) != false){ print_r($fileop); } ?&gt; &lt;/pre&gt; </code></pre> <p>HTML Result (Only 1 array, all other "stacked arrays" are lost and don't print):</p> <pre><code>Array ( [0] =&gt; Chevy [1] =&gt; Sedan [2] =&gt; Malibu [3] =&gt; &lt;a href="#"&gt;&lt;img href="http://www.domain.co/logo.png" alt="Pic" /&gt;&lt;/a&gt; [4] =&gt; 8000.00 ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload