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

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;

图片转代码服务由CSDN问答提供 功能建议

我有一个这样的数组:

  [apple]  =>  11 
 [梨] =>  5 
 [banana] =>  3 
 [黄瓜] =>  2 
 [西红柿] =>  8 
   
 
 

我想创建这样的多维数组:

  [randomArrayName1] 
 [  apple] =>  11 
 [banana] =>  3 
 [西红柿] =>  8 
 [randomArrayName2] 
 [pears] =>  5 
 [黄瓜] =>  2 
   
 
 

我在下面写了这篇文章,但它不起作用,我不知道怎么做:我需要它做什么

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

randomArrayName1 randomArrayName2 将根据 Finder()函数的结果生成。 根据该结果,我希望将水果数组创建为第二维数组。


解决方案: 问题是我编写的函数如randomArrayName1,randomArrayName2 ...实际上是一个XML函数。 因此,这个函数的结果不是数组,而是XML简单对象。 这导致了创建多维数组的问题。 一旦我实现了下面的代码,它就将SimpleXML转换为Array并且代码运行正常。 $ type是SimpleXML数组 $ out是结果数组

  foreach((array)$ 输入$ 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)
      }
    }
    */
    
    已采纳该答案
    打赏 评论
  • dongxian3852 2015-11-19 18:41

    You need to create the new index or access it if it has already been created and then create another element under that with the existing key and assign the value:

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

    This is assuming that Finder() is returning a string like randomArrayName1.

    打赏 评论

相关推荐 更多相似问题