将键数组附加到数组值

发布于 2024-11-27 04:37:09 字数 1369 浏览 2 评论 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)

就像

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

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

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?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-12-04 04:37:09
$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;
        }
    }
}
$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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文