English 中文(简体)
劳教职能
原标题:recursive function php
  • 时间:2010-08-17 16:22:13
  •  标签:
  • php
  • arrays

我有一阵 which。

$dataArray = array (
  0 => 
  array (
     UserId  =>  804023 ,
     ProjectCode  =>  RA1234 ,
     Role  =>  PI ,
  ),
  1 => 
  array (
     UserId  =>  804023 ,
     ProjectCode  =>  RA1234 ,
     Role  =>  PM ,
  ),
  2 => 
  array (
     UserId  =>  804023 ,
     ProjectCode  =>  A90123 ,
     Role  =>  CI ,
  ),
  3 => 
  array (
     UserId  =>  804023 ,
     ProjectCode  =>  A20022 ,
     Role  =>  PM ,
  ),
)

我需要这样做。

$expected = array (
  804023 => 
  array (
     RA1234  => 
    array (
      0 =>  PI ,
      1 =>  PM ,
    ),
     A90123  => 
    array (
      0 =>  PI ,
    ),
     A20022  => 
    array (
      0 =>  CI ,
    ),
  ),
)

我认为,这可以通用地利用再次入侵来实现,因为这是一个我可能多次到来的情景。

我已经走过了这么多的路程,这些钥匙构成nes阵列的关键。

$keys=array("UserId","projectCode","Role");

但是,看不到从哪里走到哪里?

public function structureData(array $data, array $keys)
 {
  //$structuredData = array();


  foreach ($data as $key => $value)
  {
   $keyForData = array_slice($keys,0,1);


   $remainingKeys = $keys;
   array_shift($remainingKeys);

   if (!array_key_exists($value[$keyForData[0]], $structuredData))
   {

    $count=count($remainingKeys);


    $structuredData[$value[$keyForData[0]]] =array();
    // this returns as expected array(804023 =>array ()); but subsequent recursive calls with the remaining data fail



   }

  }
  return $structuredData);
}
最佳回答

粗略但行之有效的解决办法。

function structureData($data, $keys){                                           
    $out = array();                                                             
    foreach($data as $row){                                                     
        $subout = &$out;                                                        
        foreach(array_slice($keys, 0, -1) as $key){                             
            $value = $row[$key];                                                
            $subout = &$subout[$value];                                         
        }                                                                       
        $subout[] = $row[$keys[count($keys) - 1]];                              

    }                                                                           
    return $out;                                                                
}                                                                               

print_r(structureData($dataArray, array( UserId ,  ProjectCode ,  Role )));     
问题回答

你不需要再行,只是一个 lo子:

foreach ($dataArray as $da) {
    $expected[$da[ UserId ]][$da[ ProjectCode ]][] = $da[ Role ];
}

var_export($expected);

/* output:

array (
  804023 => 
  array (
     RA1234  => 
    array (
      0 =>  PI ,
      1 =>  PM ,
    ),
     A90123  => 
    array (
      0 =>  CI ,
    ),
     A20022  => 
    array (
      0 =>  PM ,
    ),
  ),
)

*/

回收? Nah. 为此:

function add_role($dataArray, $userid, $project_code, $role)
{
    $dataArray[$userid][$project_code][] = $role;
}

功能解决办法:

$t = array_gather_key($dataArray, function ($e) { return $e[ UserId ]; } );

$t = array_map(
    function ($e) {
        return array_gather_key($e,
            function ($e) { return $e[ ProjectCode ];  },
            function ($e) { return $e[ Role ]; } );
     },
     $t
);

具有较高职等职能:

function array_gather_key($array, $func, $transf = null) {
    $res = array();
    foreach ($array as $elem) {
        $key = $func($elem);
        if (!array_key_exists($key, $res))
            $res[$key] = array();
        if ($transf === null)
            $res[$key][] = $elem;
        else
            $res[$key][] = $transf($elem);
    }
    return $res;
}

因此:

array(1) {
  [804023]=>
  array(3) {
    ["RA1234"]=>
    array(2) {
      [0]=>
      string(2) "PI"
      [1]=>
      string(2) "PM"
    }
    ["A90123"]=>
    array(1) {
      [0]=>
      string(2) "CI"
    }
    ["A20022"]=>
    array(1) {
      [0]=>
      string(2) "PM"
    }
  }
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签