The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Unknown array size
Discuss Unknown array size in the PHP Development forum on Dev Shed. Unknown array size PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 2nd, 2013, 08:35 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
|
|
|
Unknown array size
hello everyone!!
I ' m really really new to php and I was wondering if it's possible to search an array of unknown size.
I have a string and I use explode in order to split it into other strings using as a delimeter the space character.
Then as a result,I have an array of strings.
Now,what I want to do is call a function for every string in the array but I dont know how many strings-how many array elements- exist.
How can I make this loop?
Thanks in advance!
here's what I have so far..
PHP Code:
$wholeString = $_GET['str'];
$array = explode(" ", $wholeString );
|

January 2nd, 2013, 08:44 AM
|
|
|
You always can know the size of an array using count.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

January 2nd, 2013, 08:54 AM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Your question has generic answers which can use foreach, array_walk, count, or any number of other things. Foreach is probably what you want.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

January 2nd, 2013, 09:22 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
|
|
|
thank you guys so much for your answers.
Also I'd like to ask if "count" is the best way to check
when I have reached the last element of the array,as in my code
the last element of the array has to deal with a different function from the other ones
|

January 2nd, 2013, 09:28 AM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
use array_pop to get the last element off the array beforehand.
|

January 2nd, 2013, 09:36 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 7
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by ManiacDan use array_pop to get the last element off the array beforehand. |
If I have understand properly the use of array-pop it deletes the last element of the array.All I want to do is to save the last element in a variable in order to use it for a check in the code
|

January 2nd, 2013, 09:44 AM
|
|
|
|
It does both. It returns the last element into a variable and removes it from the array. Alternatively you can use 'count' (as previously suggested) as the index of the last element and keep the array in tact.
|

January 2nd, 2013, 09:55 AM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
PHP Code:
$array = array(,1,2,3,4,5,"hello");
$last = array_pop($array);
foreach ( $array as $value ) {
someFunction($value);
}
otherFunction($last);
-Dan
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|