doumaoao0182 2011-10-30 01:04
浏览 123
已采纳

PHP删除数组中特定索引的值,操作值,然后将值替换为相同的数组

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
)
  • 写回答

4条回答 默认 最新

  • drrkgbm6851 2011-10-30 04:56
    关注

    the following should work for you:

    <?php
    $arr = array();
    while(($fileop = fgetcsv($handle,1000,",")) != false){
        $arr[] = $fileop;
    }
    
    
    foreach ( $arr as $k => $v ) {
        $model = stripslashes($v[2]);
        $preHTML = '<div id="model" style="clear:both"><strong>';
        $postHTML = '</strong></div>';
        $fullHTML = $preHTML . $model . $postHTML;
        $arr[$k][2] = $fullHTML;
    
        $imageLink = stripslashes($v[3]);
        $arr[$k][3] = str_replace("http://www.domain.co/logo.png","/images/logo.png",$imageLink);
    }
    
    print_r($arr);
    

    A sample output:

    Array
    (
        [0] => 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
            )
    
        [1] => 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
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!