Page 1 of 1

Need some help removing element from array

Posted: Mon Oct 26, 2009 2:53 pm
by someguyhere
I have a series of arrays within an array, and I need to delete an element from each of the sub-arrays. Here is a snippet of the array:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => sls.com
            [1] => -
            [2] => 37
            [3] => $20,015
            [4] =>  
            [5] => 8D 21H 
        )
 
    [1] => Array
        (
            [0] => cnnestore.com
            [1] => 2
            [2] => 28
            [3] => $165
            [4] =>  
            [5] => 1H 4M 
        )
 
I need to remove the fifth element from each of the arrays. I tried using unset, but it worked one level higher than I needed it to. Does anyone have any iput on how to do this? While I'm asking, is this the right way to stucture the array, or would you do it differently?

Thanks in advance!

Re: Need some help removing element from array

Posted: Mon Oct 26, 2009 3:33 pm
by Mark Baker

Code: Select all

 
foreach ($array as $key => $value) {
    unset($array[$key][5]);
}
 

Re: Need some help removing element from array

Posted: Mon Oct 26, 2009 5:15 pm
by someguyhere
Worked perfectly, thanks!

I'm trying to add something, but I can't seem to see where I'm going wrong. The idea here is to pull the Alexa data and add it to the array. If possible, I'd like to add it in the position of one of the elements I removed.

Code: Select all

    foreach ($gd_data as $key => $value) {
        unset($gd_data[$key][1]);
        unset($gd_data[$key][4]);
 
$url = $gd_data[$key][0];
 
        $querystring = 'http://xml.alexa.com/data?cli=10&dat=nsa&ver=quirk-searchstatus&uid=19700101000000&userip=127.0.0.1&url='.urlencode($url);
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $querystring);
        curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
        $alexaxml = curl_exec($ch);
        curl_close($ch);
        
        $alexa_pop = get_string_between($alexaxml, "\" TEXT=\"", "\"/>");
        $alexa_pop = number_format($alexa_pop);
 
        array_push($value, $alexa_pop);
 
    }
 
Any ideas?

Re: Need some help removing element from array

Posted: Mon Oct 26, 2009 11:12 pm
by mischievous
just use array_push() to put the newly created element on the end (or where the last element was taken off)