摆脱数组在PHP中的数组

时间:2013-09-24 06:09:55

标签: php arrays

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

$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 个答案:

答案 0 :(得分:3)

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

这会有用吗?

答案 1 :(得分:0)

See the result online

<?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);

答案 2 :(得分:0)

$newarray = array();
foreach ($puppy_mother_father_arr as $array) {
   foreach ($array as $puppy => $parents) {
       $newarray[$puppy] = $parents;
   }
}
$puppy_mother_father_arr = $newarray;

答案 3 :(得分:0)

如果每个数组只有一个感兴趣的键和值,则

keycurrent可能很有用:

$result = array();
foreach($puppy_mother_father_arr as $arr) {
    $result[key($arr)] = current($arr);
}
var_dump($result);