2

配列の値を別の配列から比較する必要があります。何かしましたが、キーを保存する方法がわかりません。

$razeni=Array(0=>1,1=>2,2=>0,3=>3);
$myservices=Array(0=>"text0", 1=>"text1", 2=>"text2", 3=>"text3", 4=>"text4", 5=>"text5", 6=>"text6", 7=>"text7");

今すぐ比較

foreach ($razeni as $key=>$value) {
  $myservices_[$value] = $myservices[$value];
  unset($myservices[$value]);    
}

if (isset($myservices_))
{
  $myservices = array_merge($myservices_, $myservices);
}

結果:

Array
(
    [0] => text1
    [1] => text2
    [2] => text0
    [3] => text3
    [4] => text4
    [5] => text5
    [6] => text6
    [7] => text7
)

しかし、私はこの結果が必要でした

Array
(
    [1] => text1
    [2] => text2
    [0] => text0
    [3] => text3
    [4] => text4
    [5] => text5
    [6] => text6
    [7] => text7
)
4

1 に答える 1

1

array_merge を使用する代わりに

$myservices = $myservices_ + $myservices;

最初の配列の要素を上書きせず、インデックスを再作成せずに、2 番目の配列の配列要素を最初の配列に追加する場合は、+ 配列結合演算子を使用します。

于 2011-02-13T14:40:25.067 に答える