0

我想出了这个丑陋的冗长数组(我必须使用它):

$puppy_mother_father_arr = array(
    array('46' => array('30','29')),
    array('17' => array('30','29')),
    array('16' => array('24','29'))
);

我如何将其简化为这样的:

$puppy_mother_father_arr = array(
    '46' => array('30','29'),
    '17' => array('30','29'),
    '16' => array('24','29')
);

我在这里被困了一天。提前致谢

4

4 回答 4

3
$tmp = array();
foreach ($puppy_mother_father_arr as $parent) {
  foreach($parent as $key => $nodes) {
    $tmp[$key] = $nodes;
  }
}
$puppy_mother_father_arr = $tmp;

这行得通吗?

于 2013-09-24T06:13:02.903 回答
0

key如果每个数组只有一个感兴趣的键和current值,则可能很有用:

$result = array();
foreach($puppy_mother_father_arr as $arr) {
    $result[key($arr)] = current($arr);
}
var_dump($result);
于 2013-09-24T06:29:16.870 回答
0
$newarray = array();
foreach ($puppy_mother_father_arr as $array) {
   foreach ($array as $puppy => $parents) {
       $newarray[$puppy] = $parents;
   }
}
$puppy_mother_father_arr = $newarray;
于 2013-09-24T06:14:49.637 回答
0

在线查看结果

<?php
$puppy_mother_father_arr = array( array('46' => array('30','29')),array('17' => array('30','29')),array('16' => array(24,'29')) );


$list = array();
foreach  ($puppy_mother_father_arr as $info)
{
    foreach ($info as $key => $value)
    {
        $list[$key] = $value;
        break;
    }
}
var_export($list);
于 2013-09-24T06:13:51.227 回答