download12749
2012-07-19 21:26
浏览 24
已采纳

将2d数组转换为1d PHP

I have an array (sample below) containing 3 arrays with very similar content. I was wondering if there was an easy way to put everything into one array, rather than 3 separate ones. Everything I try seems to just overwrite the data and I'm left with the info from the 3rd array only.

array
  0 => 
    array
      'america' => int 19
      'music' => int 6
      'states' => int 5
      'bank' => int 5
  1 => 
    array
      'america' => int 19
      'home' => int 3
      'society' => int 2
      'writers' => int 2
   2 => 
    array
      'america' => int 19
      'lutheran' => int 4
      'church' => int 4
      'national' => int 4
      'cruises' => int 3

Ideally the end result will look like this:

array
          'america' => int 19
          'music' => int 6
          'states' => int 5
          'bank' => int 5
          'america' => int 19
          'home' => int 3
          'society' => int 2
          'writers' => int 2
          'america' => int 19
          'lutheran' => int 4
          'church' => int 4
          'national' => int 4
          'cruises' => int 3

Any solutions? thanks

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

我有一个数组(下面的示例),其中包含3个内容非常相似的数组。 我想知道是否有一种简单的方法将所有内容放入一个数组,而不是3个单独的数组。 我尝试的所有东西似乎只是覆盖了数据而我只留下了来自第​​三个数组的信息。

  array 
 0 =>  
 array 
'america'=>  int 19 
'music'=>  int 6 
'states'=>  int 5 
'bank'=>  int 5 
 1 =>  
 array 
'america'=>  int 19 
'home'=>  int 3 
'society'=>  int 2 
'writers'=>  int 2 
 2 =>  
 array 
'america'=>  int 19 
'utheran'=>  int 4 
'church'=>  int 4 
'national'=>  int 4 
'cruises'=>  int 3 
   
 
 

理想情况下,最终结果如下所示:

  array 
'america'=>  ;  int 19 
'music'=>  int 6 
'states'=>  int 5 
'bank'=>  int 5 
'america'=>  int 19 
'home'=>  int 3 
'society'=>  int 2 
'writers'=>  int 2 
'america'=>  int 19 
'utheran'=>  int 4 
'church'=>  int 4 
'national'=>  int 4 
'cruises'=>  int 3 
   
 
 

任何解决方案? 谢谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douweng3564 2012-07-19 21:28
    已采纳

    You won't be able to have an associative array with the same key values (i.e. 3 keys of "America", etc.). When you try to set those keys in the array, you will simply overwrite the previous value.

    已采纳该答案
    打赏 评论
  • douzhi3776 2012-07-19 21:32
    打赏 评论
  • drfb52000 2012-07-19 21:37

    I was wondering if there was an easy way to put everything into one array

    Short answer is no.

    You could make a class with a variable for the original int and a value for the number of times it occurs and then have the array point to objects of the class. Same idea could be implemented using a linked list, though both ways are more complex than what you already have.

    For example the key 'america' would point to an object with one variable set to 19 and the other variable set to 3.

    Question: do you need to know which of the original arrays a value comes from? If yes then what I mentioned above would loose that property.

    打赏 评论
  • dqdmvg7332 2015-07-15 20:17
    function array_2d_to_1d($array)
                                        {
                                            $my_val=array();
                                            $my_key=array();
                                            foreach ($array as $value) {
                                                 array_push($my_val, $value['cat_title']);
                                                 array_push($my_key, $value['cat_id']);
                                             }
    
                                            $result = array_combine($my_key, $my_val);
                                            return $result;
    
                                        }                                
    
    打赏 评论

相关推荐 更多相似问题