RE: [PHP] Pushing array onto array
-Original Message- From: Curt Zirzow [mailto:[EMAIL PROTECTED] Sent: 02 August 2003 23:07 * Thus wrote Andrew Brampton ([EMAIL PROTECTED]): Well I just coded up a very small example, and it pushing 1 array into the other... Check out: http://81.102.229.151/push.php and http://81.102.229.151/push.phps It works exactly how it should... However really the array isn't a 2 dimensional one, since PHP doesn't have them, its rather a array of arrays which is roughly the same thing (and something not to worry about)... A 2 demensional array is an array of arrays. No, it isn't. A true 2-dimensional array is completely orthogonal -- anything you can do row-wise you can automatically also do column-wise, without having to write any special functionality. An array of arrays, by its very nature, doesn't have columns as such, so if you want to work with, e.g. $arr[*][1] you have to have special functions to do so. There are other differences, but in a nutshell that's essentially it. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Pushing array onto array
-Original Message- From: Hank TT [mailto:[EMAIL PROTECTED] Sent: 03 August 2003 06:03 Well, I might have been more specific about their example, since not everyone has the book. An excerpt below (so I don't need to retype all the names of characters and foul creatures from the Lord of the Rings): --- $arr1 = array('G', 'R', 'Sr'); $arr2 = array('N', 'Su', 'O'); $arr3 = array_push($arr1, $arr2); print $arr3[3][1]; //prints Su --- Weird usage Well, it wouldn't be weird if it were correct, but it's not, it's wrong! Having executed this, the result should be: $arr3 == 4 $arr1 == array('G', 'R', 'Sr', array('N', 'Su', 'O')) so $arr3[3][1] doesn't even exist, since $arr3 is a simple scalar integer -- but on the other hand, $arr1[3][1] is indeed 'Su'. So it appears there's a typo -- theirs or yours? Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pushing array onto array
James, My question had more to do with why the authors would give this usage when array_push is not documented to behave so (as you correctly described). Were they describing an earlier version of array_push? A simple buggy example? A typo perhaps? Since the authors Hughes and Zmievski are big names in PHP, though mortals like you and me, I'm inclined to think they were thinking about something. - Original Message - From: Jason Wong [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Sunday, August 03, 2003 1:27 AM Subject: Re: [PHP] Pushing array onto array On Sunday 03 August 2003 13:02, Hank TT wrote: Well, I might have been more specific about their example, since not everyone has the book. An excerpt below (so I don't need to retype all the names of characters and foul creatures from the Lord of the Rings): --- $arr1 = array('G', 'R', 'Sr'); $arr2 = array('N', 'Su', 'O'); $arr3 = array_push($arr1, $arr2); print $arr3[3][1]; //prints Su --- I don't see how your example could work. array_push() does NOT return an array. The results of array_push() is placed in the first argument -- in this case $arr1. Weird usage Think of it like this -- it adds the 2nd array onto the 1st array. It does NOT add the *contents* of the 2nd array to the 1st array -- for that you would probably use array_merge(). Do a print_r($arr1), it might help you visualise what happens. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* It is now pitch dark. If you proceed, you will likely fall into a pit. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pushing array onto array
Well I just coded up a very small example, and it pushing 1 array into the other... Check out: http://81.102.229.151/push.php and http://81.102.229.151/push.phps It works exactly how it should... However really the array isn't a 2 dimensional one, since PHP doesn't have them, its rather a array of arrays which is roughly the same thing (and something not to worry about)... If you need more help just email me Andrew - Original Message - From: Hank TT [EMAIL PROTECTED] To: php-general [EMAIL PROTECTED] Sent: Saturday, August 02, 2003 7:58 AM Subject: [PHP] Pushing array onto array In comparing Perl's push to array_push in PHP Developer's Cookbook (2/e, p. 76), the authors note that pushing an array onto another produces a two-dimensional array. However, I have not been able to reproduce their example result, and php.net/array_push does not document this behavior. Just curious. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pushing array onto array
* Thus wrote Andrew Brampton ([EMAIL PROTECTED]): Well I just coded up a very small example, and it pushing 1 array into the other... Check out: http://81.102.229.151/push.php and http://81.102.229.151/push.phps It works exactly how it should... However really the array isn't a 2 dimensional one, since PHP doesn't have them, its rather a array of arrays which is roughly the same thing (and something not to worry about)... A 2 demensional array is an array of arrays. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pushing array onto array
* Thus wrote Andrew Brampton ([EMAIL PROTECTED]): No there is a difference... Take the example [ [a, b, c] , [ d, e, f] ] In a 2 dimensional array those values would be stored adjacent in memory... ie a b c d e f In a Array of Arrays, the first and 2nd elements would be pointers, to where the [a, b, c], and [d, e, f] arrays are actually kept... ie 0 1 2 3 4 5 6 7 2 3 a b c d e f but this can all depend on the compiler, and the language construct... But its best to recognise a difference even if its really small... how they are aligned in memory doesn't make them different in what they are. Either way you still have a 2 dimential matrix. and in PHP it can get even more confusing with its non strict typing, and un-fixed size arrays etc... Some would argue this. PHP makes it easier for the programmer, so he doesn't have to worry about type differences, memory allocation and lower level access. The programmer just needs to assign a variable and poof... it is created. Andrew - Original Message - From: Curt Zirzow [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, August 02, 2003 11:06 PM Subject: Re: [PHP] Pushing array onto array * Thus wrote Andrew Brampton ([EMAIL PROTECTED]): It works exactly how it should... However really the array isn't a 2 dimensional one, since PHP doesn't have them, its rather a array of arrays which is roughly the same thing (and something not to worry about)... A 2 demensional array is an array of arrays. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pushing array onto array
Well, I might have been more specific about their example, since not everyone has the book. An excerpt below (so I don't need to retype all the names of characters and foul creatures from the Lord of the Rings): --- $arr1 = array('G', 'R', 'Sr'); $arr2 = array('N', 'Su', 'O'); $arr3 = array_push($arr1, $arr2); print $arr3[3][1]; //prints Su --- Weird usage - Original Message - From: Andrew Brampton [EMAIL PROTECTED] To: Hank TT [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, August 02, 2003 2:09 PM Subject: Re: [PHP] Pushing array onto array Well I just coded up a very small example, and it pushing 1 array into the other... Check out: http://81.102.229.151/push.php and http://81.102.229.151/push.phps It works exactly how it should... However really the array isn't a 2 dimensional one, since PHP doesn't have them, its rather a array of arrays which is roughly the same thing (and something not to worry about)... If you need more help just email me Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pushing array onto array
On Sunday 03 August 2003 13:02, Hank TT wrote: Well, I might have been more specific about their example, since not everyone has the book. An excerpt below (so I don't need to retype all the names of characters and foul creatures from the Lord of the Rings): --- $arr1 = array('G', 'R', 'Sr'); $arr2 = array('N', 'Su', 'O'); $arr3 = array_push($arr1, $arr2); print $arr3[3][1]; //prints Su --- I don't see how your example could work. array_push() does NOT return an array. The results of array_push() is placed in the first argument -- in this case $arr1. Weird usage Think of it like this -- it adds the 2nd array onto the 1st array. It does NOT add the *contents* of the 2nd array to the 1st array -- for that you would probably use array_merge(). Do a print_r($arr1), it might help you visualise what happens. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* It is now pitch dark. If you proceed, you will likely fall into a pit. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php