0

I have a multidimensional array $arrResult1 imported from CSV file, I want to replace the multiple values in that array with different values. I have tried str_replace and it works for single replacement.

<?php
$File = 'charge1.csv';

$arrResult = array();
$handle = fopen($File,"r");
if(empty($handle) == false){
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){
    $arrResult[] = $data;
}
fclose($handle);
}


$file = 'ActualCharge.csv';
$arrResult1 = array();
 $handle = fopen($file,"r");
 if(empty($handle) == false){
while(($values = fgetcsv($handle, 1000, ",")) !== FALSE){
    $arrResult1[] = $values;
}
fclose($handle);
 } 
 $newArray = array();    
 foreach($arrResult1 as $inner_array) {
 $newArray[] = str_replace("$11.16","$14.00", $inner_array);
}
var_dump($newArray[6]);

But when I try to replace more values like this

$newArray[] = str_replace(array("$11.16","$14.00"),array("$12.16","$15.00"), $inner_array);

It does not work. I have total 16 values that need to be replaced. Can you please help and let me know what I am doing wrong here or if there is any other way to solve this. TIA here are the links to both csv files https://drive.google.com/open?id=15JXhljASiDaZAyF0I6vC5_vfvFWH5Ylo https://drive.google.com/open?id=1XzbzE39sCVVi4Ox1uj36g0smZJ4X4kCv

  • Why don't you just go through the values one by one and then push the result in the result array? – Geshode Oct 01 '18 at 05:37
  • You seem to be using [`str_replace`](http://www.php.net/str_replace) wrong. Try replacing your line with `str_replace(array("$11.16", "$12.16"), array("$14.00", "$15.00"), $inner_array)`. Otherwise, just please show the value of `$inner_array` and what you want as a result. – Jeto Oct 01 '18 at 05:37
  • @Jeto $arrResult1 is a array imported from csv file and have multiple rows. all I want to is update some of the prices. some of the values are repeated and I have triedstr_replace (array("$11.16", "$12.16"), array("$14.00", "$15.00"), $inner_array). it does not work too – gaurav chhabra Oct 01 '18 at 05:48
  • Again, take one row where it doesn't work and show us what it contains and what you want to obtain, otherwise we can't help. – Jeto Oct 01 '18 at 05:49
  • @Jeto in this case function is only working to replace 1 string, when I try two or three it does not change anything. – gaurav chhabra Oct 01 '18 at 05:52
  • @jeto if you want I can attach CSV file – gaurav chhabra Oct 01 '18 at 05:53
  • Just `var_dump($inner_array)` on a line where it fails and edit it in your post. – Jeto Oct 01 '18 at 05:56
  • @gauravchhabra, can you please add complete code so that SO member can try as you are trying, its bit difficult to work with a fragment of code. e.q. question: https://stackoverflow.com/q/52424772/2987755 – dkb Oct 01 '18 at 05:56
  • @Jeto i have shared the the csv file. it does not give any error. just does not change anything – gaurav chhabra Oct 01 '18 at 06:05
  • @dkb i have put all the code and link to CSV files – gaurav chhabra Oct 01 '18 at 06:07
  • Why are you trying to blindly replace the price, if `$inner_array` is the row from the file then you should do `$inner_array[x]` where x is the index of the array item you wish to replace. – ArtisticPhoenix Oct 01 '18 at 06:09
  • @ArtisticPhoenix some of the prices are repeated 5-6 times in file. so that function is replacing the specific value everywhere. i am new to programming so could not figure out how to change the prices for all 16 items with a single function. Thanks for your help – gaurav chhabra Oct 01 '18 at 06:43

0 Answers0