473,473 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why does this foreach loop fail to work?

[PHP]
$array = array($firstOrderByFieldIfClause, $groupByClause,
$orderByClause);
array_walk($array, create_function('&$a', '$a = substr($a, 0,
strrpos(trim($a), ",")); if ($a) $a = ", $a";'));
print_r("firstOrderByFieldIfClause = $firstOrderByFieldIfClause and
groupByClause = $groupByClause and orderByClause = $orderByClause<P>");
[/PHP]

Upon execution the fields within $array are never changed (verified by
checking code), and I don't understand why.

firstOrderByFieldIfClause = IF(image_creation_date IS NULL OR
image_creation_date = '' OR image_creation_date LIKE '0000%', 'x',
NULL) AS image_creation_date_alt, and groupByClause =
image_creation_date_alt, and orderByClause = image_creation_date_alt
desc,
Please review and help, I'm utterly lost!

Thanx
Phil

Sep 18 '06 #1
2 1166
"comp.lang.php" <ph**************@gmail.comwrote:
>
[PHP]
$array = array($firstOrderByFieldIfClause, $groupByClause,
$orderByClause);
array_walk($array, create_function('&$a', '$a = substr($a, 0,
strrpos(trim($a), ",")); if ($a) $a = ", $a";'));
print_r("firstOrderByFieldIfClause = $firstOrderByFieldIfClause and
groupByClause = $groupByClause and orderByClause = $orderByClause<P>");
[/PHP]

Upon execution the fields within $array are never changed (verified by
checking code), and I don't understand why.
It certainly does. Try "var_dump($array);", and you'll see that all three
strings were changed.

Your mistake is in thinking that changes to $array(0) will also affect
$firstOrderByFieldIfClause.

The array constructor makes three new strings.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 19 '06 #2

Tim Roberts wrote:
"comp.lang.php" <ph**************@gmail.comwrote:

[PHP]
$array = array($firstOrderByFieldIfClause, $groupByClause,
$orderByClause);
array_walk($array, create_function('&$a', '$a = substr($a, 0,
strrpos(trim($a), ",")); if ($a) $a = ", $a";'));
print_r("firstOrderByFieldIfClause = $firstOrderByFieldIfClause and
groupByClause = $groupByClause and orderByClause = $orderByClause<P>");
[/PHP]

Upon execution the fields within $array are never changed (verified by
checking code), and I don't understand why.

It certainly does. Try "var_dump($array);", and you'll see that all three
strings were changed.

Your mistake is in thinking that changes to $array(0) will also affect
$firstOrderByFieldIfClause.

The array constructor makes three new strings.
I should have figured that all complex objects that take parameters
make clones of the parameters unless passed by reference. So that's
what I did:

$array = array(&$firstOrderByFieldIfClause, &$groupByClause,
&$orderByClause);

That did the trick, thanx!
Phil
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lawrence | last post by:
Suppose I create dynamic web pages with 3 functions (which call other functions to make everything happen, but these 3 you might think of as being the top layer): registerSessions();...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
8
by: Brad Wood | last post by:
When I do this: foreach( Button btn in myForm.Controls ) An exception is raised when no buttons exist. I would simply expect execution to continue after the foreach loop (just as would be the...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
3
by: xmail123 | last post by:
Why does this code work? I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. I have two questions about why this code...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.