[PHP] Re: does array_slice not work on multidimensional arrays?

2009-08-08 Thread Nisse Engström
On Fri, 7 Aug 2009 17:06:45 -0600, John Butler wrote:

 does array_slice not work on multidimensional arrays?
 
 I have one multidimensional array, and when I run it thru'
 array_slice() nothing happens, that I can tell.
 
 If it does not work on multidimensional arrays, what is the *simplest*  
 workaround?  (I am already working past what was budgeted for this  
 this script..  so I really want to avoid anything but the most newbie  
 of hacks to achieve this.)
 
 //PHP Version 4.3.9

Works for me in PHP 5:


cat a.php

?php

  echo phpversion(), \n;
  $a1 = array (array (1,2,3,4), array ('a','b','c'), array (true, false));
  $a2 = array_slice ($a1, 1, 2);
  var_dump ($a2);

php5 a.php

5.2.5
array(2) {
  [0]=
  array(3) {
[0]=
string(1) a
[1]=
string(1) b
[2]=
string(1) c
  }
  [1]=
  array(2) {
[0]=
bool(true)
[1]=
bool(false)
  }
}


When I first looked into array_slice, I was hoping that it
could be used to extract a column from a multi-dimensional
array, e.g.:

  array_slice ($a1, 1)

would result in:

  array (2, 'b', false)


This would be very useful at times, but doesn't seem
to be possible.


/Nisse

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: does array_slice not work on multidimensional arrays?

2009-08-08 Thread John Butler

does array_slice not work on multidimensional arrays?

I have one multidimensional array, and when I run it thru'
array_slice() nothing happens, that I can tell.

If it does not work on multidimensional arrays, what is the  
*simplest*

workaround?  (I am already working past what was budgeted for this
this script..  so I really want to avoid anything but the most newbie
of hacks to achieve this.)

//PHP Version 4.3.9


Works for me in PHP 5:



cat a.php


?php

 echo phpversion(), \n;
 $a1 = array (array (1,2,3,4), array ('a','b','c'), array (true,  
false));

 $a2 = array_slice ($a1, 1, 2);
 var_dump ($a2);


php5 a.php


5.2.5
array(2) {
 [0]=
 array(3) {
   [0]=
   string(1) a
   [1]=
   string(1) b
   [2]=
   string(1) c
 }
 [1]=
 array(2) {
   [0]=
   bool(true)
   [1]=
   bool(false)
 }
}


It seems clear to me, but do you also agree that my evidence is that  
array_slice() does NOT work on multi-dimmed arrays on v.4.3.9?

Here is the evidence:

this:

var_dump($BuildPerUniqueDateArray);
echo\n\n~\n\n;
array_slice($BuildPerUniqueDateArray, 1, 2);
var_dump($BuildPerUniqueDateArray);

returns:
array(12) {
  [2009-08-08]=
  array(2) {
[t7solar_landing]=
string(1) 2
[aweber_7solar_aw]=
string(1) 1
  }
  [2009-08-07]=
  array(3) {
[t7solar_landing]=
string(1) 2
[aweber_7solar_aw]=
string(1) 1
[aweber_7solar_confirm]=
string(1) 1
  }
  [2009-08-06]=
  array(3) {
[aweber_7solar_confirm]=
string(1) 5
[t7solar_landing]=
string(1) 6
[aweber_7solar_aw]=
string(1) 3
  }
  [2009-08-05]=
  array(3) {
[aweber_7solar_confirm]=
string(1) 1
[t7solar_landing]=
string(1) 3
[aweber_7solar_aw]=
string(1) 4
  }
...
~

array(12) {
  [2009-08-08]=
  array(2) {
[t7solar_landing]=
string(1) 2
[aweber_7solar_aw]=
string(1) 1
  }
  [2009-08-07]=
  array(3) {
[t7solar_landing]=
string(1) 2
[aweber_7solar_aw]=
string(1) 1
[aweber_7solar_confirm]=
string(1) 1
  }
  [2009-08-06]=
  array(3) {
[aweber_7solar_confirm]=
string(1) 5
[t7solar_landing]=
string(1) 6
[aweber_7solar_aw]=
string(1) 3
  }
  [2009-08-05]=
  array(3) {
[aweber_7solar_confirm]=
string(1) 1
[t7solar_landing]=
string(1) 3
[aweber_7solar_aw]=
string(1) 4
  }
...


John Butler (Govinda)
govinda.webdnat...@gmail.com




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: does array_slice not work on multidimensional arrays?

2009-08-08 Thread Nisse Engström
On Sat, 8 Aug 2009 08:29:36 -0600, John Butler wrote:

 It seems clear to me, but do you also agree that my evidence is that  
 array_slice() does NOT work on multi-dimmed arrays on v.4.3.9?
 Here is the evidence:
 
 this:
 
 var_dump($BuildPerUniqueDateArray);
 echo\n\n~\n\n;
 array_slice($BuildPerUniqueDateArray, 1, 2);
 var_dump($BuildPerUniqueDateArray);

It seems clear to me that you are dumping /the same/
array twice.


/Nisse

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: does array_slice not work on multidimensional arrays?

2009-08-08 Thread John Butler

It seems clear to me, but do you also agree that my evidence is that
array_slice() does NOT work on multi-dimmed arrays on v.4.3.9?
Here is the evidence:

this:

var_dump($BuildPerUniqueDateArray);
echo\n\n~\n\n;
array_slice($BuildPerUniqueDateArray, 1, 2);
var_dump($BuildPerUniqueDateArray);


It seems clear to me that you are dumping /the same/
array twice.



Aha.  I see  now.   I had to *assign* the results of the array_slice  
to a [new] array and then use that ...
(trying to do something and expecting the results to stick even  
without assigning them to a var,  bites me every week or so.  I'll get  
it through my old habits-from-other-languages skull eventually.)


Thank you Nisse,

-Govinda

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: does array_slice not work on multidimensional arrays?

2009-08-08 Thread Ashley Sheridan
On Sat, 2009-08-08 at 12:18 -0400, Robert Cummings wrote:

 John Butler wrote:
  It is annoying working with arrays sometimes, as some functions work  
  on
  the actual array, while others return the results of working on the
  array without changing anything. Is there any particular reason for
  that?
  
  ...you mean what are the reasons for the different ways the arrays work?
  
  Then you must be asking the list in general (not me ;-))
 
 Efficiency is often one of the reasons. Do you really want the overhead 
 incurred by returning a copy of a 5 member. Most of the sort 
 functions work on the array itself.
 
 By working on the array itself, PHP offers you, the programmer, the 
 choice of incurring the overhead since you can copy it before sorting.
 
 Cheers,
 Rob.
 -- 
 http://www.interjinn.com
 Application and Templating Framework for PHP
 

That makes sense.

Thanks,
Ash
http://www.ashleysheridan.co.uk


Re: [PHP] Re: does array_slice not work on multidimensional arrays?

2009-08-08 Thread Nisse Engström
On Sat, 08 Aug 2009 16:05:20 +0100, Ashley Sheridan wrote:

 It is annoying working with arrays sometimes, as some functions work on
 the actual array, while others return the results of working on the
 array without changing anything. Is there any particular reason for
 that?

Well in this case [array_slice()], I'd say you most probably
want to keep the original array intact. In other cases,
such as sort() or array_push(), you really want to modify
the array you pass to them.

The implementors have probably tried to pick the most
reasonable method for each function, and it seems to work
out well for me. But I'm only guessing.


/Nisse

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php