dso0139 2011-09-14 08:41
浏览 94
已采纳

PHP - 根据键名从多维数组创建单个数组

I know there are a lot of answers on multi-dimensional arrays but I couldn't find what I was looking for exactly. I'm new to PHP and can't quite get my head around some of the other examples to modify them. If someone could show me the way, it would be much appreciated.

An external service is passing me the following multidimensional array.

$mArray = Array (
  [success] => 1
  [errors] => 0
  [data] => Array (
    [0] => Array (
      [email] => me@example.com
      [id] => 123456 
      [email_type] => html 
      [ip_opt] => 10.10.1.1 
      [ip_signup] => 
      [member_rating] => X 
      [info_changed] => 2011-08-17 08:56:51 
      [web_id] => 123456789
      [language] =>
      [merges] => Array (
        [EMAIL] => me@example.com
        [NAME] => Firstname 
        [LNAME] => Lastname 
        [ACCOUNT] => ACME Ltd 
        [ACCMANID] => 123456adc 
        [ACCMANTEL] => 1234 123456 
        [ACCMANMAIL] => an.other@example.com
        [ACCMANFN] => Humpty
        [ACCMANLN] => Dumpty 
      )
      [status] => unknown
      [timestamp] => 2011-08-17 08:56:51
      [lists] => Array ( ) 
      [geo] => Array ( ) 
      [clients] => Array ( ) 
      [static_segments] => Array ( )
    ) 
  ) 
)

The only information I'm interested in are the key/value pairs that are held in the array under the key name 'merges'. It's about the third array deep. The key name of the array will always be called merges but there's no guarantee that its location in the array won't be moved. The number of key/value pairs in the merges array is also changeable.

I think what I need is a function for array_walk_recursive($mArray, "myfunction", $search);, where $search holds the string for the Key name (merges) I'm looking for. It needs to walk the array until it finds the key, check that it holds an array and then (preserving the keys), return each key/value pair into a single array.

So, for clarity, the output of the function would return:

    $sArray = Array (
      [EMAIL] => me@example.com
      [NAME] => Firstname 
      [LNAME] => Lastname 
      [ACCOUNT] => ACME Ltd 
      [ACCMANID] => 123456adc 
      [ACCMANTEL] => 1234 123456 
      [ACCMANMAIL] => an.other@example.com 
      [ACCMANFN] => Humpty 
      [ACCMANLN] => Dumpty
    )

I can then move on to the next step in my project, which is to compare the keys in the single merges array to element IDs obtained from an HTML DOM Parser and replace the attribute values with those contained in the single array.

I probably need a foreach loop. I know I can use is_array to verify if $search is an array. It's joining it all together that I'm struggling with.

Thanks for your help.

  • 写回答

6条回答 默认 最新

  • dongqiuge5435 2011-09-15 11:16
    关注

    Here is a general purpose function that will work it's way through a nested array and return the value associated with the first occurance of the supplied key. It allows for integer or string keys. If no matching key is found it returns false.

    // return the value a key in the supplied array  
    function get_keyval($arr,$mykey)
    {  
        foreach($arr as $key => $value){
            if((gettype($key) == gettype($mykey)) && ($key == $mykey)) {
                return $value;
            }
            if(is_array($value)){
                return get_keyval($value,$mykey);               
            }
        }
        return false;
    }
    
    // test it out
    $myArray = get_keyval($suppliedArray, "merges");
    foreach($myArray as $key => $value){
        echo "$key = $value
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题