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


PHP remove value of specific index in array, manipulate value, then replace value to same array

试图找出如何在数组中提取特定索引的值,操作它,然后将值替换回数组的原始位置

我有一个来自POST表单的数组它上传了一个CSV:

if(isset($_POST['submit'])){
  $csvFile = $_FILES['csv']['tmp_name'];
  $handle = fopen($csvFile,"r");
  $fileop = fgetcsv($handle,1000,",");
}
上传的CSV文件通常有20到100行5列(索引键)。下面是print_r($fileop) while循环中只有2条记录的例子。 HTML显示:

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
)

这里有一些我正在努力完成的例子。

编辑# 1:

$imageLink = stripslashes($fileop[3]);
str_replace("http://www.domain.co/logo.png","/images/logo.png",$imageLink);
编辑# 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
)

大问题:我如何将这些被操纵的值返回到数组?

我已经在这个上面搜索了几个小时了,我现在超负荷了,更困惑了。任何帮助都非常感激!

更新:

我一直在玩下面提供的一些答案,到目前为止还没有多少运气。它确实可以在从CSV文件中检索的"堆叠"数组中更新单个数组,但我无法让它为特定键更新所有数组并保留"堆叠"数组。

我不确定我是否使用了正确的术语"堆叠数组",但基本上,如果我从CSV中循环遍历堆叠数组,我会得到许多单独数组的打印输出,所以当需要插入MySQL DB时,我可以循环遍历数组并将它们作为单独的记录写入。

我尝试了以下操作,它丢失了所有其他数组,只保留了它修改的第一个数组:

while(($fileop = fgetcsv($handle,1000,",")) != false){
    $model = stripslashes($fileop[2]);
    $fileop[2] = '<div id="model" style="clear:both"><strong>'.$model.'</strong></div>';
}

现在,当执行下面的打印时,我没有得到所有的数组:

<pre>
<?php
while(($fileop = fgetcsv($handle,1000,",")) != false){
    print_r($fileop);
}
?>
</pre>

HTML结果(只有一个数组,所有其他"堆叠数组"丢失,不打印):

Array
(
    [0] => Chevy
    [1] => Sedan
    [2] => Malibu
    [3] => <a href="#"><img href="http://www.domain.co/logo.png" alt="Pic" /></a>
    [4] => 8000.00
)

你可以这样做:

<?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);

示例输出:

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
        )
)

您可以简单地引用数组值,就像检索它的值一样,但是在赋值操作符的左侧使用它。例如,您的编辑#1将变成:

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

从你的问题中我了解到你的主要问题是与索引一起删除元素。似乎支持问题可以从答案的其余部分中解决。

表示for,这里有一个数组

$var = Array(
             'index1' => 'one content',
             'index2' => 'another content',   
             'index3' => 'again another content'
            );

要删除数组的索引内容(这里说index2),必须使用

unset($var['index2']);

这是它如何删除索引本身的内容

echo $var;
--------------------------
Array(
        'index1' => 'one content'   
        'index3' => 'again another content'
)

如果我回答了你的问题请告诉我