2011-10-30 27 views
1

試圖找出如何爲數組中的特定索引提取值,對其進行處理,然後將值替換回其數組中原來的位置。PHP刪除數組中特定索引的值,操縱值,然後將值替換爲相同的數組

我有從表單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; 

新的HTML顯示:

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 
) 

大問題:我如何返回這些操縱值回陣列?

我一直在尋找這個幾個小時,我現在超載,更困惑。任何幫助深表感謝!

UPDATE:

我一直在玩下面提供一些答案,並沒有多少運氣至今。它可以在從CSV文件中檢索到的「堆疊」數組中更新單個數組,但我無法獲取它來更新所有數組的特定鍵並保留「堆疊」數組。

我不知道我使用正確的術語「堆疊陣列」,但基本上如果我通過從CSV堆疊數組循環通過我得到許多單個數組的打印輸出,所以當它來到INSERT到MySQL DB,我可以通過數組循環並將它們寫入單獨的記錄中。

我試過以下,它失去了所有的其他陣列,只保留了第一個1,它修改:做以下打印我沒有得到所有的陣列時

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結果(僅1個陣列,所有其它 「堆疊陣列」 丟失,並且不打印):

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

回答

1

以下應爲你工作:

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

) 
+0

輝煌,它的作品!非常感謝。現在我一直困擾着這個日子。 – SirOracle

0
$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); 
+0

嗨斯蒂芬妮,感謝您的答覆。我一直在玩你的例子,迄今爲止還沒有多少運氣。它可以在從CSV文件中檢索到的「堆疊」數組中更新單個數組,但我無法獲取它來更新所有數組的特定鍵並保留「堆疊」數組。 – SirOracle

0

您可以簡單地引用數組值的方式與您在檢索其值時使用的方法相同,但可以在賦值運算符的左側使用它。例如,您的編輯#1將成爲:

$imageLink = stripslashes($fileop[3]); 
$fileop[3] = str_replace("http://www.domain.co/logo.png","/images/logo.png",$imageLink); 
0

從你的問題我認識到,你的主要問題是刪除元素和索引。這似乎支持問題都可以從答案

說,對於剩下的解決,有一個陣列

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

刪除索引內容(這裏說索引2)陣列的,你必須使用

unset($var['index2']); 

這是怎麼它將刪除內容與指數本身

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

讓我知道,如果我回答你的問題