dongnue4923 2015-11-19 18:38
浏览 177
已采纳

PHP根据函数创建二维数组

I have an array like this:

[apple] => 11
[pear] => 5
[banana] => 3
[cucumber] => 2
[tomatoes] => 8

I would like to create multi-dimensional array like this:

[randomArrayName1]
        [apple] => 11
        [banana] => 3
        [tomatoes] => 8
[randomArrayName2]
        [pears] => 5
        [cucumber] => 2

I wrote this below but it is not working and I have no clue how to make it do: what I need it to do

$quantity = array();
foreach($fruit as $key => $value) {
    $quantity = Finder($key);
}

randomArrayName1 and randomArrayName2 will be generated depending on Finder() function's result. Depending on that result I want fruit array to be created as a second dimension array.


Solution: The problem was that the function I wrote that created names like randomArrayName1, randomArrayName2... was in fact a XML function. Therefore, result of this function was not an array, it was XML simple object. This caused problem on creating multidimensional array. Once I implemented the code below it converted SimpleXML to Array and code worked fine. $type is SimpleXML array $out is result array

   foreach ( (array) $type as $index => $node )
    $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
  • 写回答

2条回答 默认 最新

  • drsb77336 2015-11-19 19:02
    关注

    You can use array_reduce. It takes three arguments, an initial array to operate on, a function which should operate on each element (which in turn requires two arguments, $carry, which is a variable that gets passed through to the function each time through the return statement, and $item, which is the array item being operated on) and finally an "initial" value for $carry within the function, which in this case is going to be our multidimensional array that gets eventually returned.

    This is an example where the Finder function returns a key based on whether the value of the items in the array is odd or even, and sorts the values accordingly.

    Note that we are operating on the array_keys of fruits, so that we have access to both key and value inside our reducer.

    <?php
    
    $fruit = ['apple' => 11, 'pear' => 5, 'banana' => 3, 'cucumber' => 2, 'tomatoes' => 8];
    
    function Finder($fruit) {
        return $fruit % 2 == 0 ? 'even' : 'odd';
    }
    
    $multi = array_reduce(array_keys($fruit), function($carry, $item) use ($fruit) {
        $val = $fruit[$item];
        $carry[Finder($val)][$item] = $val;
        return $carry;
    }, []);
    
    var_dump($multi);
    
    /* // output
    array(2) {
      ["odd"]=>
      array(3) {
        ["apple"]=>
        int(11)
        ["pear"]=>
        int(5)
        ["banana"]=>
        int(3)
      }
      ["even"]=>
      array(2) {
        ["cucumber"]=>
        int(2)
        ["tomatoes"]=>
        int(8)
      }
    }
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印