0

(データベースから返された行に基づいて) 別の配列で空の配列の値を設定しようとしています。

私はいくつかのことを試しましたarray_mergeが、最初の配列に追加するだけです。

これを行う方法があるかどうか、または各配列を反復してマージする必要があるかどうかに興味がありますか?

2 番目の配列には 1 ~ 3 個の配列を含めることができますが、最初の ( 「空の」 ) 配列には常に 3 つの要素があります。

Empty array

(
    [0] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

    [1] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

    [2] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

)

データをコピーしたい配列

Array
(
    [0] => Array
        (
            [id] => 1
            [quote_id] => 1
            ...
        )

    [1] => Array
        (
            [id] => 2
            [quote_id] => 1
            ...
        )

) 

達成したいこと

Array
(
    [0] => Array
        (
            [id] => 1
            [quote_id] => 1
            ...
        )

    [1] => Array
        (
            [id] => 2
            [quote_id] => 1
            ...
        )

      [2] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

) 
4

4 に答える 4

2

配列ユニオン演算子Docsを利用できます。

$combined = $rows + $empty;

+演算子は、左側の配列に追加された右側の配列を返します。両方の配列に存在するキーの場合、左側の配列の要素が使用され、右側の配列の一致する要素は無視されます。

于 2012-10-01T10:34:29.547 に答える
0

を使用することもできますarray_replace()が、キーには十分注意する必要があります。PHP マニュアルの例を参照してください。

于 2012-10-01T10:30:22.817 に答える
0

あなたが試すことができます

$empty = array(
"0" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null),
"1" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null),
"2" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null))

;

$content = Array(
"0" => Array("id" => 1,"quote_id" => 1,"position" => 1,"type" => "dwdwdw","number" => 22,"cost" => 33.00,"total" => 726.00),
"1" => Array("id" => 2,"quote_id" => 1,"position" => 2,"type" => "dwdw","number" => 22,"cost" => 22.00,"total" => 484.00));



var_dump(array_merge($content,array_unique($empty)));

出力

array
  0 => 
    array
      'id' => int 1
      'quote_id' => int 1
      'position' => int 1
      'type' => string 'dwdwdw' (length=6)
      'number' => int 22
      'cost' => float 33
      'total' => float 726
  1 => 
    array
      'id' => int 2
      'quote_id' => int 1
      'position' => int 2
      'type' => string 'dwdw' (length=4)
      'number' => int 22
      'cost' => float 22
      'total' => float 484
  2 => 
    array
      'id' => null
      'quote_id' => null
      'position' => null
      'type' => null
      'number' => null
      'cost' => null
      'total' => null
于 2012-10-01T10:31:36.347 に答える
0

これを試して、

<?php
 $result=mysql_query($query);
 $i=0;
 while($row=mysql_fetch_assoc($result)){
 $youArray[$i]['id']=$row['id'];
 $youArray[$i]['quote_id']=$row['quote_id'];
 //remaining values
 $i++;
 }
?>
于 2012-10-01T10:35:53.047 に答える