开发者

append key array to an array value

开发者 https://www.devze.com 2023-03-23 21:36 出处:网络
array \'strongfruit\' => array apple => string \'say:helloworld\' banana => string \'say:omgdude\'
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)

to be something like

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)

yesterday i ask about joining arrays, preserving different value, pick one if same. with php

heres how we join them to get the order;

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

the different is how can we append the key to a value in an array?.


$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

精彩评论

暂无评论...
验证码 换一张
取 消