Computing.Net > Forums > Web Development > php: unset() array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

php: unset() array

Reply to Message Icon

Name: mie2com
Date: December 13, 2006 at 23:28:57 Pacific
OS: WinXP
CPU/Ram: Pentium III
Product: Sony
Comment:

How to unset element in array and continue with loop. For example;

//This
$fruits[0] = "pineapple";
$fruits[1] = "pomegranate";
$fruits[2] = "tangerine";
$fruits[3] = "kiwi";

unset($fruits[1]);

for ($index = 0; $index < count($fruits); $index++){
echo $fruits[$index], "\n";

}

//Output
pineapple


How to unset an element and continue loop so it will display:

pineapple
tangerine
kiwi



Sponsored Link
Ads by Google

Response Number 1
Name: Laler
Date: December 14, 2006 at 05:53:18 Pacific
Reply:

Hi,

There're many approaches.

* After unsetting:

foreach ($fruits as $value)
{
echo $value . "\n";
}

(will loop through all available elements, not based on the keys as in your example above)


reset ($fruits);

(reset the array keys, so you can use index-based loop as in your example above).


It all depends on what you're going to achieve.

---
Fubar


0

Response Number 2
Name: Michael J (by mjdamato)
Date: December 14, 2006 at 08:14:13 Pacific
Reply:

reset ($fruits);

That will not reset the indexes of the array elements. From the manual:
reset -- Set the internal pointer of an array to its first element

However, the foreach is a better approach - assuming the index isn't required later on.

Michael J


0

Response Number 3
Name: Michael J (by mjdamato)
Date: December 14, 2006 at 08:19:27 Pacific
Reply:

One method of reindexing the array would be to use array_values().

//This
$fruits[0] = "pineapple";
$fruits[1] = "pomegranate";
$fruits[2] = "tangerine";
$fruits[3] = "kiwi";

unset($fruits[1]);
$fruits = array_values($fruits);

for ($index = 0; $index < count($fruits); $index++){
echo $fruits[$index], "\n";
}

//Output
pineapple
tangerine
kiwi

Michael J


0

Response Number 4
Name: Laler
Date: December 14, 2006 at 13:13:58 Pacific
Reply:

yes, Michael's right :D

shuffle() is the one that resets the keys.

---
Fubar


0

Response Number 5
Name: mie2com
Date: December 15, 2006 at 03:07:46 Pacific
Reply:

Thanks guys. I guess I need to go through string functions and understand them one by one. Does PHP programmer memorize all those functions such as math and string functions? Or, they do still make references to manual? What about you guys? Do you memorize all the functions? and, how you learn PHP in the beginning. The reason I ask is because I dont attend formal class and learn by e-books.


0

Related Posts

See More



Response Number 6
Name: Laler
Date: December 15, 2006 at 04:21:10 Pacific
Reply:

Programming is all about the logic. We don't need to memorize all the functions/syntax for a specific language, just keep the manual/reference nearby.

http://php.net/docs.php

Also remember that Google is a friend.

---

When you work with a specific language for a long time, you'll automatically memorize some of the functions.

For example, I got used to something like this after modifying an array:

shuffle($arr); reset($arr);

..or...

sort($arr); reset($arr);

The above will shuffle/sort the elements, and make it like a clean-new array. (But then I'm forgetting that reset() is not the one that rearranges the element index) :D

---
Fubar


0

Response Number 7
Name: Michael J (by mjdamato)
Date: December 15, 2006 at 06:31:38 Pacific
Reply:

I have no formal taining either. As Laler stated, it's all about the logic. You need to understand the constructs of programming and then implement them accordingly. For instance i see a lot of people using many, many elseif statements when they should be using switch() or some other methodology. But, they use what I call "brute force" programming.

As far as memorizing the functions - no I do not have them memorized. But, I have enough experience to know what functions "should" exist and, when needed, will go look for it. The official manual is here: http://www.php.net

In fact, there have been times I did not know something existed and I wrote a custom function to perform a task that could have been accomplished with a built-in function. Part of the learning process, I suppose.

Michael J


0

Response Number 8
Name: mie2com
Date: December 16, 2006 at 02:01:16 Pacific
Reply:

What is "brute force" programming?


0

Response Number 9
Name: Laler
Date: December 16, 2006 at 04:29:13 Pacific
Reply:

Look at Michael's example about using else if vs switch above.

Another example:

foreach ($array as $value)
{
$query = "SELECT * FROM foo WHERE bar = $value";
$sql = mysql_query ($query);
// print a row
}

The above is a no no, you should whenever possible customize the $query only in the loop, then interact with the database once.

$query = "";

foreach ($array as $value)
{
$query .= "bar = $value";
if (//loop not ends){ $query .= " OR "; }
}

$sql = mysql_query("SELECT * FROM foo WHERE $query");
// print the whole row

Basically, brute force programming means that the logic you use although correct but not efficient enough (especially from the machine's point of view).

---
Fubar


0

Response Number 10
Name: Laler
Date: December 16, 2006 at 04:32:39 Pacific
Reply:

ah didn't I tell you above that google is your friend? ;p

Brute force describes a primitive programming style, one in which the programmer relies on the computer's processing power instead of using his or her own intelligence to simplify the problem, often ignoring problems of scale and applying naive methods suited to small problems directly to large ones. The term can also be used in reference to programming style: brute-force programs are written in a heavyhanded, tedious way, full of repetition and devoid of any elegance or useful abstraction (see also brute force and ignorance).

Whether brute-force programming should actually be considered stupid or not depends on the context; if the problem is not terribly big, the extra CPU time spent on a brute-force solution may cost less than the programmer time it would take to develop a more 'intelligent' algorithm. Additionally, a more intelligent algorithm may imply more long-term complexity cost and bug-chasing than are justified by the speed improvement.

---
Fubar


0

Response Number 11
Name: mie2com
Date: December 16, 2006 at 05:43:22 Pacific
Reply:

:) I see. This means that brute force programming can be considered as bad practice. Rite? Where you can simply use 'switch' statement instead of repeating 'elseif' statement dozens of time.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Ultimate Slideshow - Big ... what do you think of my s...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: php: unset() array

RSS parser code problem in PHP www.computing.net/answers/webdevel/rss-parser-code-problem-in-php/754.html

PHP mySQL Navigation www.computing.net/answers/webdevel/php-mysql-navigation/4236.html

Website help! Please! www.computing.net/answers/webdevel/website-help-please/1802.html