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'
        ),
    );
8
  • 5
    You can't duplicate keys Commented Aug 8, 2013 at 10:56
  • 1
    You're not going to get exactly that output, since an array can't have the same key twice. Also, what's your problem/question? Commented Aug 8, 2013 at 10:56
  • 1
    And how would you distinguish between the two? What would $array['youtube'] be? Commented Aug 8, 2013 at 10:56
  • stackoverflow.com/questions/5450148/…? Commented Aug 8, 2013 at 10:57
  • 1
    What's problem with original data, it will not take your thousands of GB space. Commented Aug 8, 2013 at 10:58

2 Answers 2

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
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

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
@DarylGill - Is that more like what you were talking about?
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; 
  }
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.