2016-01-21 114 views
-1

所以這可能是一個簡單的問題,但我不能確定它,好吧,我將如何到達數組裏面的數據?我想從數組的第二個元素中獲取最後兩個逗號分隔的值,如果這是有道理的。陣列的數組內部元素

我的數組是這樣的:

Array 
(
[0] => Array 
    (
     [0] => Print Date 
     [1] => Cost 
     [2] => Recipient 
     [3] => Status 
     [4] => Tracking #/Insurance ID 
     [5] => Date Delivered 
     [6] => Carrier 
     [7] => Class Service 
     [8] => Special Services 
     [9] => Insured Value 
     [10] => Cost Code 
     [11] => Weight 
     [12] => Mail Date 
     [13] => Refund Type 
    ) 

[1] => Array 
    (
     [0] => 1/20/2016 
     [1] => $8.15 
     [2] => Justin De Los Polive, PO BOX 2353, BLANCO, TX 78606-1232 
     [3] => Printed 
     [4] => ="9405511" 
     [5] => 
     [6] => USPS 
     [7] => Priority Mail (R) 
     [8] => USPS Tracking 
     [9] => 
     [10] => 
     [11] => 1lb 8oz 
     [12] => 1/20/2016 
     [13] => E-refund 
    ) 

[2] => Array 
    (
     [0] => 1/20/2016 
     [1] => $8.15 
     [2] => Kist Benings, PO BOX 126, SPECULATOR, NY 12164-0026 
     [3] => Printed 
     [4] => ="9405511899563327" 
     [5] => 
     [6] => USPS 
     [7] => Priority Mail (R) 
     [8] => USPS Tracking 
     [9] => 
     [10] => 
     [11] => 1lb 8oz 
     [12] => 1/20/2016 
     [13] => E-refund 
    ) 

[3] => Array 
    (
     [0] => 1/20/2016 
     [1] => $8.92 
     [2] => billy flyn, PO BOX 696, P.O. BOX 696, WESTCLIFFE, CO 81252-1696 
     [3] => Printed 
     [4] => ="94063388113621" 
     [5] => 
     [6] => USPS 
     [7] => Priority Mail (R) 
     [8] => USPS Tracking 
     [9] => 
     [10] => 
     [11] => 1lb 8oz 
     [12] => 1/20/2016 
     [13] => E-refund 
    ) 

我希望能夠搶在第二個元素的最後兩個逗號分隔值(即國家和城市),並把它們轉化爲自己元素所以它看起來像這樣:

[2] => Array 
    (
     [0] => 1/20/2016 
     [1] => $8.15 
     [2] => Kist Benings, PO BOX 126, 
     [3] => SPECULATOR 
     [4] => NY 12164-0026 
     [5] => Printed 
     [6] => ="9405511899563327" 
     [8] => 
     [9] => USPS 
     [10] => Priority Mail (R) 
     [11] => USPS Tracking 
     [12] => 
     [13] => 
     [14] => 1lb 8oz 
     [15] => 1/20/2016 
     [16] => E-refund 
    ) 

我的代碼:

<?php 

function csv_to_array($filename='', $delimiter=',') 
{ 
    if(!file_exists($filename) || !is_readable($filename)) 
     return FALSE; 

    $header = NULL; 
    $data = array(); 
    if (($handle = fopen($filename, 'r')) !== FALSE) 
    { 
     while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) 
     { 
      if(!$header) 
       $header = $row; 
      else 
       $data[] = array_combine($header, $row); 
     } 
     fclose($handle); 
    } 
    return $data; 
} 

echo "<pre>"; 
print_r(csv_to_array('PrintHistory.csv')); 
echo "</pre>"; 

die(); 
?> 
+4

'回聲$陣列[2] [12],$陣列[2] [13 ]'? –

+1

[array_pop](http://php.net/array_pop)? –

+2

我很困惑,因爲索引12和13不包含你提到的郵政編碼或城市,所以如果你打算訪問[2]中的地址部分,你可以使用'explode(',',$ array [2] [2 ])' – Alex2php

回答

0

假設記錄洽:

採取Recipient和爆炸了。 輸出記錄是使用數組片生成的。

笨拙,但它的工作...

working demo...

驗證碼:

// output in here 
$out = array(); 

// lose first record... 
array_shift($data); 

foreach ($data as $details) { 

    $recipient = explode(',', $details[2]); 

    $outRec = array_merge(
       array_slice($details, 0, 2), 

       array($recipient[0] .', '. $recipient[1]), 
       array_slice($recipient, 2, 1), 
       array_slice($recipient, 3, 1), 

       array_slice($details, 3) 
      ); 

    $out[] = $outRec; 
} 
0

如果Recipient格式是相當一致的:

foreach(array_column($csv, 2) as $val) { 
    preg_match('/(\w+), (\w{2}) [\d-]+$/', $val, $matches); 
    $city = isset($matches[1]) ? $matches[1] : 'none'; 
    $state = isset($matches[2]) ? $matches[2] : 'none'; 
} 
  • array_column()得到所有從內陣列2索引的陣列。
  • 循環該數組。
  • 正則表達式應該得到的城市和州。

你可能需要做一個$something = array_column()然後unset($something[0]);循環之前擺脫了頭陣。

+0

對不起,我超新的PHP,但我得到一個未定義的抵消:1 $城市線和同樣的事情$狀態?你在哪裏設置這些值和相同的東西匹配? –

+0

這僅僅是一個側面項目,只是試圖進入我自己的編程,但是當我使用它時,它保持不變並且沒有任何變化。 –

+0

保持不變。它沒有改變任何東西。 'echo「$ city $ state」;' – AbraCadaver