从php数组中删除数组键

时间:2013-08-08 10:54:51

标签: php arrays

我有一个数组,想要删除它的键值..它们是0,1等等。数组有动态值。 并且关键指标可以是动态增加或减少。

$url = Array 
        (
            0 => Array
            (
                'youtube ' => Array
                (
                    'youtube.com' => "https://www.youtube.com/dfssfskj8i"
                ),
            ),

            1 => Array
            (
                'youtube' => Array
                (
                    'youtube.com' => 'https://www.youtube.com/sfsfsd'
                ),
            )
        );



$temp = array();
foreach($url as $key => $val){
    foreach($val as $key1 => $val1){
        $temp[$key1][$val1] = $key1; 
    }
}                

我需要输出

Array 
    (

        'youtube ' => Array
        (
            'youtube.com' => "https://www.youtube.com/dfssfskj8i"

        ),

        'youtube' => Array
        (
            'youtube.com' => 'https://www.youtube.com/sfsfsd'
        ),
    );

2 个答案:

答案 0 :(得分:3)

也许你正试图做这样的事情:

$out = array();
foreach ($url as $key => $value){
    $dex = key($value);
    $out[$dex][] = reset($value[$dex]);
}
print_r($out);

如果在键入“youtube”后删除了额外的空格,您将获得以下输出:

Array
(
    [youtube] => Array
        (
            [0] => https://www.youtube.com/dfssfskj8i
            [1] => https://www.youtube.com/sfsfsd
        )

)

答案 1 :(得分:0)

试试这个,

$url = Array 
    (
        0 => Array
        (
            'youtube ' => Array
            (
                'youtube.com' => "https://www.youtube.com/dfssfskj8i"
            ),
        ),

        1 => Array
        (
            'youtube' => Array
            (
                'youtube.com' => 'https://www.youtube.com/sfsfsd'
            ),
        )
    );

$temp = array();
foreach($url as $key => $val){
  foreach($val as $key1 => $val1){
     $temp[$key1] = $val1; 
  }
}