PHP删除键来构建另一个数组

时间:2013-07-30 00:47:28

标签: php arrays

我正在一个现有的程序中工作,该程序的功能可以带来比我需要的更多结果。我试图不重建该功能。我只需要将阵列减少到可用的大小。该数组如下所示:

array (size=2)
0 => 
array (size=24)
  'event' => '2013-07-22' 
  'endDate' => '0000-00-00' 
  'startTime' => '09:00:00' 
  'endTime' => '09:20:00' 
  'duration' => '1200'
  'recurrtype' => '0' 
  'recurrspec' => 
  'recurrfreq' => '0' 
  'catid' => '9' 
  'eid' => '13714'
  'title' => 'Physical' 
  'hometext' => 'CALLING PT # NOT IN SERV.' 
  'fname' => 'John' 
  'mname' => '' 
  'lname' => 'Bass Jr' 
  'id' => '993' 
  'pubpid' => '993' 
  'phone_home' => '555-111-2222'
  'phone_cell' => ''
  'ufname' => 'Dr' 
  'umname' => 'B'
  'ulname' => 'No'
  'uprovider_id' => '2' 
  'catname' => 'Physical'
1 => 
array (size=24)
  'event' => '2013-07-22' 
  'endDate' => '0000-00-00' 
  'startTime' => '09:00:00' 
  'endTime' => '09:20:00' 
  'duration' => '1200'
  'recurrtype' => '0' 
  'recurrspec' => 
  'recurrfreq' => '0' 
  'catid' => '9' 
  'eid' => '13717'
  'title' => 'Physical' 
  'hometext' => '' 
  'fname' => 'James' 
  'mname' => '' 
  'lname' => 'Lenny Jr' 
  'id' => '993' 
  'pubpid' => '993' 
  'phone_home' => '555-111-2222'
  'phone_cell' => ''
  'ufname' => 'Dr' 
  'umname' => 'B'
  'ulname' => 'No'
  'uprovider_id' => '2' 
  'catname' => 'Physical'

我需要的所需数组是:

 array (size=2)
 0 =>
  'event' => '2013-07-22' 
  'startTime' => '09:00:00' 
  'fname' => 'John' 
  'lname' => 'Bass Jr' 
  'phone_home' => '555-111-2222'
 1 =>
  'event' => '2013-07-22' 
  'startTime' => '09:00:00' 
  'fname' => 'James' 
  'lname' => 'Lenny Jr' 
  'phone_home' => '555-111-2222'

我在PHP手册中搜索了一个函数,该函数会返回给我这个数组,但是我无法看到如何过滤掉我不需要的键。 提前致谢。

4 个答案:

答案 0 :(得分:0)

创建自己的函数,如:

function get_desired_array($theArray)
{
   $desired_keys = array( 'event','startTime', 'fname', 'lname','phone_home');
   $desired_array = array();

   foreach ($theArray as $childArray)
   {
      foreach ($childArray as $k => $v)
      {
          if(in_array($k, $desired_keys))
          {
             $desired_array[$k] = $v;
          }
      }
   }

   return $desired_array;
 }

运行功能:

$desired_array = get_desired_array($theArray);

//you can now:
var_dump($desired_array);

答案 1 :(得分:0)

带头后给了我。我试过很简单的事。我知道我需要一个foreach循环。所以我用这种方式构建了循环:

$parentArray = getFunction();
  foreach ($parentArray as $childArray) {

       $desired_array[] = array (
            "Fname" => $childArray['fname'],
            "Lname" => $childArray['lname'],
            "Phone" => $childArray['phone_home'],
            "Date" => $childArray['event'],
            "Time" => $childArray['startTime']                                
          );

    }

看起来foreach循环只返回我在$ childArray中指定的值,而忽略了所有其余的值。

答案 2 :(得分:0)

你可以使用function:array_column。但需要你的php> = 5.5.0

答案 3 :(得分:0)

PHP中有特殊功能:array_intersect_key

这是代码:

$originArray = array(
  // your original array here
  // 0 => array(...)
  // 1 => array(...)
  // ...
  // n => array(...)
);
$desiredKeys = array_flip(array('event', 'startTime', 'fname', 'lname', 'phone_home'));
foreach ($originArray as $index => $subArray)
{
    $originArray[$index] = array_intersect_key($subArray, $desiredKeys);
}

PS。我使用array_flip将值转换为键,因为array_intersect_key通过键进行比较。另一种定义$ desiredKeys的方法:

$desiredKeys = array('event'=>1, 'startTime'=>1, 'fname'=>1, 'lname'=>1, 'phone_home'=>1);