2011-07-31 9 views
0
array 
    'strongfruit' => 
    array 
     apple => string 'say:helloworld' 
     banana => string 'say:omgdude' 
     grape => string 'say:dope' 
     alienfruit => string 'say:ganja' 
    'weakfruit' => 
    array 
     apple => string 'say:helloworld' 
     banana => string 'say:omgdude' 
     grape => string 'say:dope' 
     orange => string 'say:yeahdude' 
    'moreweakerfruit' => 
    array 
     apple => string 'say:helloworld' 
     anotheralienfruit => string 'say:yeahhellyeah' 
    (etc) 

私はおよそjoining arrays, preserving different value, pick one if same. with phpは、配列の値にキー配列を付加

を尋ねる

array 
     apple => string 'strongfruit:say:helloworld' (from strong) 
     banana => string 'strongfruit:say:omgdude' (from strong) 
     grape => string 'strongfruit:say:dope' (from strong) 
     alienfruit => string 'strongfruit:say:ganja' (from strong) 
     orange => string 'weakfruit:say:yeahdude' (from weak) 
     anotheralienfruit => string 'moreweakerfruit:say:yeahhellyeah' (from weakeretc) 

昨日のようなものであることをHERESに私達は順序を得るためにそれらを結合する方法。

$result = array(); 
foreach ($array as $value) { $result += $value; } 

異なる我々は、配列内の値にキーを追加することができる方法ですか?

+0

複数のキー(つまり、「apple」)の下に表示される果物を、どのように処理したいですか?最初に遭遇した後のものが捨てられるべきか? –

+0

強い果実からそれを手に入れよう。他の人はそこにいるべきではありません。 –

答えて

1
$newarray = array(); 

foreach ($array as $type => $fruit) { 
    foreach ($fruit as $fruitname => $string) { 
     //if the fruit is not already in the new array 
     if (!isset($newarray[$fruitname])) { 
      $newarray[$value] = $type . ':' . $string; 
     } 
    } 
} 
+0

$値は何から来ていますか? '$ newarray [$ fruitname] = $ typeを意味しますか? ':' $ string; ' –