将键数组附加到数组值

时间:2011-07-31 07:09:44

标签: php arrays logic

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)

就像是

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)

昨天我问joining arrays, preserving different value, pick one if same. with php

继承人如何加入他们以获得订单;

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

不同的是如何将键附加到数组中的值?

1 个答案:

答案 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;
        }
    }
}