2012-03-03 7 views
1

ペアの値(名前と電子メール)の配列があり、2つのオフセットごとに通常の配列から連想配列を作成しようとしています。例:私が試した通常の配列の2つのオフセットのそれぞれから連想配列を作成するにはどうすればよいですか?

Array 
(
    [0] => Array 
     (
      [name] => joe 
      [email] => [email protected] 
     ) 
    [1] => Array 
     (
      [name] => bill 
      [email] => [email protected] 
     ) 
) 

$num = count($csvArray); 
for ($i=0; $i < $num; $i+2) { 
    $newArray[] = array(
     'name' => $csvArray[$i], 
     'email' => $csvArray[$i+1] 
    ); 
} 

それは私にこれを与え、それは次のようになりますので、

Array 
(
    [0] => joe 
    [1] => [email protected] 
    [2] => bill 
    [3] => [email protected] 
    [4] => kyle 
    [5] => [email protected] 
    [6] => matt 
    [7] => [email protected] 
    [8] => chris 
    [9] => [email protected] 
) 

私は、各[$i] & [$i+1]のための連想配列を作成したいと思いますエラー:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in C:\xampp\htdocs\array.php on line 20

何か間違っていますか?あなたのforループに問題がある

+1

'array.php'ファイルの20行目はどの行ですか? –

答えて

4

...それは、現在、それはあなたがarray_chunkを使用することができます

2

をインクリメントので、無限ループなっていない

for($i=0; $i < $num; $i=$i+2) 

でなければなりません。

$newArray = array_chunk($csvArray, 2); 
+0

あなたが私に尋ねるならば完璧な解決策。 –

+0

@Madmartiganありがとう:) – xdazz

関連する問題