2012-02-02 81 views
1

我有一個需要大量文件名的腳本,然後序列化數據,串行化數據中可能有數百個文件名。PHP - 抓取序列化數據中的每個第四個值

我希望能夠做的是回顯出每個第四個文件名。

我不太清楚如何去做這件事,如果有人可以幫助它,將不勝感激。

以下是串行數據的例子,其只拿到了8個記錄,所以只有2名是輸出

a:8:{i:0;s:62:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_o.jpg";i:1;s:62:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_s.jpg";i:2;s:62:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_t.jpg";i:3;s:63:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_st.jpg";i:4;s:62:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_o.jpg";i:5;s:62:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_s.jpg";i:6;s:62:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_t.jpg";i:7;s:63:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_st.jpg";} 
+4

做什麼用'unserialize'問題? – Vyktor 2012-02-02 14:25:14

回答

2

您可以用MOD運營商或一個簡單的for循環實現這一目標:

$data = "a:8:{i:0;s:62:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_o.jpg";i:1;s:62:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_s.jpg";i:2;s:62:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_t.jpg";i:3;s:63:"1328018910RYw4LKmf3yb2ZhCHcFB9VvPqGMTrxgXzdDpWQntJN7k6j8_st.jpg";i:4;s:62:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_o.jpg";i:5;s:62:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_s.jpg";i:6;s:62:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_t.jpg";i:7;s:63:"1328018917rdwfFQ3xBDgvLKZCH4j8qTpYkz2VmJ9y7XntRcNbMG6PWh_st.jpg";}" 

$data = unserialize($data); 
for($idata = 3; $idata <= count($data)-1; $idata += 4){ 
    echo $data[$idata].'<br />'; 
} 

如果你想mod運算符做到這一點,而不是:

$data = unserialize($data); 
$idata = 0; 
foreach($data as $dataitem){ 
    if(($idata % 4) == 3){ 
     echo $dataitem.'<br />'; 
    } 
} 

模是OPERAT或作爲X把一個號碼,返回餘:

0 % 4 = 0 
1 % 4 = 1 
2 % 4 = 2 
3 % 4 = 3 
4 % 4 = 0 
5 % 4 = 1 
6 % 4 = 2 
7 % 4 = 3 
8 % 4 = 0 
9 % 4 = 1 
+0

那麼....這是完美的,非常感謝:) – BigJobbies 2012-02-02 14:30:44

0

你可以簡單的序列化返回的數據...那麼你可以趕上

相關問題