Need some help removing element from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Need some help removing element from array

Post 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!
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Need some help removing element from array

Post by Mark Baker »

Code: Select all

 
foreach ($array as $key => $value) {
    unset($array[$key][5]);
}
 
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Need some help removing element from array

Post 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?
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: Need some help removing element from array

Post by mischievous »

just use array_push() to put the newly created element on the end (or where the last element was taken off)
Post Reply