добавить массив ключей к значению массива - PullRequest
0 голосов
/ 31 июля 2011
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)

вчера я спрашиваю о соединении массивов, сохраняя другое значение, выбирайте одно, если оно одинаковое. с php

Вот как мы присоединяемся к ним, чтобы получить заказ;

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

отличается , как мы можем добавить ключ к значению в массиве? .

1 Ответ

1 голос
/ 31 июля 2011
$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;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...