I'm trying to understand/debug a bit of PHP, the code is as follows:- if (isset($data['date'])) { if (is_array($data['date'])) { foreach($data['date'] as $key => $date) { $date = $this->_convertDate(trim($date)); if (!$date) unset($data['date'][$key]); else $data['date'][$key] = $date; dbglog($data, "Converted data"); } } else { unset($data['date']); } } What's happening is that it always ends up with nothing in $data (what's reported by that dbglog()). I'm confused by what happens with that foreach as, as far as I can see, $data['date'] is just what I would call a list in python, it's not an array indexed by values it's just a sequential array. So, in the foreach loop $key gets the values from the $date array and $date is empty (confirmed by further dbglog()). Am I understanding this right? If I create an array:- $aa = array('abc', 'def', 'ghi', 'jkl'); Then do:- foreach $aa as $x => $y { // what do I get for $x and $y here? } Sorry this is a bit confused/muddled, I'm definitely not a PHP programmer. -- Chris Green
Hi Chris On Tue, Feb 9, 2010 at 8:36 PM, Chris G <cl@isbd.net> wrote:
I'm trying to understand/debug a bit of PHP, the code is as follows:-
if (isset($data['date'])) { if (is_array($data['date'])) { foreach($data['date'] as $key => $date) { $date = $this->_convertDate(trim($date)); if (!$date) unset($data['date'][$key]); else $data['date'][$key] = $date; dbglog($data, "Converted data"); } } else { unset($data['date']); } }
What's happening is that it always ends up with nothing in $data (what's reported by that dbglog()).
I'm confused by what happens with that foreach as, as far as I can see, $data['date'] is just what I would call a list in python, it's not an array indexed by values it's just a sequential array.
So, in the foreach loop $key gets the values from the $date array and $date is empty (confirmed by further dbglog()).
Am I understanding this right? If I create an array:-
$aa = array('abc', 'def', 'ghi', 'jkl');
Then do:-
foreach $aa as $x => $y { // what do I get for $x and $y here? }
The $x would be the "key" into the array e.g. $aa[0] and the $y is the value of the key index e.g. $aa[0] = 'abc' not sure if that helps ? Ian www:www.codingfrieds.com
Sorry this is a bit confused/muddled, I'm definitely not a PHP programmer.
-- Chris Green
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
I think Ian is correct but also you try going mad with debugging power? foreach($data['date'] as $key => $date) { echo $date; $date = $this->_convertDate(trim($date)); echo $date; if (!$date) unset($data['date'][$key]); echo $data['date'][$key]; else $data['date'][$key] = $date; echo $date; dbglog($data, "Converted data"); } A silly idea but sometimes it helps me to see the $variable being carried along through each line so I can find the problem line. -- Regards, James ;) Marie von Ebner-Eschenbach - "Even a stopped clock is right twice a day." - http://www.brainyquote.com/quotes/authors/m/marie_von_ebnereschenbac.html
On Tue, Feb 09, 2010 at 09:01:19PM +0000, James Bensley wrote:
I think Ian is correct but also you try going mad with debugging power?
foreach($data['date'] as $key => $date) { echo $date; $date = $this->_convertDate(trim($date)); echo $date; if (!$date) unset($data['date'][$key]); echo $data['date'][$key]; else $data['date'][$key] = $date; echo $date; dbglog($data, "Converted data"); }
A silly idea but sometimes it helps me to see the $variable being carried along through each line so I can find the problem line.
Yes, I ended up doing almost exactly this, it works as it should with $key getting set ti 1, 2, 3, 4, etc. The error in the code I was trying to debug was *something* to do with foreach not workign with arrays of one entry (or the one entry not making an array of what was supposed to be an array). -- Chris Green
Chris On Tue, Feb 9, 2010 at 10:03 PM, Chris G <cl@isbd.net> wrote:
On Tue, Feb 09, 2010 at 09:01:19PM +0000, James Bensley wrote:
I think Ian is correct but also you try going mad with debugging power?
foreach($data['date'] as $key => $date) { echo $date; $date = $this->_convertDate(trim($date)); echo $date; if (!$date) unset($data['date'][$key]); echo $data['date'][$key]; else $data['date'][$key] = $date; echo $date; dbglog($data, "Converted data"); }
A silly idea but sometimes it helps me to see the $variable being carried along through each line so I can find the problem line.
Yes, I ended up doing almost exactly this, it works as it should with $key getting set ti 1, 2, 3, 4, etc.
The error in the code I was trying to debug was *something* to do with foreach not workign with arrays of one entry (or the one entry not making an array of what was supposed to be an array).
Great that you sorted it :).. I do find php a nice language at times. Ian www: www.codingfriends.com
-- Chris Green
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
On Tue, Feb 09, 2010 at 10:12:52PM +0000, Ian Porter wrote:
Chris
On Tue, Feb 9, 2010 at 10:03 PM, Chris G <cl@isbd.net> wrote:
On Tue, Feb 09, 2010 at 09:01:19PM +0000, James Bensley wrote:
I think Ian is correct but also you try going mad with debugging power?
foreach($data['date'] as $key => $date) { echo $date; $date = $this->_convertDate(trim($date)); echo $date; if (!$date) unset($data['date'][$key]); echo $data['date'][$key]; else $data['date'][$key] = $date; echo $date; dbglog($data, "Converted data"); }
A silly idea but sometimes it helps me to see the $variable being carried along through each line so I can find the problem line.
Yes, I ended up doing almost exactly this, it works as it should with $key getting set ti 1, 2, 3, 4, etc.
The error in the code I was trying to debug was *something* to do with foreach not workign with arrays of one entry (or the one entry not making an array of what was supposed to be an array).
Great that you sorted it :)..
I do find php a nice language at times.
I much prefer python but PHP is OK. -- Chris Green
I do find php a nice language at times.
I much prefer python but PHP is OK.
--
Python, that is one language, I never really did check out in anger as such. C++/Java/C#/SQL/PHP are the main ones that I know.. What would you say is a good thing about python ? so that I could look into it, to wet my appetite as such. Ian www:www.codingfriends.com
On Tue, Feb 09, 2010 at 10:33:20PM +0000, Ian Porter wrote:
I do find php a nice language at times.
I much prefer python but PHP is OK.
--
Python, that is one language, I never really did check out in anger as such. C++/Java/C#/SQL/PHP are the main ones that I know.. What would you say is a good thing about python ? so that I could look into it, to wet my appetite as such.
It's just very straightforward and 'clean'. I'm a C/C++ programmer of many years with a bit of SQL and other knowledge. I've only used python for the past two or three years but it's my language of choice now for anything in between a trivial shell script and a full blown C/C++ development. -- Chris Green
Python, that is one language, I never really did check out in anger as such. C++/Java/C#/SQL/PHP are the main ones that I know.. What would you say is a good thing about python ? so that I could look into it, to wet my appetite as such.
It's just very straightforward and 'clean'. I'm a C/C++ programmer of many years with a bit of SQL and other knowledge. I've only used python for the past two or three years but it's my language of choice now for anything in between a trivial shell script and a full blown C/C++ development.
Sounds interesting :). shall have to check it out.. any websites that you would recommend ? Ian www:www.codingfriends.com
-- Chris Green
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
On Thu, Feb 11, 2010 at 09:52:02AM +0000, Ian Porter wrote:
Python, that is one language, I never really did check out in anger as such. C++/Java/C#/SQL/PHP are the main ones that I know.. What would you say is a good thing about python ? so that I could look into it, to wet my appetite as such.
It's just very straightforward and 'clean'. I'm a C/C++ programmer of many years with a bit of SQL and other knowledge. I've only used python for the past two or three years but it's my language of choice now for anything in between a trivial shell script and a full blown C/C++ development.
Sounds interesting :). shall have to check it out.. any websites that you would recommend ?
http://python.org/ has just about all you need to know (and a great deal you don't need to know!). -- Chris Green
http://python.org/ has just about all you need to know (and a great deal you don't need to know!).
Cheers Ian
-- Chris Green
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
On 11/02/2010, Ian Porter <ian@codingfriends.com> wrote:
http://python.org/ has just about all you need to know (and a great deal you don't need to know!).
This is also great, which assumes the reader knows how to program in at least another language: http://diveintopython.org/toc/index.html Srdjan
On 11 Feb 2010, at 11:08, Srdjan Todorovic wrote:
This is also great, which assumes the reader knows how to program in at least another language:
Agreed, diveintopython is a great resource. -- David Reynolds david@reynoldsfamily.org.uk
participants (5)
-
Chris G -
David Reynolds -
Ian Porter -
James Bensley -
Srdjan Todorovic