1

i have a array and want to remove its key values.. which are 0 ,1 and so on .. array have dynamic values. and the key index can be dynamic increase or decease.

$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; 
    }
}                

I need Output

Array 
    (

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

        ),

        'youtube' => Array
        (
            'youtube.com' => 'https://www.youtube.com/sfsfsd'
        ),
    );
Shagun Sood
  • 153
  • 1
  • 2
  • 11

2 Answers2

3

Maybe you're trying to do something like this:

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

If you get rid of the extra space after you key "youtube ", you'll get the following output:

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

)
Expedito
  • 7,771
  • 5
  • 30
  • 43
  • You could recursively call this chunk to get it to a 2Dimensional array?, also Array keys cannot be duplicated.. So only the last element with the duplicate key will be shown – Daryl Gill Aug 08 '13 at 11:26
  • @DarylGill - Is that more like what you were talking about? – Expedito Aug 08 '13 at 11:37
0

Try This,

$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; 
  }
}  
Manish Chauhan
  • 595
  • 2
  • 7
  • 14