RES: [PHP] Array help.

2012-10-24 Thread Samuel Lopes Grigolato
Could you try changing this:

if($groupTest != FALSE) {

to this:

if($groupTest !== FALSE) {

?

-Mensagem original-
De: Paul Halliday [mailto:paul.halli...@gmail.com] 
Enviada em: quarta-feira, 24 de outubro de 2012 15:38
Para: PHP-General
Assunto: [PHP] Array help.

I am processing v4IP's and what I want to do is a prefix substitution if the
3rd octet matches a predefined list $groupMappings. I went down this  path
and it isn't working as expected. Drawing a blank on this one. Why does 40
miss the comparison?

$hostname = Z;
$ips = array('10.1.40.1','10.1.41.1','10.1.1.1','10.1.40.1','10.9.1.1');

foreach ($ips as $ip) {

$groupMappings = array('40' ='A','41' ='B','1' ='C');

$ocTest = explode(., $ip);
$groupKeys = array_keys($groupMappings);
$groupTest = array_search($ocTest[2], $groupKeys);

if($groupTest != FALSE) {
$hostGroup = $groupMappings[$groupKeys[$groupTest]];
echo Hit! $ip : $hostname : $hostGroup\n;
} else {
$hostGroup = substr($hostname, 0,2);
echo Miss! $ip : $hostname : $hostGroup\n;
}
}

Miss! 10.1.40.1 : Z : Z
Hit! 10.1.41.1 : Z : B
Hit! 10.1.1.1 : Z : C
Miss! 10.1.40.1 : Z : Z
Hit! 10.9.1.1 : Z : C

Thanks!

--
Paul Halliday
http://www.pintumbler.org/

--
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] Array help.

2012-10-24 Thread Paul Halliday
On Wed, Oct 24, 2012 at 2:40 PM, Samuel Lopes Grigolato
samuel.grigol...@gmail.com wrote:
 Could you try changing this:

 if($groupTest != FALSE) {

 to this:

 if($groupTest !== FALSE) {

 ?

Hah. Perfect! Thanks.


 -Mensagem original-
 De: Paul Halliday [mailto:paul.halli...@gmail.com]
 Enviada em: quarta-feira, 24 de outubro de 2012 15:38
 Para: PHP-General
 Assunto: [PHP] Array help.

 I am processing v4IP's and what I want to do is a prefix substitution if the
 3rd octet matches a predefined list $groupMappings. I went down this  path
 and it isn't working as expected. Drawing a blank on this one. Why does 40
 miss the comparison?

 $hostname = Z;
 $ips = array('10.1.40.1','10.1.41.1','10.1.1.1','10.1.40.1','10.9.1.1');

 foreach ($ips as $ip) {

 $groupMappings = array('40' ='A','41' ='B','1' ='C');

 $ocTest = explode(., $ip);
 $groupKeys = array_keys($groupMappings);
 $groupTest = array_search($ocTest[2], $groupKeys);

 if($groupTest != FALSE) {
 $hostGroup = $groupMappings[$groupKeys[$groupTest]];
 echo Hit! $ip : $hostname : $hostGroup\n;
 } else {
 $hostGroup = substr($hostname, 0,2);
 echo Miss! $ip : $hostname : $hostGroup\n;
 }
 }

 Miss! 10.1.40.1 : Z : Z
 Hit! 10.1.41.1 : Z : B
 Hit! 10.1.1.1 : Z : C
 Miss! 10.1.40.1 : Z : Z
 Hit! 10.9.1.1 : Z : C

 Thanks!

 --
 Paul Halliday
 http://www.pintumbler.org/

 --
 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] Array help.

2012-10-24 Thread Ford, Mike
 From: Paul Halliday [paul.halli...@gmail.com]
 Sent: 24 October 2012 18:38
 To: PHP-General
 Subject: [PHP] Array help.
 
 $groupMappings = array('40' ='A','41' ='B','1' ='C');
 
 $ocTest = explode(., $ip);
 $groupKeys = array_keys($groupMappings);
 $groupTest = array_search($ocTest[2], $groupKeys);
 
 if($groupTest != FALSE) {

I think you're making a little bit of a meal of this. My initial thoughts
included pointing you at array_key_exists() (and, why on earth
have you got $ocTest[2] in quotes?), but then I realised if I were
writing this I'd probably just use isset(), thus:

   $ocTest = explode(., $ip);
   if (isset($groupMappings[$ocTest[2]])):
  // success
   else:
  // fail
   endif;

Hope this helps!


Cheers!

Mike

-- 
Mike Ford, Electronic Information Developer, Libraries and Learning Information
Portland PD507, City Campus, Leeds Metropolitan University,
Portland Way, LEEDS, LS1 3HE, United Kingdom
E: m.f...@leedsmet.ac.uk T: +44 113 812 4730

To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] Array unset()

2012-09-24 Thread Ashley Sheridan


Ken Robinson kenrb...@rbnsn.com wrote:

At 08:50 PM 9/23/2012, Ron Piggott wrote:

I am wondering if there is a way to remove from
an array where the value is 0 (“zero”)

Array example:

$total_points_awarded = array(  1 = 17, 3 = 14, 4 = 0, 5 = 1, 6 =
0 );

In this example I would like to remove element # 4 and # 6.

The “key” ( 1,3,4,5,6 ) represents the
member’s account #.  It is an auto_increment value in a mySQL table
The “value” ( 17,14,0,1,0 ) represents their score.

The application for this is a list of the top
users.  If someone has 0 points I don’t want to include them.

Any thoughts?  Any help is appreciated.

Look at array_filter()  ... http://php.net/array_filter

?php
$total_points_awarded = array(  1 = 17, 3 = 14, 4 = 0, 5 = 1, 6 =
0 );
print_r(array_filter($total_points_awarded));


Ken


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

Wouldn't it be far easier to do this at the database level in the query?

--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

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



[PHP] Array unset()

2012-09-23 Thread Ron Piggott

I am wondering if there is a way to remove from an array where the value is 0 
(“zero”)

Array example:

$total_points_awarded = array(  1 = 17, 3 = 14, 4 = 0, 5 = 1, 6 = 0 );

In this example I would like to remove element # 4 and # 6.  

The “key” ( 1,3,4,5,6 ) represents the member’s account #.  It is an 
auto_increment value in a mySQL table
The “value” ( 17,14,0,1,0 ) represents their score.

The application for this is a list of the top users.  If someone has 0 points I 
don’t want to include them.  

Any thoughts?  Any help is appreciated.  

Ron

Ron Piggott



www.TheVerseOfTheDay.info 


Re: [PHP] Array unset()

2012-09-23 Thread Ken Robinson

At 08:50 PM 9/23/2012, Ron Piggott wrote:

I am wondering if there is a way to remove from 
an array where the value is 0 (“zero”)


Array example:

$total_points_awarded = array(  1 = 17, 3 = 14, 4 = 0, 5 = 1, 6 = 0 );

In this example I would like to remove element # 4 and # 6.

The “key” ( 1,3,4,5,6 ) represents the 
member’s account #.  It is an auto_increment value in a mySQL table

The “value” ( 17,14,0,1,0 ) represents their score.

The application for this is a list of the top 
users.  If someone has 0 points I don’t want to include them.


Any thoughts?  Any help is appreciated.


Look at array_filter()  ... http://php.net/array_filter

?php
$total_points_awarded = array(  1 = 17, 3 = 14, 4 = 0, 5 = 1, 6 = 0 );
print_r(array_filter($total_points_awarded));


Ken 



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



Re: [PHP] PHP array unions

2012-09-14 Thread Matijn Woudt
Op 14 sep. 2012 07:51 schreef Adam Richardson simples...@gmail.com het
volgende:

 On Wed, Sep 12, 2012 at 2:37 PM, Sebastian Krebs krebs@gmail.com
wrote:
  Hi,
 
  In PHP the array is in fact a hash map, but especially it is _used_ for
  nearly everything map-, set-, ...-like thing. So in short: The is no
  operator or built-in function, that merges two arrays _and_ treat them
as
  set (instead of the hashmap, what they are). Your solution is the way
to go.

 Sure, I know about the underlying implementation. I was just hopeful
 because several of the array functions handle the maps differently
 depending on whether the keys are numeric or string or both.

 If I wanted to get cute, I could store the value in the key (e.g.,
 array('value 1' = 0, 'value 2' = 0, ...)), and that allows me to use
 the '+' operator. In spite of the nice performance benefits of this
 approach (leveraging the hashes benefits), the code that utilizes the
 arrays becomes quite clunky.

 Thanks,

 Adam


It doesn't need to be clunky.. just use array_flip and you've got the old
array again..

-Matijn


Re: [PHP] PHP array unions

2012-09-14 Thread Adam Richardson
On Fri, Sep 14, 2012 at 2:30 AM, Matijn Woudt tijn...@gmail.com wrote:

 It doesn't need to be clunky.. just use array_flip and you've got the old
 array again..


Well, array_flip has it's own potential issues (duplicate values are
lost, so my example of using zeros would not work.) I suppose I could
duplicate the keys as the values (e.g., array('value 1' = 'value 1',
'value 2' = 'value 2', ...).) Then, the keys would  allow me to
utilize the nice properties of hash maps whilst maintaining the
ability to work with the values as one normally does in typical array
functions.

Ex:

$a1 = array('apples' = 'apples', 'oranges' = 'oranges', 'pears' = 'pears');
$a2 = array('oranges' = 'oranges', 'kiwi' = 'kiwi');
// can use the union operator without any additional calls and the
performance is stellar
$a3 = $a1 + $a2
// can use the values of the array using the convention that the value
is what you expect to handle/manipulate
foreach ($a3 as $val) {
  echo $val
}

Here, the clunkiness is the redundancy in the array, but, as Claude
Shannon has demonstrated, redundancy isn't all bad :)

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com

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



Re: [PHP] PHP array unions

2012-09-13 Thread Adam Richardson
On Wed, Sep 12, 2012 at 2:37 PM, Sebastian Krebs krebs@gmail.com wrote:
 Hi,

 In PHP the array is in fact a hash map, but especially it is _used_ for
 nearly everything map-, set-, ...-like thing. So in short: The is no
 operator or built-in function, that merges two arrays _and_ treat them as
 set (instead of the hashmap, what they are). Your solution is the way to go.

Sure, I know about the underlying implementation. I was just hopeful
because several of the array functions handle the maps differently
depending on whether the keys are numeric or string or both.

If I wanted to get cute, I could store the value in the key (e.g.,
array('value 1' = 0, 'value 2' = 0, ...)), and that allows me to use
the '+' operator. In spite of the nice performance benefits of this
approach (leveraging the hashes benefits), the code that utilizes the
arrays becomes quite clunky.

Thanks,

Adam

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



Re: [PHP] PHP array unions

2012-09-12 Thread Sebastian Krebs

Hi,

In PHP the array is in fact a hash map, but especially it is _used_ for 
nearly everything map-, set-, ...-like thing. So in short: The is no 
operator or built-in function, that merges two arrays _and_ treat them 
as set (instead of the hashmap, what they are). Your solution is the way 
to go.


Regards,
Sebastian

Am 12.09.2012 17:10, schrieb Adam Richardson:

Hi!

So, PHP has the '+' array operator, which forms the union of arrays,
but it does so by key. What I'm looking for is a function that forms
the union of arrays based on value. Currently, I use code like the
following:

array_unique(array_merge($array1, $array2, $array3));

This is useful to me because I tend to program using functional
programming principles. Just want to make sure I'm not missing a core
function that already does this. If there's not a core function, I
might just build an array_union extension because the functionality is
so common in my codebases.

Thanks,

Adam




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



[PHP] array

2011-12-31 Thread saeed ahmed
how can you explain someone in a simplest and everyday use example of ARRAY.

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



Re: [PHP] array

2011-12-31 Thread Ashley Sheridan
On Sat, 2011-12-31 at 14:53 +0100, saeed ahmed wrote:

 how can you explain someone in a simplest and everyday use example of ARRAY.
 


The manual page explains it pretty succinctly. I don't think you'll get
more simple than this, as there is obvious prerequisite knowledge
assumed (i.e. that you know what a simple variable is, etc)
-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] array

2011-12-31 Thread Govinda
 how can you explain someone in a simplest and everyday use example of ARRAY.
 
 The manual page explains it pretty succinctly. I don't think you'll get
 more simple than this, as there is obvious prerequisite knowledge
 assumed (i.e. that you know what a simple variable is, etc)

Hi saeed,

Ash means this:

http://www.php.net/manual/en/language.types.array.php

See the examples in grey-colored boxes.

Arrays are just a collection of things.  Similar to when you assign one 
variable one value.. well with an array you assign many variables one value 
each.  But the reason you use an array is that instead of a bunch of separate 
variables, you want those variables to be part of a collection..  i.e. all 
those variables have something in common.. like for example you might use one 
array to describe the parts of a car, and another array to describe all the 
fruits in your kitchen.

You could do this:  (all separate independent variables)

$part1 = 'spark plug';
$part2 = 'rear-view mirror';
$part3 = 'steering wheel';
etc.

$fruit1 = 'apple';
$fruit2 = 'banana';
$fruit3 = 'orange';
etc.

...but (depending on your application, and the logic you use), it might make 
more sense to do something like this instead: (make arrays!)

(these, below, are arrays where the key is just a number which is automatically 
assigned.  You can also use another syntax (see the manual) to make arrays 
where you generate your own custom keys, and those keys can be strings instead 
of numbers.)

$arrParts = array('spark plug', 'rear-view mirror', 'steering wheel');

$arrFruits = array('apple', 'banana', 'orange');


To use the arrays, there are tons of functions:

http://www.php.net/manual/en/ref.array.php

:-)

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



Re: [PHP] Array has `trailing comma`, why not the same for function parameter list?

2011-10-31 Thread Daniel Brown
On Sun, Oct 30, 2011 at 08:47, Nam Gi VU nam.gi...@gmail.com wrote:
 It is convenient to have a trailing comma when defining an array - so as
 easy to add/remove code to add/remove an entry to the array

 array(
    'key00' = 'value00',
    'key01' = 'value01',
    'key02' = 'value02',
    ...
 )

 I suggest to PHP Development team to make it available in the syntax to
 have a leading comma
 array(
    ...
    , 'key00' = 'value00'
    , 'key01' = 'value01'
    , 'key02' = 'value02'
 )
 in such way, we can thought of the leading commas as the list bulletings.

 And the same things would be lovely to be applied also to function
 parameter list. I don't see why not :)

 What do you thing about my suggestion? Hope to hear from you!

    For feature requests such as this, please discuss it on the
Internals list (CC'd on this email) and suggest it via the bug tracker
at https://bugs.php.net/ (with the bug type as a Feature/Change
Request).

-- 
/Daniel P. Brown
Network Infrastructure Manager
http://www.php.net/

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



[PHP] Novice: PHP array by reference question (by C++ programmer)

2011-10-31 Thread Manish Gupta
I have a class that takes as input, an array by reference and stores it in a 
member variable. A method in this class later modifies the member variable 
(which contains reference to the array). When I access the local variable 
that was passed by reference to the constructor of this class object, I see 
the local variable hasn't changed at all. I think code will describe what 
words may not have:


?php

class foo
{
   public $_bar;
   public function  __construct( $bar)
   {
   $this-_bar = $bar;
   }

   function do_your_thing()
   {
   $temp = array(
   'One' = 1,
   'Two' = 2
   );

   $this-_bar[] = $temp;

   echo('from Do_your_thing: ');
   print_r($this-_bar);   //  [1]
   }
}

$abc = array();
$var = new foo($abc);
$var-do_your_thing();

echo('from main [local variable]: ');
print_r($abc);   //  [2]

echo('from main: [object member variable] ');
print_r($var-_bar);//  [3]
?

I expected the output from [1], [2] and [3] to be the same. But instead I 
get the following:


from Do_your_thing: Array
(
   [0] = Array
   (
   [One] = 1
   [Two] = 2
   )

)
from main [local variable]: Array
(
)
from main: [object member variable] Array
(
   [0] = Array
   (
   [One] = 1
   [Two] = 2
   )

)

What am I missing? Please help, I'm new to PHP and this is really blocking 
me.


Thanks
M@nish 



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



[PHP] Novice: PHP array by reference question (by C++ programmer)

2011-10-31 Thread Manish Gupta

I have a class that takes as input, an array by reference and stores it in a
member variable. A method in this class later modifies the member variable
(which contains reference to the array). When I access the local variable
that was passed by reference to the constructor of this class object, I see
the local variable hasn't changed at all. I think code will describe what
words may not have:

?php

class foo
{
   public $_bar;
   public function  __construct( $bar)
   {
   $this-_bar = $bar;
   }

   function do_your_thing()
   {
   $temp = array(
   'One' = 1,
   'Two' = 2
   );

   $this-_bar[] = $temp;

   echo('from Do_your_thing: ');
   print_r($this-_bar);   //  [1]
   }
}

$abc = array();
$var = new foo($abc);
$var-do_your_thing();

echo('from main [local variable]: ');
print_r($abc);   //  [2]

echo('from main: [object member variable] ');
print_r($var-_bar);//  [3]
?

I expected the output from [1], [2] and [3] to be the same. But instead I
get the following:

from Do_your_thing: Array
(
   [0] = Array
   (
   [One] = 1
   [Two] = 2
   )

)
from main [local variable]: Array
(
)
from main: [object member variable] Array
(
   [0] = Array
   (
   [One] = 1
   [Two] = 2
   )

)

What am I missing? Please help, I'm new to PHP and this is really blocking
me.

Thanks
M@nish


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



[PHP] Novice: PHP array by reference question (by C++ programmer)

2011-10-31 Thread Louis Huppenbauer
-- Forwarded message --
From: Louis Huppenbauer louis.huppenba...@gmail.com
Date: 2011/10/31
Subject: Re: [PHP] Novice: PHP array by reference question (by C++
programmer)
To: Manish Gupta gman...@gmail.com


You have to assign the value by reference too

public function  __construct( $bar)
  {
  $this-_bar = $bar;
  }


2011/10/31 Manish Gupta gman...@gmail.com

  I have a class that takes as input, an array by reference and stores it
 in a
 member variable. A method in this class later modifies the member variable
 (which contains reference to the array). When I access the local variable
 that was passed by reference to the constructor of this class object, I see
 the local variable hasn't changed at all. I think code will describe what
 words may not have:

 ?php

 class foo
 {
   public $_bar;
   public function  __construct( $bar)
   {
   $this-_bar = $bar;
   }

   function do_your_thing()
   {
   $temp = array(
   'One' = 1,
   'Two' = 2
   );

   $this-_bar[] = $temp;

   echo('from Do_your_thing: ');
   print_r($this-_bar);   //  [1]
   }
 }

 $abc = array();
 $var = new foo($abc);
 $var-do_your_thing();

 echo('from main [local variable]: ');
 print_r($abc);   //  [2]

 echo('from main: [object member variable] ');
 print_r($var-_bar);//  [3]
 ?

 I expected the output from [1], [2] and [3] to be the same. But instead I
 get the following:

 from Do_your_thing: Array
 (
   [0] = Array
   (
   [One] = 1
   [Two] = 2
   )

 )
 from main [local variable]: Array
 (
 )
 from main: [object member variable] Array
 (
   [0] = Array
   (
   [One] = 1
   [Two] = 2
   )

 )

 What am I missing? Please help, I'm new to PHP and this is really blocking
 me.

 Thanks
 M@nish


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




[PHP] Array has `trailing comma`, why not the same for function parameter list?

2011-10-30 Thread Nam Gi VU
It is convenient to have a trailing comma when defining an array - so as
easy to add/remove code to add/remove an entry to the array

array(
'key00' = 'value00',
'key01' = 'value01',
'key02' = 'value02',
...
)

I suggest to PHP Development team to make it available in the syntax to
have a leading comma
array(
...
, 'key00' = 'value00'
, 'key01' = 'value01'
, 'key02' = 'value02'
)
in such way, we can thought of the leading commas as the list bulletings.

And the same things would be lovely to be applied also to function
parameter list. I don't see why not :)

What do you thing about my suggestion? Hope to hear from you!

-- 
Nam


Re: [PHP] Array has `trailing comma`, why not the same for function parameter list?

2011-10-30 Thread Jeremiah Dodds
On Sun, Oct 30, 2011 at 8:47 AM, Nam Gi VU nam.gi...@gmail.com wrote:
 It is convenient to have a trailing comma when defining an array - so as
 easy to add/remove code to add/remove an entry to the array

 array(
    'key00' = 'value00',
    'key01' = 'value01',
    'key02' = 'value02',
    ...
 )

 I suggest to PHP Development team to make it available in the syntax to
 have a leading comma
 array(
    ...
    , 'key00' = 'value00'
    , 'key01' = 'value01'
    , 'key02' = 'value02'
 )
 in such way, we can thought of the leading commas as the list bulletings.

 And the same things would be lovely to be applied also to function
 parameter list. I don't see why not :)

 What do you thing about my suggestion? Hope to hear from you!


WRT functions: while I could see how this would seem like a good idea
because of the symmetry in what's going on at a very high level of
abstraction, I would be against including it in a language. I don't
particularly like it for arrays, but it does make it easy to add items
into an array, which happens pretty often in development, especially
when writing exploratory code. Function signatures, on the other hand,
probably aren't changing nearly as often.

 At the very least, I wouldn't want to encourage people to make
functions with many parameters, as it's normally a sign of poor
design. This wouldn't explicitly do so, but would do so implicitly as
it's only real purpose is to make it easier to add things.

As far as the supporting-a-leading-comma thing goes, I'm pretty
ambivalent on it. I don't really like the style outside of Haskell
code, but that's just a preference.

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



[PHP] array problem

2011-09-09 Thread Marc Fromm
I am reading a csv file into an array. The csv file.

users.csv file contents:
w12345678,a
w23456789,b
w34567890,c

$csvfilename = users.csv;
$handle = fopen($csvfilename, r);
if($handle) {
while (($line = 
fgetcsv($handle, 1000, ,)) !== FALSE) {
$arrUsers[] = 
$line;
}
fclose($handle);
}

When I echo out the elements in the elements in the array $arrUsers I get:
Array
Array
Array

foreach ($arrUsers as $user){
echo $user . br /;
}

I can't figure out why the word Array is replacing the actual data.



Re: [PHP] array problem

2011-09-09 Thread Joshua Stoutenburg
The function fgetcsv() returns an array.

http://php.net/manual/en/function.fgetcsv.php


On Fri, Sep 9, 2011 at 9:00 AM, Marc Fromm marc.fr...@wwu.edu wrote:

 I am reading a csv file into an array. The csv file.

 users.csv file contents:
 w12345678,a
 w23456789,b
 w34567890,c

$csvfilename = users.csv;
 $handle = fopen($csvfilename, r);
if($handle) {
while (($line =
 fgetcsv($handle, 1000, ,)) !== FALSE) {
$arrUsers[]
 = $line;
}
fclose($handle);
}

 When I echo out the elements in the elements in the array $arrUsers I get:
 Array
 Array
 Array

 foreach ($arrUsers as $user){
echo $user . br /;
 }

 I can't figure out why the word Array is replacing the actual data.




Re: [PHP] array problem

2011-09-09 Thread Steve Staples
On Fri, 2011-09-09 at 16:00 +, Marc Fromm wrote:
 I am reading a csv file into an array. The csv file.
 
 users.csv file contents:
 w12345678,a
 w23456789,b
 w34567890,c
 
 $csvfilename = users.csv;
 $handle = fopen($csvfilename, r);
 if($handle) {
 while (($line = 
 fgetcsv($handle, 1000, ,)) !== FALSE) {
 $arrUsers[] = 
 $line;
 }
 fclose($handle);
 }
 
 When I echo out the elements in the elements in the array $arrUsers I get:
 Array
 Array
 Array
 
 foreach ($arrUsers as $user){
 echo $user . br /;
 }
 
 I can't figure out why the word Array is replacing the actual data.
 

Try print_r($arrUsers);

also, the $line is an array of the CSV, so you're storing an array,
within the array $arrUsers.

foreach ($arrUsers as $user){
foreach ($user as $val) {
echo $val . br /;
}
echo 'hr /';
}


-- 

Steve Staples
Web Application Developer
519.258.2333 x8414


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



Re: [PHP] array problem

2011-09-09 Thread Adam Balogh
hi,

try to use print_r or var_dump to echo compound data type


Re: [PHP] array problem

2011-09-09 Thread Ashley Sheridan
You are echoing out an array. If you use something like print_r() or var_dump() 
you will see the array elements
Thanks,
Ash
http://www.ashleysheridan.co.uk
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Marc Fromm marc.fr...@wwu.edu wrote:

I am reading a csv file into an array. The csv file.

users.csv file contents:
w12345678,a
w23456789,b
w34567890,c

$csvfilename = users.csv;
$handle = fopen($csvfilename, r);
if($handle) {
while (($line = fgetcsv($handle, 1000, ,)) !== FALSE) {
$arrUsers[] = $line;
}
fclose($handle);
}

When I echo out the elements in the elements in the array $arrUsers I get:
Array
Array
Array

foreach ($arrUsers as $user){
echo $user . br /;
}

I can't figure out why the word Array is replacing the actual data.



[PHP] Array Search

2011-03-25 Thread Ethan Rosenberg

Dear List -

Here is a code snippet:

$bla = array(g1 = $results[7][6],
   h1  = $results[7][7]);
print_r($bla);
$value = h1;
   $locate1 = array_search($value, $bla);
   echo This is locate ; print_r($locate1);
   if(in_array($value, $bla)) print_r($bla);

Neither the array_search or the in_array functions give any results. 
I have tried it with both h1 and h1;


$results[7][6]  = Wn;
$results[7][7]  =  Wr;

This is a chess board  where g1 and h1 are the coordinates and the 
results array contains the pieces at that coordinate.


What am I doing wrong?

Advice and comments please.

Thanks.

Ethan Rosenberg 




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



[PHP] Array of Error Codes: Key/Values

2011-03-16 Thread Brendan_Crowley
Hi,

I'm new to php and i'm looking to setup an array (or what work best) of codes 
and corresponding error strings, for example (pseudo code):
ERROR_CODES = array('-1' = 'Error opening file', '-2' = 'General File IO 
Error', '-3' = 'Database connection error');

Access these string values using the key codes (negative key values) to 
formulate a SoapFault, e.g.:
throw new SoapFault('-1', ERROR_CODES[-1], 'actor', 'detail', 'name', 'header');

How best to implement this in php?

Thanks,
Brendan


Re: [PHP] Array from one form to other?

2011-02-20 Thread Tamara Temple


On Feb 19, 2011, at 6:38 PM, Yogesh wrote:
I have two forms. One form helps read an input file into an array.  
And the

other form needs this array as an input.
I am able to read the input file into an array, but how do I pass it  
over to

the other form.

Both forms have PHP file as 'action'.


I don't entirely understand this; Dan Brown gave you solution to use  
curl to pass the array to the second form (do you mean script  
here?). That would certainly work, but I'm wondering if it wouldn't be  
more secure to spool out the array to a file from the first script  
after it has processed it, and then read it in to the second script  
when it starts. This assumes the two scripts are running on the same  
server, of course. The second script could be called after the first  
script finishes by using a header redirect. I'm curious about people's  
thought on the security and efficacy of different ways of sharing data  
between scripts when you're not using sessions.




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



Re: [PHP] Array from one form to other?

2011-02-20 Thread Yogesh
Hi Tamara,


I don't entirely understand this; Dan Brown gave you solution to use curl to
 pass the array to the second form (do you mean script here?). That would
 certainly work, but I'm wondering if it wouldn't be more secure to spool out
 the array to a file from the first script after it has processed it, and
 then read it in to the second script when it starts. This assumes the two
 scripts are running on the same server, of course. The second script could
 be called after the first script finishes by using a header redirect. I'm
 curious about people's thought on the security and efficacy of different
 ways of sharing data between scripts when you're not using sessions.



I am quite new to PHP. (I am having trouble to understand how HTML, PHP and
Javascript work together). Right now I just wanted to get the job done. But
if you can suggest me a more efficient way to do it, I will definitely look
into it.

Thanks,

-Yogesh


Re: [PHP] Array from one form to other?

2011-02-20 Thread Tamara Temple


On Feb 20, 2011, at 10:51 AM, Yogesh wrote:

I don't entirely understand this; Dan Brown gave you solution to use  
curl to pass the array to the second form (do you mean script  
here?). That would certainly work, but I'm wondering if it wouldn't  
be more secure to spool out the array to a file from the first  
script after it has processed it, and then read it in to the second  
script when it starts. This assumes the two scripts are running on  
the same server, of course. The second script could be called after  
the first script finishes by using a header redirect. I'm curious  
about people's thought on the security and efficacy of different  
ways of sharing data between scripts when you're not using sessions.



I am quite new to PHP. (I am having trouble to understand how HTML,  
PHP and Javascript work together). Right now I just wanted to get  
the job done. But if you can suggest me a more efficient way to do  
it, I will definitely look into it.


Are you also new to programming in general? If so, I suggest spending  
some time studying up on programming concepts and design patterns. I  
do understand the idea of just get the job done, but without basic  
knowledge, the result is not going to be very good, and likely lead to  
maintenance nightmares down the road.


As I don't really understand what it is you're trying to do, it's hard  
to make concrete suggestions. From your very brief description, it  
sounds like you have one script that takes the form submission and  
populates an array, which you want to share with another script.  
Typically, data sharing between scripts is accomplished in a few  
different ways:


1) use of session variables (which requires the script to invoke  
session management)


B) storing the information in a persistent data base (e.g. MySQL)

iii) storing the information in a transient data store (e.g. a  
temporary file). There are different ways to format the data in the  
file, including as php script (so you can include the file), JSON,  
YAML, etc.


d) passing the information to the second script via a query string or  
a post data buffer


Any of these things require understanding of the mechanisms and  
implementation idioms involved. Choosing a particular implementation  
depends on several variables in your overall design and implementation  
environment. A mailing list is a poor way to transmit this  
information, which is why most of it is documented elsewhere. 

[PHP] Array from one form to other?

2011-02-19 Thread Yogesh
Hello,

I have two forms. One form helps read an input file into an array. And the
other form needs this array as an input.
I am able to read the input file into an array, but how do I pass it over to
the other form.

Both forms have PHP file as 'action'.

Please help. Thanks

- Yogesh


Re: [PHP] Array from one form to other?

2011-02-19 Thread Daniel Brown
On Sat, Feb 19, 2011 at 19:38, Yogesh yogesh...@gmail.com wrote:
 Hello,

 I have two forms. One form helps read an input file into an array. And the
 other form needs this array as an input.
 I am able to read the input file into an array, but how do I pass it over to
 the other form.

 Both forms have PHP file as 'action'.

What's the method?  GET or POST?

-- 
/Daniel P. Brown
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

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



Re: [PHP] Array from one form to other?

2011-02-19 Thread Yogesh
POST





On Sat, Feb 19, 2011 at 9:44 PM, Daniel Brown danbr...@php.net wrote:

 On Sat, Feb 19, 2011 at 19:38, Yogesh yogesh...@gmail.com wrote:
  Hello,
 
  I have two forms. One form helps read an input file into an array. And
 the
  other form needs this array as an input.
  I am able to read the input file into an array, but how do I pass it over
 to
  the other form.
 
  Both forms have PHP file as 'action'.

 What's the method?  GET or POST?

 --
 /Daniel P. Brown
 Network Infrastructure Manager
 Documentation, Webmaster Teams
 http://www.php.net/



Re: [PHP] Array from one form to other?

2011-02-19 Thread Daniel Brown
On Sat, Feb 19, 2011 at 21:50, Yogesh yogesh...@gmail.com wrote:
 POST

Use cURL, look into curl_setopt(), and add square brackets (and
optional key names) to your array.  A quick start:

?php
$ch = curl_init();
curl_setopt(CURLOPT_POST,1);
curl_setopt(CURLOPT_POSTFIELDS,'firstArr[]=applefirstArr[]=orangefirstArr[]=bananasecondArr[one]=foosecondArr[two]=bar');
// etc.
?

-- 
/Daniel P. Brown
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

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



Re: [PHP] array to var - with different name

2011-01-21 Thread Donovan Brooke

Paul M Foster wrote:
[snip]

Shawn, I don't know if I have a good reason, other than I rather like
working with string vars instead of array vars from $_REQUEST for
(sticky forms and conditionals). I can check/verify them as well in the
process.


You should probably get used to dealing with the array variables.
However, you can also look at the extract() function. It won't do
exactly what you want, but it will pull the array keys/values into the
current symbol table. Use with caution (note the second parameter to the
function).

Paul



Well, It occurs to me that you can't code in PHP without getting use to 
dealing with arrays. ;-)


I just much rather like typing:
input name=f_email value=?PHP if (isset($t_email)) { print 
htmlspecialchars($t_email);} ? /


Instead of:
input name=f_email value=?PHP if (isset($_GET['f_email'])) { print 
htmlspecialchars($_GET['f_email']);} ? /


or

if ($t_ok) {

}

instead of:
if ($_GET['f_ok']) {

}

Save's a lot of typing time as far as I can tell.

Donovan





--
D Brooke

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



Re: [PHP] array to var - with different name

2011-01-21 Thread Donovan Brooke

Donovan Brooke wrote:
[snip]

if ($t_ok) {

}



Small correction.. with my established naming convention.. the above 
ideally would be:

 if ($b_ok) {

 }


D

--
D Brooke

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



[PHP] array to var - with different name

2011-01-20 Thread Donovan Brooke

Hello again!

I'm trying to find a good way to convert array key/value's to
variable name values... but with the caveat of the name being
slightly different than the original key
(to fit my naming conventions).

first, I (tediously) did this:

---
if (isset($_GET['f_action'])) {
  $t_action = $_GET['f_action'];
}

if (isset($_POST['f_action'])) {
  $t_action = $_POST['f_action'];
}

if (isset($_GET['f_ap'])) {
  $t_ap = $_GET['f_ap'];
}

if (isset($_POST['f_ap'])) {
  $t_ap = $_POST['f_ap'];
}
---

Instead, I wanted to find *all* incoming f_ keys in the POST/GET 
array, and convert them to a variable name consisting of t_ in one 
statement.


I then did this test and it appears to work (sorry for email line breaks):

-
$a_formvars = array('f_1' = '1','f_2' = '2','f_3' = '3','f_4' = 
'4','f_5' = '5','f_6' = '6',);


$t_string = ;
foreach ($a_formvars as $key = $value) {
  if (substr($key,0,2) == 'f_') {
$t_string = $t_string . t_ . substr($key,2) . =$value;
parse_str($t_string);
  }
}
-

I figure I can adapt the above by doing something like:

$a_formvars = array_merge($_POST,$_GET);

However, I thought I'd check with you all to see if there is something
I'm missing. I don't speak PHP that well and there may be an easier way.

Thanks,
Donovan


--
D Brooke

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



Re: [PHP] array to var - with different name

2011-01-20 Thread Daniel Molina Wegener
On Thursday 20 January 2011,
Donovan Brooke li...@euca.us wrote:

 Hello again!
 
 I'm trying to find a good way to convert array key/value's to
 variable name values... but with the caveat of the name being
 slightly different than the original key
 (to fit my naming conventions).
 
 first, I (tediously) did this:
 
 ---
 if (isset($_GET['f_action'])) {
$t_action = $_GET['f_action'];
 }
 
 if (isset($_POST['f_action'])) {
$t_action = $_POST['f_action'];
 }
 
 if (isset($_GET['f_ap'])) {
$t_ap = $_GET['f_ap'];
 }
 
 if (isset($_POST['f_ap'])) {
$t_ap = $_POST['f_ap'];
 }
 ---
 
 Instead, I wanted to find *all* incoming f_ keys in the POST/GET
 array, and convert them to a variable name consisting of t_ in one
 statement.

  That was ver tedious...

 
 I then did this test and it appears to work (sorry for email line
 breaks):
 
 -
 $a_formvars = array('f_1' = '1','f_2' = '2','f_3' = '3','f_4' =
 '4','f_5' = '5','f_6' = '6',);
 
 $t_string = ;
 foreach ($a_formvars as $key = $value) {
if (substr($key,0,2) == 'f_') {
  $t_string = $t_string . t_ . substr($key,2) . =$value;
  parse_str($t_string);
}
 }
 -
 
 I figure I can adapt the above by doing something like:
 
 $a_formvars = array_merge($_POST,$_GET);
 
 However, I thought I'd check with you all to see if there is something
 I'm missing. I don't speak PHP that well and there may be an easier way.

  Did you tried the $_REQUEST variable?

  Take a look on:

  ?php echo var_dump($_REQUEST); ?

 
 Thanks,
 Donovan

Best regards,
-- 
Daniel Molina Wegener dmw [at] coder [dot] cl
System Programmer  Web Developer
Phone: +56 (2) 979-0277 | Blog: http://coder.cl/


Re: [PHP] array to var - with different name

2011-01-20 Thread Tommy Pham
On Thu, Jan 20, 2011 at 2:28 PM, Donovan Brooke wrote:
 Hello again!

 I'm trying to find a good way to convert array key/value's to
 variable name values... but with the caveat of the name being
 slightly different than the original key
 (to fit my naming conventions).

 first, I (tediously) did this:

 ---
 if (isset($_GET['f_action'])) {
  $t_action = $_GET['f_action'];
 }

 if (isset($_POST['f_action'])) {
  $t_action = $_POST['f_action'];
 }

 if (isset($_GET['f_ap'])) {
  $t_ap = $_GET['f_ap'];
 }

 if (isset($_POST['f_ap'])) {
  $t_ap = $_POST['f_ap'];
 }
 ---

 Instead, I wanted to find *all* incoming f_ keys in the POST/GET array,
 and convert them to a variable name consisting of t_ in one statement.

 I then did this test and it appears to work (sorry for email line breaks):

 -
 $a_formvars = array('f_1' = '1','f_2' = '2','f_3' = '3','f_4' =
 '4','f_5' = '5','f_6' = '6',);

 $t_string = ;
 foreach ($a_formvars as $key = $value) {
  if (substr($key,0,2) == 'f_') {
    $t_string = $t_string . t_ . substr($key,2) . =$value;
    parse_str($t_string);
  }
 }
 -

 I figure I can adapt the above by doing something like:

 $a_formvars = array_merge($_POST,$_GET);

 However, I thought I'd check with you all to see if there is something
 I'm missing. I don't speak PHP that well and there may be an easier way.

 Thanks,
 Donovan


 --
 D Brooke


foreach ($_GET as $key = $value) $$key = $value;
foreach ($_POST as $key = $value) $$key = $value;

or

foreach ($_REQUEST as $key = $value) $$key = $value;

short-circuited one-liners :)

Regards,
Tommy

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



Re: [PHP] array to var - with different name

2011-01-20 Thread Tommy Pham
On Thu, Jan 20, 2011 at 3:09 PM, Tommy Pham tommy...@gmail.com wrote:
 On Thu, Jan 20, 2011 at 2:28 PM, Donovan Brooke wrote:
 Hello again!

 I'm trying to find a good way to convert array key/value's to
 variable name values... but with the caveat of the name being
 slightly different than the original key
 (to fit my naming conventions).

 first, I (tediously) did this:

 ---
 if (isset($_GET['f_action'])) {
  $t_action = $_GET['f_action'];
 }

 if (isset($_POST['f_action'])) {
  $t_action = $_POST['f_action'];
 }

 if (isset($_GET['f_ap'])) {
  $t_ap = $_GET['f_ap'];
 }

 if (isset($_POST['f_ap'])) {
  $t_ap = $_POST['f_ap'];
 }
 ---

 Instead, I wanted to find *all* incoming f_ keys in the POST/GET array,
 and convert them to a variable name consisting of t_ in one statement.

 I then did this test and it appears to work (sorry for email line breaks):

 -
 $a_formvars = array('f_1' = '1','f_2' = '2','f_3' = '3','f_4' =
 '4','f_5' = '5','f_6' = '6',);

 $t_string = ;
 foreach ($a_formvars as $key = $value) {
  if (substr($key,0,2) == 'f_') {
    $t_string = $t_string . t_ . substr($key,2) . =$value;
    parse_str($t_string);
  }
 }
 -

 I figure I can adapt the above by doing something like:

 $a_formvars = array_merge($_POST,$_GET);

 However, I thought I'd check with you all to see if there is something
 I'm missing. I don't speak PHP that well and there may be an easier way.

 Thanks,
 Donovan


 --
 D Brooke


 foreach ($_GET as $key = $value) $$key = $value;
 foreach ($_POST as $key = $value) $$key = $value;

 or

 foreach ($_REQUEST as $key = $value) $$key = $value;

 short-circuited one-liners :)

 Regards,
 Tommy


akk... wrong clicked before I had a chance to fix the code. anyway,

foreach ($_GET as $key = $value) if (substr($key, 0, 2) == 'f_')
${'t_'.substr($key, 2)} = $value;

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



Re: [PHP] array to var - with different name

2011-01-20 Thread Donovan Brooke

Tommy Pham wrote:
[snip]

foreach ($_REQUEST as $key =  $value) $$key = $value;

short-circuited one-liners :)

Regards,
Tommy



akk... wrong clicked before I had a chance to fix the code. anyway,

foreach ($_GET as $key =  $value) if (substr($key, 0, 2) == 'f_')
${'t_'.substr($key, 2)} = $value;



Tommy, excellent.. I had just rewrote your first suggestion:

foreach ($a_formvars as $key = $value) ${str_replace('f_', 't_',$key)} 
= $value;


(which works)
but I like that you are only affecting the vars that *begin* with 
$match. I suppose the above would also work with $_REQUEST.




Shawn, I don't know if I have a good reason, other than I rather like
working with string vars instead of array vars from $_REQUEST for
(sticky forms and conditionals). I can check/verify them as well in the 
process.


Thanks to all that posted.. I always learn from the skin the cat game.

Donovan



--
D Brooke

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



RE: [PHP] array to var - with different name

2011-01-20 Thread Tommy Pham
 -Original Message-
 From: Donovan Brooke [mailto:li...@euca.us]
 Sent: Thursday, January 20, 2011 3:29 PM
 Cc: php-general@lists.php.net
 Subject: Re: [PHP] array to var - with different name
 
 Tommy Pham wrote:
 [snip]
  foreach ($_REQUEST as $key =  $value) $$key = $value;
 
  short-circuited one-liners :)
 
  Regards,
  Tommy
 
 
  akk... wrong clicked before I had a chance to fix the code. anyway,
 
  foreach ($_GET as $key =  $value) if (substr($key, 0, 2) == 'f_')
  ${'t_'.substr($key, 2)} = $value;
 
 
 Tommy, excellent.. I had just rewrote your first suggestion:
 
 foreach ($a_formvars as $key = $value) ${str_replace('f_', 't_',$key)} =
 $value;
 
 (which works)
 but I like that you are only affecting the vars that *begin* with $match. I
 suppose the above would also work with $_REQUEST.
 
 
 
 Shawn, I don't know if I have a good reason, other than I rather like
 working with string vars instead of array vars from $_REQUEST for
 (sticky forms and conditionals). I can check/verify them as well in the
 process.
 
 Thanks to all that posted.. I always learn from the skin the cat game.
 
 Donovan
 
 
 --
 D Brooke
 

I advice strongly against str_replace if you're looking specifically 'f_' at 
the __beginning__ of the string.  substr would guarantee it.

Regards,
Tommy




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



Re: [PHP] Array Symbol Suggestion

2011-01-13 Thread Richard Quadling
On 12 January 2011 20:23,  sono...@fannullone.us wrote:
 Thanks for all the responses to my suggestion.  I realize this would be a 
 major change, so that's why I also mentioned it as an addition to the 
 language.

 I'm sure it's just what you're used to, but still being new to all this, it 
 just makes sense (to me anyway) to have different symbols for different 
 variable types:
 $scalar
 @array
 #hash

 Since the @ sign is already reserved, maybe there's another symbol that would 
 work better?  I don't know.  These are just ideas that I came up with while 
 reading and I thought I'd throw it out there to see what others thought.

 I like the idea of a naming convention, so that's what I'll do in my scripts. 
  I also appreciate the heads up on is_string(), is_array(), and var_dump().

 Thanks again,
 Marc

PHP recently introduced namespaces to PHP5. One of the issues at the
time was the namespace separator. Do to all the common symbols already
being used, it was necessary to re-use one and the context dictates
its intent.

So whilst \n is the newline character, in the namespace string below

namespace my\namespaces\are\here;

the \n is not a newline.

With that, there are no common symbols available.



The Hungarian Notation [1] was what I was taught all those years ago
when I learnt standard C programming. I've kept that with me through
to PHP. Some say it is redundant in PHP. I suppose this is true, but
it works for me and doesn't really get in the way.

One thing to remember though is that PHP is a loosely typed language.
Having a mechanism which would somehow enforce the type based upon a
symbol would certainly be a different way of working for PHP.

This is mentioned in the [2] and is suggested to be a poor way of
working due to the lack of symbols in general.


Regards,

Richard.

[1] http://en.wikipedia.org/wiki/Hungarian_notation
[2] http://en.wikipedia.org/wiki/Hungarian_notation#Relation_to_sigils
-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

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



Re: [PHP] Array Symbol Suggestion

2011-01-13 Thread David Harkness
On Thu, Jan 13, 2011 at 2:23 AM, Richard Quadling rquadl...@gmail.comwrote:

 The Hungarian Notation [1] was what I was taught all those years ago
 when I learnt standard C programming.


I learned it early on as well, and I never really liked it. Instead of
$iFish I would prefer a more descriptive name such as $fishCount. Sure, it's
a little longer to type, but it tells you what that number measures. In
today's world of objects and loosely-typed languages, a descriptive variable
name can be more important than a symbol or notation to hint at the type.

As for arrays, I always name the variable plural. And if it maps keys to
values instead of holding a list of items, I will typically name it
$foosByBar, e.g. $customersById. From that name I *know* it's array
already--no need for a prefix or special symbol.

$oPlayer, $sName, $iWidth...what's the point? The context in which the
variable is used can provide more meaning. If you stick to short
functions/methods that do one specific thing, you'll be able to tell that
$player is an object, $name is a string, and $width is an integer.

I highly recommend the book Clean Code: A Handbook of Agile
Software Craftsmanship by Robert C. Martin. [1] It has a lot of great advice
on keeping your code easy to understand, test, and maintain.

David

[1]
http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882


Re: [PHP] Array Symbol Suggestion

2011-01-13 Thread David Hutto
On Thu, Jan 13, 2011 at 12:59 PM, David Harkness
davi...@highgearmedia.com wrote:
 On Thu, Jan 13, 2011 at 2:23 AM, Richard Quadling rquadl...@gmail.comwrote:

 The Hungarian Notation [1] was what I was taught all those years ago
 when I learnt standard C programming.


 I learned it early on as well, and I never really liked it. Instead of
 $iFish I would prefer a more descriptive name such as $fishCount.


What info did you get on hook for the client?



Sure, it's
 a little longer to type, but it tells you what that number measures. In
 today's world of objects and loosely-typed languages, a descriptive variable
 name can be more important than a symbol or notation to hint at the type.

 As for arrays, I always name the variable plural. And if it maps keys to
 values instead of holding a list of items, I will typically name it
 $foosByBar, e.g. $customersById. From that name I *know* it's array
 already--no need for a prefix or special symbol.

 $oPlayer, $sName, $iWidth...what's the point? The context in which the
 variable is used can provide more meaning. If you stick to short
 functions/methods that do one specific thing, you'll be able to tell that
 $player is an object, $name is a string, and $width is an integer.

 I highly recommend the book Clean Code: A Handbook of Agile
 Software Craftsmanship by Robert C. Martin. [1] It has a lot of great advice
 on keeping your code easy to understand, test, and maintain.

 David

 [1]
 http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882




-- 
Sometimes...my mama...says I get over excited about technology.

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



Re: [PHP] Array Symbol Suggestion

2011-01-13 Thread David Harkness
On Thu, Jan 13, 2011 at 10:07 AM, David Hutto smokefl...@gmail.com wrote:

 On Thu, Jan 13, 2011 at 12:59 PM, David Harkness
  I learned it early on as well, and I never really liked it. Instead of
  $iFish I would prefer a more descriptive name such as $fishCount.

 What info did you get on hook for the client?


The brain is so interesting. I have no idea where $iFish came from. I've
never done an application even remotely related to fishing or the fishing
industry. Not even a fish-based game. :)

David


[PHP] Array Symbol Suggestion

2011-01-12 Thread sono-io
I'd like to make a suggestion for a change, or possibly an addition, to the PHP 
language.

I'm learning PHP and have been very excited with what it can do in relation to 
HTML.  But when I got to the part about arrays, I was disappointed to see that 
they are designated with a $ the same as other variables.  I was learning Perl 
before I switched, and it uses the @ sign to designate an array.  That makes it 
a lot simpler to see at a glance what is an array and what isn't - at least for 
beginners like me.

Has there been any talk of adopting the @ sign for arrays in PHP?  Or is that 
symbol used for something else that I haven't read about yet?

What is the proper channel for making suggestions like this?

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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Per Jessen
sono...@fannullone.us wrote:

 I'd like to make a suggestion for a change, or possibly an addition,
 to the PHP language.
 
 I'm learning PHP and have been very excited with what it can do in
 relation to HTML.  But when I got to the part about arrays, I was
 disappointed to see that they are designated with a $ the same as
 other variables.  I was learning Perl before I switched, and it uses
 the @ sign to designate an array.  That makes it a lot simpler to see
 at a glance what is an array and what isn't - at least for beginners
 like me.
 
 Has there been any talk of adopting the @ sign for arrays in PHP?  Or
 is that symbol used for something else that I haven't read about yet?
 
 What is the proper channel for making suggestions like this?

The php-development mailing list.  What you're suggesting is a pretty
fundamental change, don't be disappointed if it is not met with
universal approval.


-- 
Per Jessen, Zürich (5.9°C)


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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Donovan Brooke

sono...@fannullone.us wrote:

I'd like to make a suggestion for a change, or possibly an addition, to the PHP 
language.

I'm learning PHP and have been very excited with what it can do in relation to 
HTML.  But when I got to the part about arrays, I was disappointed to see that 
they are designated with a $ the same as other variables.  I was learning Perl 
before I switched, and it uses the @ sign to designate an array.  That makes it 
a lot simpler to see at a glance what is an array and what isn't - at least for 
beginners like me.

Has there been any talk of adopting the @ sign for arrays in PHP?  Or is that 
symbol used for something else that I haven't read about yet?

What is the proper channel for making suggestions like this?

Thanks,
Marc



Hi Marc,
I'm a PHP n00b as well and had similar thoughts regarding this..

just imagine two variables called the same thing.. a string and array.. 
and accidentally resetting one..


$oops = something;

however, from my experience, there is often this kind of problem in
any language, and that is where naming conventions come in very handy.

I don't know if the PHP community has any standard convention.. but I
would suggest something like:

$a_foo  (for arrays)
$f_foo  (imploding into form variables)
$s_foo  (string variables)
$db_foo  (variables coming from databases perhaps)
etc..

This way, you'd never be confused of the origin of the variable.

Donovan



--
D Brooke

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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Ashley Sheridan
On Wed, 2011-01-12 at 11:45 -0800, sono...@fannullone.us wrote:

 I'd like to make a suggestion for a change, or possibly an addition, to the 
 PHP language.
 
 I'm learning PHP and have been very excited with what it can do in relation 
 to HTML.  But when I got to the part about arrays, I was disappointed to see 
 that they are designated with a $ the same as other variables.  I was 
 learning Perl before I switched, and it uses the @ sign to designate an 
 array.  That makes it a lot simpler to see at a glance what is an array and 
 what isn't - at least for beginners like me.
 
 Has there been any talk of adopting the @ sign for arrays in PHP?  Or is that 
 symbol used for something else that I haven't read about yet?
 
 What is the proper channel for making suggestions like this?
 
 Thanks,
 Marc


PHP is a loosely typed language, so you can have a variable that,
throughout its lifetime in an app, is both a scaler (integer, string,
etc) or an array. For example:

?php
$message = hello world;

echo $message;

$message = array('hello', 'bob');
echo {$message[0]} {$message[1]};
?

There are functions you can use to determine the type of a variable,
such as is_string() and is_array() and you can use var_dump() in debug
statements to quickly see the type of a variable. I think changing
something as integral as a variable prefix would break a lot of code
which makes use of the loose typing, and would cause much more confusion
further down the line.

Also, as you may have guessed, the @ symbol is already in use at the
moment. In PHP it ensures that minor errors are silently ignored. For
example:

@executeSomeFunction()

would run that named function, but ignore any minor errors and warnings.
You'll typically find it used a lot in calls to mail() as that can be
flaky on some systems due to a number of factors outside of PHP.

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




Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Joshua Kehn
On Jan 12, 2011, at 3:28 PM, Ashley Sheridan wrote:

 If you check out the manual pages for those functions as well, you'll
 see other related functions. I must say, of any language I've used, the
 php.net documentation is by far the best, giving plenty of information
 and user comments too. It's a resource I still can't do without, and I
 reckon even the old hands on this list would say the same.
 
 
 Thanks,
 Ash
 http://www.ashleysheridan.co.uk
 
 

I fully agree with you on php.net being some of the best documentation out 
there.

I would say that a lot of the Java documentation is wonderfully done as well. 
It doesn't offer user comments, but it is very complete and covers just about 
every aspect of a class.

Regards,

-Josh

Joshua Kehn | josh.k...@gmail.com
http://joshuakehn.com


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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Daniel Brown
On Wed, Jan 12, 2011 at 14:45,  sono...@fannullone.us wrote:
 I'd like to make a suggestion for a change, or possibly an addition, to the 
 PHP language.

 I'm learning PHP and have been very excited with what it can do in relation 
 to HTML.  But when I got to the part about arrays, I was disappointed to see 
 that they are designated with a $ the same as other variables.  I was 
 learning Perl before I switched, and it uses the @ sign to designate an 
 array.  That makes it a lot simpler to see at a glance what is an array and 
 what isn't - at least for beginners like me.

 Has there been any talk of adopting the @ sign for arrays in PHP?  Or is that 
 symbol used for something else that I haven't read about yet?

The @ is an error control operator, used to buffer the output and
store it in a variable - $php_errormsg.  There's no way that would be
changed to become an array designator (though that doesn't mean your
idea itself is a bad one).

 What is the proper channel for making suggestions like this?

Usually on the Internals mailing list (intern...@lists.php.net) or
as a Feature Request in the bug tracker (http://bugs.php.net/).

-- 
/Daniel P. Brown
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Michael Shadle
On Wed, Jan 12, 2011 at 12:37 PM, Daniel Brown danbr...@php.net wrote:

    The @ is an error control operator, used to buffer the output and
 store it in a variable - $php_errormsg.  There's no way that would be
 changed to become an array designator (though that doesn't mean your
 idea itself is a bad one).

@ squelches error messages.

AFAIK $php_errormsg is the last error that PHP incurred. not based on @

@ just silences the errors from being reported, which is a bad
thing as error collection is done even if error_reporting is off, it
is still built internally as a string, that's why developing with
E_ALL and E_STRICT even on is the best practice. even notices wind up
adding to the internal error/etc. string stack.

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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Daniel Brown
On Wed, Jan 12, 2011 at 15:41, Michael Shadle mike...@gmail.com wrote:
 On Wed, Jan 12, 2011 at 12:37 PM, Daniel Brown danbr...@php.net wrote:

    The @ is an error control operator, used to buffer the output and
 store it in a variable - $php_errormsg.  There's no way that would be
 changed to become an array designator (though that doesn't mean your
 idea itself is a bad one).

 @ squelches error messages.

 AFAIK $php_errormsg is the last error that PHP incurred. not based on @

Correct.  The way I worded it makes it sound like @ is what
populates the variable, which would be incorrect.  Plus, I should also
mention that $php_errormsg is only available if you enable
track_errors anyway, which (if I remember correctly) is off by
default.

Thanks for pointing that out, Mike.

-- 
/Daniel P. Brown
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Per Jessen
Ashley Sheridan wrote:

 On Wed, 2011-01-12 at 12:23 -0800, sono...@fannullone.us wrote:
 
 Thanks for all the responses to my suggestion.  I realize this would
 be a major change, so that's why I also mentioned it as an addition
 to the language.
 
 I'm sure it's just what you're used to, but still being new to all
 this, it just makes sense (to me anyway) to have different symbols
 for different variable types: $scalar @array
 #hash
 
 Since the @ sign is already reserved, maybe there's another symbol
 that would work better?  I don't know.  These are just ideas that I
 came up with while reading and I thought I'd throw it out there to
 see what others thought.
 
 I like the idea of a naming convention, so that's what I'll do in my
 scripts.  I also appreciate the heads up on is_string(), is_array(),
 and var_dump().
 
 Thanks again,
 Marc
 
 
 If you check out the manual pages for those functions as well, you'll
 see other related functions. I must say, of any language I've used,
 the php.net documentation is by far the best, giving plenty of
 information and user comments too. It's a resource I still can't do
 without, and I reckon even the old hands on this list would say the
 same.

Yes, I wouldn't want to be without my local php.net mirror.  Other
languages that can easily match the quality of the documentation -
assembler, C and C++, to name a few. 


-- 
Per Jessen, Zürich (7.9°C)


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



Re: [PHP] Array Symbol Suggestion

2011-01-12 Thread Per Jessen
Donovan Brooke wrote:

 however, from my experience, there is often this kind of problem in
 any language, and that is where naming conventions come in very handy.
 
 I don't know if the PHP community has any standard convention.. 

One popular naming convention:

http://en.wikipedia.org/wiki/Hungarian_notation



-- 
Per Jessen, Zürich (7.8°C)


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



Re: [PHP] array question

2010-12-20 Thread Ravi Gehlot
Jim Lucas has it. You can use the preg_match function to find it. I would
use regexp for that reason. regexp is good for making sure things are typed
the way they need to (mostly used for).

Ravi.


On Sat, Dec 18, 2010 at 5:17 PM, Jim Lucas li...@cmsws.com wrote:

 On 12/17/2010 12:52 PM, Sorin Buturugeanu wrote:

 Hello all!

 I have a question regarding arrays and the way I can use a value.

 Let's say I have this string:

 $s = 'banana,apple,mellon,grape,nut,orange'

 I want to explode it, and get the third value. For this I would normally
 do:

 $a = explode(',', $s);
 echo $s[2];

 That's all fine, but is there a way to get the value directly, without
 having to write another line in my script. I mean something like this:

 echo explode(',', $s)[2];

 or

 echo {explode(',', $s)}[2];

 I couldn't find out this answer anywhere, that's why I posted here.

 Cheers and thanks!


 Sure it CAN be done.  Nobody laugh too loud here... But...

 ?php

 $s = 'banana,apple,mellon,grape,nut,orange';
 echo preg_replace('/([^,]+,){3}([^,]+).*/', '$2', $s);

 ?
 Outputs: grape

 The {3} part is equivalent to the array position.  Change that number, and
 you change which word will get displayed.

 Jim Lucas

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




Re: [PHP] array question

2010-12-18 Thread Jim Lucas

On 12/17/2010 12:52 PM, Sorin Buturugeanu wrote:

Hello all!

I have a question regarding arrays and the way I can use a value.

Let's say I have this string:

$s = 'banana,apple,mellon,grape,nut,orange'

I want to explode it, and get the third value. For this I would normally do:

$a = explode(',', $s);
echo $s[2];

That's all fine, but is there a way to get the value directly, without
having to write another line in my script. I mean something like this:

echo explode(',', $s)[2];

or

echo {explode(',', $s)}[2];

I couldn't find out this answer anywhere, that's why I posted here.

Cheers and thanks!



Sure it CAN be done.  Nobody laugh too loud here... But...

?php

$s = 'banana,apple,mellon,grape,nut,orange';
echo preg_replace('/([^,]+,){3}([^,]+).*/', '$2', $s);

?
Outputs: grape

The {3} part is equivalent to the array position.  Change that number, 
and you change which word will get displayed.


Jim Lucas

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



[PHP] array question

2010-12-17 Thread Sorin Buturugeanu
Hello all!

I have a question regarding arrays and the way I can use a value.

Let's say I have this string:

$s = 'banana,apple,mellon,grape,nut,orange'

I want to explode it, and get the third value. For this I would normally do:

$a = explode(',', $s);
echo $s[2];

That's all fine, but is there a way to get the value directly, without
having to write another line in my script. I mean something like this:

echo explode(',', $s)[2];

or

echo {explode(',', $s)}[2];

I couldn't find out this answer anywhere, that's why I posted here.

Cheers and thanks!


RE: [PHP] array question

2010-12-17 Thread Jay Blanchard
[snip]
I have a question regarding arrays and the way I can use a value.

Let's say I have this string:

$s = 'banana,apple,mellon,grape,nut,orange'

I want to explode it, and get the third value. For this I would normally
do:

$a = explode(',', $s);
echo $s[2];

That's all fine, but is there a way to get the value directly, without
having to write another line in my script. I mean something like this:

echo explode(',', $s)[2];

or

echo {explode(',', $s)}[2];

I couldn't find out this answer anywhere, that's why I posted here.
[/snip]

Because the array is not formed until after the explode you cannot do it
with one command, but you could place 2 commands on one line :)

$a = explode(',', $s); echo $a[2];

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



Re: [PHP] array question

2010-12-17 Thread Daniel Brown
On Fri, Dec 17, 2010 at 15:52, Sorin Buturugeanu m...@soin.ro wrote:
 Hello all!

 I have a question regarding arrays and the way I can use a value.

 Let's say I have this string:

 $s = 'banana,apple,mellon,grape,nut,orange'

 I want to explode it, and get the third value. For this I would normally do:

 $a = explode(',', $s);
 echo $s[2];

 That's all fine, but is there a way to get the value directly, without
 having to write another line in my script. I mean something like this:

 echo explode(',', $s)[2];

 or

 echo {explode(',', $s)}[2];

 I couldn't find out this answer anywhere, that's why I posted here.

Unfortunately, no --- at least, not yet.  Chaining discussions
come up now and again, so it's quite possible that future versions of
PHP will have something similar.  That said, for now you could do
something like this:

?php
/**
 * mixed return_item( string $car, mixed $pos )
 *  - $str The original string
 *  - $charThe delimiting character(s) by which to explode
 *  - $pos The position to return
 *  - $shift   Whether or not we should see 1 as the first array position
 */
function return_item($str,$char,$pos=null,$shift=false) {

// Make sure $char exists in $str, return false if not.
if (!strpos($str,$char)) return false;

// Split $char by $str into the array $arr
$arr = explode($char,$str);

// If $pos undefined or null, return the whole array
if (is_null($pos)) return $arr;

// If $pos is an array, return the requested positions
if (isset($pos)  is_array($pos)  !empty($pos)) {

// Instantiate a second array container for return
$ret = array();

// Iterate
foreach ($pos as $i) {

// This is just in case it was given screwy or a number as
a non-integer
if (!is_int($i)  is_numeric($i)) $i = (int)round($i);

// Make sure $i is now an integer and that position exists
if (!is_int($i) || !isset($arr[$i]) || empty($arr[$i])) continue;

// If all seems okay, append this to $ret
$ret[] = $arr[$i];
}

// Return the array
return $ret;
}

/**
  * If $pos is a number (integer or round()'able number),
  * we'll go ahead and make sure the position is there.
  * If so, we'll return it.
  */
if (is_int($pos) || is_numeric($pos)) {

// This is just in case it was given screwy or as a non-integer
if (!is_int($pos)) $pos = (int)round($pos);

// If we want to start the array count at 1, do that now
if (isset($shift)  ($shift === true || $shift === 1)) {

//  but only if the number isn't zero
if ($pos !== 0) --$pos;

}

// Return the single position if it exists
if (isset($arr[$pos])  !empty($arr[$pos])) return $arr[$pos];
}

/**
 * If we've failed every case, something is either
 * wrong or we supplied bad data.  Return false.
 * Either way, feel free to add some trigger_error()
 * stuff here if you want to have the function hold
 * your hand.
 */
return false;
}



/**
 * Some simple examples
 */

$foo = 'apple,banana,carrot,orange,carrot,lettuce,tomato,beer,carrot,idiot';

return_item($foo,',',7); // Returns 'beer'
return_item($foo,'carrot',0); // Returns 'apple,banana,'
return_item($foo,','); // Returns all items in an array
return_item($foo,',',array(0,'2',6.6)); // Returns array: apple,carrot,beer
return_item($foo,',',1,true); // Returns 'apple'
?


Of course, as with almost all code I submit here, it's typed
directly into this window and is untested, so you use it at your own
risk, your mileage may vary, see a doctor if you have an erection
lasting more than four hours, et cetera.

Happy Friday, all.

-- 
/Daniel P. Brown
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

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



Re: [PHP] array question

2010-12-17 Thread Sorin Buturugeanu
Tanks for all of your responses!

I guess a function is the way to go. I just have to see if the situation
comes up enough times to justify the function approach.

@Dan: I really enjoyed your disclaimer :D


--
Sorin Buturugeanu
www.soin.ro
http://www.facebook.com/buturugeanu
http://www.twitter.com/soinrohttp://www.soin.ro/feed/blogblog:
Despre Launch48 si ce poti face in 2 zile http://www.soin.ro/b75



On 17 December 2010 23:48, Daniel Brown danbr...@php.net wrote:

 On Fri, Dec 17, 2010 at 15:52, Sorin Buturugeanu m...@soin.ro wrote:
  Hello all!
 
  I have a question regarding arrays and the way I can use a value.
 
  Let's say I have this string:
 
  $s = 'banana,apple,mellon,grape,nut,orange'
 
  I want to explode it, and get the third value. For this I would normally
 do:
 
  $a = explode(',', $s);
  echo $s[2];
 
  That's all fine, but is there a way to get the value directly, without
  having to write another line in my script. I mean something like this:
 
  echo explode(',', $s)[2];
 
  or
 
  echo {explode(',', $s)}[2];
 
  I couldn't find out this answer anywhere, that's why I posted here.

 Unfortunately, no --- at least, not yet.  Chaining discussions
 come up now and again, so it's quite possible that future versions of
 PHP will have something similar.  That said, for now you could do
 something like this:

 ?php
 /**
  * mixed return_item( string $car, mixed $pos )
  *  - $str The original string
  *  - $charThe delimiting character(s) by which to explode
  *  - $pos The position to return
  *  - $shift   Whether or not we should see 1 as the first array position
  */
 function return_item($str,$char,$pos=null,$shift=false) {

// Make sure $char exists in $str, return false if not.
if (!strpos($str,$char)) return false;

// Split $char by $str into the array $arr
$arr = explode($char,$str);

// If $pos undefined or null, return the whole array
if (is_null($pos)) return $arr;

// If $pos is an array, return the requested positions
if (isset($pos)  is_array($pos)  !empty($pos)) {

// Instantiate a second array container for return
$ret = array();

// Iterate
foreach ($pos as $i) {

// This is just in case it was given screwy or a number as
 a non-integer
if (!is_int($i)  is_numeric($i)) $i = (int)round($i);

// Make sure $i is now an integer and that position exists
if (!is_int($i) || !isset($arr[$i]) || empty($arr[$i]))
 continue;

// If all seems okay, append this to $ret
$ret[] = $arr[$i];
}

// Return the array
return $ret;
}

/**
  * If $pos is a number (integer or round()'able number),
  * we'll go ahead and make sure the position is there.
  * If so, we'll return it.
  */
if (is_int($pos) || is_numeric($pos)) {

// This is just in case it was given screwy or as a non-integer
if (!is_int($pos)) $pos = (int)round($pos);

// If we want to start the array count at 1, do that now
if (isset($shift)  ($shift === true || $shift === 1)) {

//  but only if the number isn't zero
if ($pos !== 0) --$pos;

}

// Return the single position if it exists
if (isset($arr[$pos])  !empty($arr[$pos])) return $arr[$pos];
}

/**
 * If we've failed every case, something is either
 * wrong or we supplied bad data.  Return false.
 * Either way, feel free to add some trigger_error()
 * stuff here if you want to have the function hold
 * your hand.
 */
return false;
 }



 /**
  * Some simple examples
  */

 $foo =
 'apple,banana,carrot,orange,carrot,lettuce,tomato,beer,carrot,idiot';

 return_item($foo,',',7); // Returns 'beer'
 return_item($foo,'carrot',0); // Returns 'apple,banana,'
 return_item($foo,','); // Returns all items in an array
 return_item($foo,',',array(0,'2',6.6)); // Returns array: apple,carrot,beer
 return_item($foo,',',1,true); // Returns 'apple'
 ?


Of course, as with almost all code I submit here, it's typed
 directly into this window and is untested, so you use it at your own
 risk, your mileage may vary, see a doctor if you have an erection
 lasting more than four hours, et cetera.

Happy Friday, all.

 --
 /Daniel P. Brown
 Network Infrastructure Manager
 Documentation, Webmaster Teams
 http://www.php.net/

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




Re: [PHP] Array problem

2010-10-28 Thread Richard Quadling
On 27 October 2010 22:15, Kevin Kinsey k...@daleco.biz wrote:
 Marc Guay wrote:

 As Nicholas pointed out, the extra underscore in the key is the issue.

 That's way too easy a fix.  I think he should check to make sure his
 version of PHP was compiled with the right extensions and that the
 browser isn't doing something unpredictably bizarre when submitting
 the form.

 Just checked the card file, today's cause is: Sunspots.

 KDK


I always believed that cosmic radiation was the cause.


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

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



RE: [PHP] Array problem

2010-10-28 Thread Bob McConnell
From: Richard Quadling

 On 27 October 2010 22:15, Kevin Kinsey k...@daleco.biz wrote:
 Marc Guay wrote:

 As Nicholas pointed out, the extra underscore in the key is the issue.

 That's way too easy a fix.  I think he should check to make sure his
 version of PHP was compiled with the right extensions and that the
 browser isn't doing something unpredictably bizarre when submitting
 the form.

 Just checked the card file, today's cause is: Sunspots.
 
 I always believed that cosmic radiation was the cause.

I'll second the cosmic radiation. We are currently in the low activity portion 
of the 11 year sunspot cycle[1], and predictions of the next high are lower 
than most cycles recorded over the past century[2]. So that one is not an easy 
sell right now.

Bob McConnell

[1] http://www.windows2universe.org/sun/activity/sunspot_cycle.html
[2] http://solarscience.msfc.nasa.gov/predict.shtml

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



Re: [PHP] Array problem

2010-10-27 Thread Kevin Kinsey

Marc Guay wrote:

As Nicholas pointed out, the extra underscore in the key is the issue.


That's way too easy a fix.  I think he should check to make sure his
version of PHP was compiled with the right extensions and that the
browser isn't doing something unpredictably bizarre when submitting
the form.


Just checked the card file, today's cause is: Sunspots.

KDK

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



Re: [PHP] Array / form processing

2010-10-08 Thread Sebastian Detert

Ron Piggott schrieb:

I am writing a custom shopping cart that eventually the cart will be
uploaded to PayPal for payment.  I need to be able to include the option
that the purchase is a gift certificate.



At present my add to cart function goes like this:

===
# Gift Certificate: 1 is a gift; 2 is personal use

if ( $gift_certificate == yes ) {
$gift = 1;
} else {
$gift = 2;
}

$_SESSION['life_coaching_order'][$product][$gift]['quantity'] = 
$_SESSION['life_coaching_order'][$product][$gift]['quantity'] + 1;

===

Now I need to display the shopping cart contents.  I want to do this
through an array as the contents of the shopping cart are in a session
variable.  I start displaying the shopping cart contents by a FOREACH
loop:

===
foreach ($_SESSION['life_coaching_order'] AS $coaching_fee_theme_reference
= $value ) {
===

What I need help with is that I don't know how to test the value of $gift
in the above array if it is a 1 or 2 (which symbolizes this is a gift
certificate).

I have something like this in mind:
if ( $_SESSION['life_coaching_order'] == 2 ) {

But I don't know how to access all the components of the array while I am
going through the FOREACH loop.

By using a 1 or 2 I have made gift certificates their own product.  If
you a better method I could use please provide me with this feedback.

Ron

The Verse of the Day
Encouragement from God's Word
www.TheVerseOfTheDay.info


  
First at all, I wouldn't use 1 or 2 for defining important informations. 
use something like

define('ORDER_GIFT', 1);
define('ORDER_PERSONAL',2);

If you want to check all values of your array you can use several 
foreach loops like


foreach ($_SESSION['life_coaching_order'] AS $coaching_product = $tmp_array)
{
 foreach ($tmp_array as $coaching_gift = $tmp_array2)
 {
   switch ($coaching_gift)
 case ORDER_GIFT: break;

 case ORDER_PERSONAL: break;
)
 } 
}



Personally I would prefer writing a class like

class Order
{
  private $product;
  private $gift;
  private $quantity;

  const ORDER_GIFT=1;
  const ORDER_PERSONAL=2;

 function getGift() {
   return $this - gift;
 }
}

using

$_SESSION['life_coaching_order'][] = new Order();

foreach ( $_SESSION['life_coaching_order'] as $order )
{
 switch ( $order - getGift() )

 case ORDER_GIFT: break;

 case ORDER_PERSONAL: break;
  
} 


I hope that will help you,

Sebastian
http://elygor.de


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



[PHP] Array / form processing

2010-10-07 Thread Ron Piggott

I am writing a custom shopping cart that eventually the cart will be
uploaded to PayPal for payment.  I need to be able to include the option
that the purchase is a gift certificate.



At present my add to cart function goes like this:

===
# Gift Certificate: 1 is a gift; 2 is personal use

if ( $gift_certificate == yes ) {
$gift = 1;
} else {
$gift = 2;
}

$_SESSION['life_coaching_order'][$product][$gift]['quantity'] = 
$_SESSION['life_coaching_order'][$product][$gift]['quantity'] + 1;
===

Now I need to display the shopping cart contents.  I want to do this
through an array as the contents of the shopping cart are in a session
variable.  I start displaying the shopping cart contents by a FOREACH
loop:

===
foreach ($_SESSION['life_coaching_order'] AS $coaching_fee_theme_reference
= $value ) {
===

What I need help with is that I don't know how to test the value of $gift
in the above array if it is a 1 or 2 (which symbolizes this is a gift
certificate).

I have something like this in mind:
if ( $_SESSION['life_coaching_order'] == 2 ) {

But I don't know how to access all the components of the array while I am
going through the FOREACH loop.

By using a 1 or 2 I have made gift certificates their own product.  If
you a better method I could use please provide me with this feedback.

Ron

The Verse of the Day
Encouragement from God's Word
www.TheVerseOfTheDay.info


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



Re: [PHP] Array / form processing

2010-10-07 Thread chris h
$_SESSION['life_coaching_order'][$product][$gift]['quantity'] =
$_SESSION['life_coaching_order'][$product][$gift]['quantity'] + 1;
===

...

===
foreach ($_SESSION['life_coaching_order'] AS $coaching_fee_theme_reference
= $value ) {
===


In this example $value would be an array. To test if it is a gift or not you
would do this from within the foreach loop:

//gift
if ( isset($value[1])  isset($value[1]['quantity']) )
{
  $gift_quantity = $value[1]['quantity'];
}

//personal use
if ( isset($value[2])  isset($value[2]['quantity']) )
{
  $personal_quantity = $value[2]['quantity'];
}


Technically the above IF's are optional, but they are proper syntax.

I don't know how you are with OOP, but you may have more luck using objects
instead of a complex array.

Chris H.


On Thu, Oct 7, 2010 at 3:35 PM, Ron Piggott
ron.pigg...@actsministries.orgwrote:


 I am writing a custom shopping cart that eventually the cart will be
 uploaded to PayPal for payment.  I need to be able to include the option
 that the purchase is a gift certificate.



 At present my add to cart function goes like this:

 ===
 # Gift Certificate: 1 is a gift; 2 is personal use

 if ( $gift_certificate == yes ) {
$gift = 1;
 } else {
$gift = 2;
 }

 $_SESSION['life_coaching_order'][$product][$gift]['quantity'] =
 $_SESSION['life_coaching_order'][$product][$gift]['quantity'] + 1;
 ===

 Now I need to display the shopping cart contents.  I want to do this
 through an array as the contents of the shopping cart are in a session
 variable.  I start displaying the shopping cart contents by a FOREACH
 loop:

 ===
 foreach ($_SESSION['life_coaching_order'] AS $coaching_fee_theme_reference
 = $value ) {
 ===

 What I need help with is that I don't know how to test the value of $gift
 in the above array if it is a 1 or 2 (which symbolizes this is a gift
 certificate).

 I have something like this in mind:
 if ( $_SESSION['life_coaching_order'] == 2 ) {

 But I don't know how to access all the components of the array while I am
 going through the FOREACH loop.

 By using a 1 or 2 I have made gift certificates their own product.  If
 you a better method I could use please provide me with this feedback.

 Ron

 The Verse of the Day
 Encouragement from God's Word
 www.TheVerseOfTheDay.info


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




Re: [PHP] Array / form processing

2010-10-07 Thread Ron Piggott

Many thanks, Chris.

I have one additional question about this shopping cart project.  I need
to make a submit button for the purpose of removing an item from the
shopping cart.

input type=submit name=submit value=Remove class=place_order/

What I am struggling with is to find an effective method for passing the
product serial number (auto_increment in the table it is stored in) so I
know which product the user is removing from their purchase.  Then I will
just unset the session variable that matches.

What are your suggestion(s)?

Thank you your help with my original question Chris.

Ron

 $_SESSION['life_coaching_order'][$product][$gift]['quantity'] =
 $_SESSION['life_coaching_order'][$product][$gift]['quantity'] + 1;
 ===

 ...

 ===
 foreach ($_SESSION['life_coaching_order'] AS $coaching_fee_theme_reference
 = $value ) {
 ===


 In this example $value would be an array. To test if it is a gift or not
 you
 would do this from within the foreach loop:

 //gift
 if ( isset($value[1])  isset($value[1]['quantity']) )
 {
   $gift_quantity = $value[1]['quantity'];
 }

 //personal use
 if ( isset($value[2])  isset($value[2]['quantity']) )
 {
   $personal_quantity = $value[2]['quantity'];
 }


 Technically the above IF's are optional, but they are proper syntax.

 I don't know how you are with OOP, but you may have more luck using
 objects
 instead of a complex array.

 Chris H.


 On Thu, Oct 7, 2010 at 3:35 PM, Ron Piggott
 ron.pigg...@actsministries.orgwrote:


 I am writing a custom shopping cart that eventually the cart will be
 uploaded to PayPal for payment.  I need to be able to include the option
 that the purchase is a gift certificate.



 At present my add to cart function goes like this:

 ===
 # Gift Certificate: 1 is a gift; 2 is personal use

 if ( $gift_certificate == yes ) {
$gift = 1;
 } else {
$gift = 2;
 }

 $_SESSION['life_coaching_order'][$product][$gift]['quantity'] =
 $_SESSION['life_coaching_order'][$product][$gift]['quantity'] + 1;
 ===

 Now I need to display the shopping cart contents.  I want to do this
 through an array as the contents of the shopping cart are in a session
 variable.  I start displaying the shopping cart contents by a FOREACH
 loop:

 ===
 foreach ($_SESSION['life_coaching_order'] AS
 $coaching_fee_theme_reference
 = $value ) {
 ===

 What I need help with is that I don't know how to test the value of
 $gift
 in the above array if it is a 1 or 2 (which symbolizes this is a gift
 certificate).

 I have something like this in mind:
 if ( $_SESSION['life_coaching_order'] == 2 ) {

 But I don't know how to access all the components of the array while I
 am
 going through the FOREACH loop.

 By using a 1 or 2 I have made gift certificates their own product.
 If
 you a better method I could use please provide me with this feedback.

 Ron

 The Verse of the Day
 Encouragement from God's Word
 www.TheVerseOfTheDay.info


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






The Verse of the Day
Encouragement from God's Word
www.TheVerseOfTheDay.info


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



Re: [PHP] Array / form processing

2010-10-07 Thread chris h
input type=submit name=submit value=Remove class=place_order/

I don't know what the context is like, but you may be better off just using
an entire form here with hidden fields. i.e.

form target=... action=...
 input type=hidden name=submit value=Remove /
 input type=hidden name=product_so value=1234 /
 input type=submit value=Remove class=place_order/
/form


Without knowing what else is going on in your page, and how the request is
being handled on the server, it's kind of hard to give exact advice. :)

Chris H.


Re: [PHP] Array question

2010-09-26 Thread a...@ashleysheridan.co.uk
I'd also like to add to that:

$array = array();
$array[] = 'text';
$array[2] = 123;
$array[] = 'hello';

Would output:

$array(
0 = 'text',
2 = 123,
3 = 'hello',
);

Note the missing index 1, as php makes a numerical index that is one greater 
than the highest already in use. As the index 2 was explicitly created, php 
made the next one at 3.

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

- Reply message -
From: chris h chris...@gmail.com
Date: Sat, Sep 25, 2010 22:05
Subject: [PHP] Array question
To: MikeB mpbr...@gmail.com
Cc: php-general@lists.php.net


Mike,

$results[] will automatically push a value unto the end of an array.

So doing this...
--
$magic = array();
$magic[] = 'a';
$magic[] = 'b';
$magic[] = 'c';
-

is exactly this same as doing this...
--
$normal = array();
$normal[0] = 'a';
$normal[1] = 'b';
$normal[2] = 'c';
-

And yes, in your example $results[] would be equivalent to $results[$j]


For more reference:
http://www.php.net/manual/en/language.types.array.php


Chris H.


On Sat, Sep 25, 2010 at 4:31 PM, MikeB mpbr...@gmail.com wrote:

 I have the following code:

 $query = SELECT * FROM classics;
 $result = mysql_query($query);

 if (!$result) die (Database access failed:  . mysql_error());
 $rows = mysql_num_rows($result);

 for ($j = 0 ; $j  $rows ; ++$j)
 {
$results[] = mysql_fetch_array($result);
 }

 mysql_close($db_server);

 My question, in the loop, why does tha author use:

 $results[] = mysql_fetch_array($result);

 instead of (as I would expect):

 $results[$j] = mysql_fetch_array($result);?

 What PHP magic is at work here?

 Thanks.


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




Re: [PHP] Array question

2010-09-26 Thread tedd

At 3:31 PM -0500 9/25/10, MikeB wrote:

-snip-

My question, in the loop, why does tha author use:

$results[] = mysql_fetch_array($result);

instead of (as I would expect):

$results[$j] = mysql_fetch_array($result);?

What PHP magic is at work here?


Mike:

That's just a shorthand way to populate an array in PHP.

One of the reasons for this feature is that somewhere in your code 
you may not know what the next index should be. So, if you use --


$results[] = $next_item;

-- then the $next_item will be automagically added to the next 
available index in the array. So you may be right in calling it PHP 
magic because I have not seen this in other languages.


Understand?

Cheers,

tedd
--
---
http://sperling.com/

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



[PHP] Array question

2010-09-25 Thread MikeB

I have the following code:

$query = SELECT * FROM classics;
$result = mysql_query($query);

if (!$result) die (Database access failed:  . mysql_error());
$rows = mysql_num_rows($result);

for ($j = 0 ; $j  $rows ; ++$j)
{
$results[] = mysql_fetch_array($result);
}

mysql_close($db_server);

My question, in the loop, why does tha author use:

$results[] = mysql_fetch_array($result);

instead of (as I would expect):

$results[$j] = mysql_fetch_array($result);?

What PHP magic is at work here?

Thanks.


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



Re: [PHP] Array question

2010-09-25 Thread chris h
Mike,

$results[] will automatically push a value unto the end of an array.

So doing this...
--
$magic = array();
$magic[] = 'a';
$magic[] = 'b';
$magic[] = 'c';
-

is exactly this same as doing this...
--
$normal = array();
$normal[0] = 'a';
$normal[1] = 'b';
$normal[2] = 'c';
-

And yes, in your example $results[] would be equivalent to $results[$j]


For more reference:
http://www.php.net/manual/en/language.types.array.php


Chris H.


On Sat, Sep 25, 2010 at 4:31 PM, MikeB mpbr...@gmail.com wrote:

 I have the following code:

 $query = SELECT * FROM classics;
 $result = mysql_query($query);

 if (!$result) die (Database access failed:  . mysql_error());
 $rows = mysql_num_rows($result);

 for ($j = 0 ; $j  $rows ; ++$j)
 {
$results[] = mysql_fetch_array($result);
 }

 mysql_close($db_server);

 My question, in the loop, why does tha author use:

 $results[] = mysql_fetch_array($result);

 instead of (as I would expect):

 $results[$j] = mysql_fetch_array($result);?

 What PHP magic is at work here?

 Thanks.


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




[PHP] Array help.

2010-07-30 Thread Paul Halliday
I have a query that may not always return a result for a value, I need
to reflect this with a 0. I am trying to overcome this by doing this
(the keys are ID's):

while ($row = mysql_fetch_row($statusQuery)) {

$cat = array(0=0,1=0,11=0,12=0,13=0,14=0,15=0,16=0,17=0,19=0);

switch ($row[1]) {
case 0: $cat[0] = $row[0]; break;
case 1: $cat[1] = $row[0]; break;
case 11: $cat[11] = $row[0]; break;
case 12: $cat[12] = $row[0]; break;
case 13: $cat[13] = $row[0]; break;
case 14: $cat[14] = $row[0]; break;
case 15: $cat[15] = $row[0]; break;
case 16: $cat[16] = $row[0]; break;
case 17: $cat[17] = $row[0]; break;
case 19: $cat[19] = $row[0]; break;
}

print_r($cat);
}

Which gives me this:

Array ( [0] = 15547 [1] = 0 [11] = 0 [12] = 0 [13] = 0 [14] = 0
[15] = 0 [16] = 0 [17] = 0 [19] = 0 )
Array ( [0] = 0 [1] = 0 [11] = 0 [12] = 0 [13] = 0 [14] = 0 [15]
= 30 [16] = 0 [17] = 0 [19] = 0 )

The query only return 2 rows:

15 | 30
0 | 15547

What am I doing wrong? Is there a more elegant way to achieve what I want?

Thanks.

-- 
Paul Halliday
Ideation | Individualization | Learner | Achiever | Analytical
http://www.pintumbler.org

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



Re: [PHP] Array help.

2010-07-30 Thread Joshua Kehn

On Jul 30, 2010, at 2:36 PM, Paul Halliday wrote:

 I have a query that may not always return a result for a value, I need
 to reflect this with a 0. I am trying to overcome this by doing this
 (the keys are ID's):
 
 while ($row = mysql_fetch_row($statusQuery)) {
 
$cat = 
 array(0=0,1=0,11=0,12=0,13=0,14=0,15=0,16=0,17=0,19=0);
 
switch ($row[1]) {
case 0: $cat[0] = $row[0]; break;
case 1: $cat[1] = $row[0]; break;
case 11: $cat[11] = $row[0]; break;
case 12: $cat[12] = $row[0]; break;
case 13: $cat[13] = $row[0]; break;
case 14: $cat[14] = $row[0]; break;
case 15: $cat[15] = $row[0]; break;
case 16: $cat[16] = $row[0]; break;
case 17: $cat[17] = $row[0]; break;
case 19: $cat[19] = $row[0]; break;
}
 
print_r($cat);
}
 
 Which gives me this:
 
 Array ( [0] = 15547 [1] = 0 [11] = 0 [12] = 0 [13] = 0 [14] = 0
 [15] = 0 [16] = 0 [17] = 0 [19] = 0 )
 Array ( [0] = 0 [1] = 0 [11] = 0 [12] = 0 [13] = 0 [14] = 0 [15]s
 = 30 [16] = 0 [17] = 0 [19] = 0 )
 
 The query only return 2 rows:
 
 15 | 30
 0 | 15547
 
 What am I doing wrong? Is there a more elegant way to achieve what I want?
 
 Thanks.
 
 -- 
 Paul Halliday
 Ideation | Individualization | Learner | Achiever | Analytical
 http://www.pintumbler.org
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 

Paul-

Why not say:

$cat = array();
if(isset($row[1])
{
$cat[$row[1]] = $row[0];
}

print_r($cat);

Regards,

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



Re: [PHP] Array help.

2010-07-30 Thread Paul Halliday
On Fri, Jul 30, 2010 at 3:44 PM, Joshua Kehn josh.k...@gmail.com wrote:

 On Jul 30, 2010, at 2:36 PM, Paul Halliday wrote:

 I have a query that may not always return a result for a value, I need
 to reflect this with a 0. I am trying to overcome this by doing this
 (the keys are ID's):

 while ($row = mysql_fetch_row($statusQuery)) {

        $cat = 
 array(0=0,1=0,11=0,12=0,13=0,14=0,15=0,16=0,17=0,19=0);

        switch ($row[1]) {
            case 0: $cat[0] = $row[0]; break;
            case 1: $cat[1] = $row[0]; break;
            case 11: $cat[11] = $row[0]; break;
            case 12: $cat[12] = $row[0]; break;
            case 13: $cat[13] = $row[0]; break;
            case 14: $cat[14] = $row[0]; break;
            case 15: $cat[15] = $row[0]; break;
            case 16: $cat[16] = $row[0]; break;
            case 17: $cat[17] = $row[0]; break;
            case 19: $cat[19] = $row[0]; break;
        }

        print_r($cat);
    }

 Which gives me this:

 Array ( [0] = 15547 [1] = 0 [11] = 0 [12] = 0 [13] = 0 [14] = 0
 [15] = 0 [16] = 0 [17] = 0 [19] = 0 )
 Array ( [0] = 0 [1] = 0 [11] = 0 [12] = 0 [13] = 0 [14] = 0 [15]s
 = 30 [16] = 0 [17] = 0 [19] = 0 )

 The query only return 2 rows:

 15 | 30
 0 | 15547

 What am I doing wrong? Is there a more elegant way to achieve what I want?

 Thanks.

 --
 Paul Halliday
 Ideation | Individualization | Learner | Achiever | Analytical
 http://www.pintumbler.org

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


 Paul-

 Why not say:

 $cat = array();
 if(isset($row[1])
 {
    $cat[$row[1]] = $row[0];
 }

 print_r($cat);

 Regards,

 -Josh

I need the results that don't have values assigned though.

ex:

c = 0
h = 9
t = 0
f = 21

Even if the query returned only:

h = 9
f = 21

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



Re: [PHP] Array help.

2010-07-30 Thread Joshua Kehn

On Jul 30, 2010, at 3:03 PM, Paul Halliday wrote:

 
 Paul-
 
 Why are those values not defaulted to 0 in the database?
 
 Regards,
 
 -Josh
 
 
 
 They are defaulted, the query is grouping:
 
 select count(status) as count, status from table group by status order
 by status desc;

Paul-

Correct, so stuff with a status of 0 will still show up unless I'm missing 
something.

Regards,

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



Re: [PHP] php array in different OS

2010-07-27 Thread jose javier parra sanchez
you are probably getting a memory limit error, check your php.ini

2010/7/21 fyang fy...@ipp.ac.cn:
 Dear all,
   I have a simple test code in different OS ,but it give me a different
 result.
   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and
 count:5.

  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it can
 not display count value.
  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

 good luck,

 Yang Fei
 2010-7-20

 --
 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



[PHP] Re: php array in different OS

2010-07-23 Thread Colin Guthrie

'Twas brillig, and fyang at 22/07/10 03:34 did gyre and gimble:

Dear Bob McConnell,
Thank you for your reply.
I really post the same message eight times because of the first e-mail 
authentication.please remove the extra e-mail in your free time.
There are two servers ,the first installation of 32-bit linux(RHEL),the 
second installlation 64-bit linux(CENTOS).
PHP version on 32-bit linux(RHEL):5.2.7
PHP version on 64-bit linux(CENTOS):5.2.13
   I found this problem,because the software transplantation.In the 64-bit 
systems,the array seems to always have limited capacity. I'm not sure that is 
php version problem or need other configurations.


I suspect it's just different configuration.

That said, I've generally found that 64bit versions of PHP need more 
memory than their 32bit equivs, so perhaps all you need to do is 
something like:


ini_set('memory_limit', '50M');

and you'll be fine.

Col


--

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]


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



[PHP] Re: php array in different OS

2010-07-23 Thread Yang Fei
Dear Colin Guthrie ,

 Thanks for your help very much.
 According to your suggestion, I have solved the question.

best wish,
Yang Fei
2010-7-24


RE: [PHP] php array in different OS

2010-07-21 Thread Bob McConnell
From: fyang

 I have a simple test code in different OS ,but it give me a
different 
 result.
 the code as follows:
?php
 $n= 5;
 for($i=0;$i$n;$i++)
 {
  $data[]=array(,$i,$i/1000);
  echo $i,  ,$data[$i][1],br;
 }
 echo count:,count($data);
?
OS1:  Red Hat Enterprise Linux Server release 5.1
  Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
test result:  the result is correct,it can display 5 data and 
 count:5.
 
OS2: CentOS release 5.4
 Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
test result: the result is wrong,it can only display 31148 data and
it 
 can not display count value.
I'm not sure the result relate to array capacity in different OS.
Please give me some tips,thanks in advance.

Did you really have to post the same message eight times?

CentOS is Red Hat minus the proprietary elements, so you actually have
two releases of the same OS here. The bigger question is what version of
PHP are you running on each of them and how are they configured?

Bob McConnell

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



Re: [PHP] php array in different OS

2010-07-21 Thread fyang
Dear Bob McConnell,
   Thank you for your reply.
   I really post the same message eight times because of the first e-mail 
authentication.please remove the extra e-mail in your free time.
   There are two servers ,the first installation of 32-bit linux(RHEL),the 
second installlation 64-bit linux(CENTOS).
   PHP version on 32-bit linux(RHEL):5.2.7
   PHP version on 64-bit linux(CENTOS):5.2.13
  I found this problem,because the software transplantation.In the 64-bit 
systems,the array seems to always have limited capacity. I'm not sure that is 
php version problem or need other configurations.
  Please give further guidance, thank you very much!


best wishs,

Yang Fei
2010-7-22








发件人: Bob McConnell 
发送时间: 2010-07-21  20:06:36 
收件人: fyang; php-general@lists.php.net 
抄送: 
主题: RE: [PHP] php array in different OS 
 
From: fyang

 I have a simple test code in different OS ,but it give me a
different 
 result.
 the code as follows:
 ?php
 $n= 5;
 for($i=0;$i $n;$i++)
 {
  $data[]=array(,$i,$i/1000);
  echo $i,  ,$data[$i][1], br ;
 }
 echo count:,count($data);
? 
OS1:  Red Hat Enterprise Linux Server release 5.1
  Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
test result:  the result is correct,it can display 5 data and 
 count:5.
 
OS2: CentOS release 5.4
 Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
test result: the result is wrong,it can only display 31148 data and
it 
 can not display count value.
I'm not sure the result relate to array capacity in different OS.
Please give me some tips,thanks in advance.

Did you really have to post the same message eight times?

CentOS is Red Hat minus the proprietary elements, so you actually have
two releases of the same OS here. The bigger question is what version of
PHP are you running on each of them and how are they configured?

Bob McConnell


[PHP] php array in different OS

2010-07-20 Thread fyang

Dear all,
   I have a simple test code in different OS ,but it give me a different 
result.

   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and 
count:5.


  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it 
can not display count value.

  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

good luck,

Yang Fei
2010-7-20 



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



[PHP] php array in different OS

2010-07-20 Thread fyang

Dear all,
   I have a simple test code in different OS ,but it give me a different 
result.

   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and 
count:5.


  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it 
can not display count value.

  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

good luck,

Yang Fei
2010-7-20


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



[PHP] php array in different OS

2010-07-20 Thread fyang

Dear all,
   I have a simple test code in different OS ,but it give me a different 
result.

   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and 
count:5.


  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it 
can not display count value.

  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

good luck,

Yang Fei
2010-7-20


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



[PHP] php array in different OS

2010-07-20 Thread fyang

Dear all,
   I have a simple test code in different OS ,but it give me a different 
result.

   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and 
count:5.


  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it 
can not display count value.

  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

good luck,

Yang Fei
2010-7-20


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



[PHP] php array in different OS

2010-07-20 Thread fyang

Dear all,
   I have a simple test code in different OS ,but it give me a different 
result.

   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and 
count:5.


  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it 
can not display count value.

  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

good luck,

Yang Fei
2010-7-20 



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



[PHP] php array in different OS

2010-07-20 Thread fyang

Dear all,
   I have a simple test code in different OS ,but it give me a different 
result.

   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and 
count:5.


  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it 
can not display count value.

  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

good luck,

Yang Fei
2010-7-20 



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



[PHP] php array in different OS

2010-07-20 Thread Yang Fei
Dear all,
I have a simple test code in different OS ,but it give me a different 
result.
the code as follows:
   ?php
$n= 5;
for($i=0;$i$n;$i++)
{
 $data[]=array(,$i,$i/1000);
 echo $i,  ,$data[$i][1],br;
}
echo count:,count($data);
   ?
   OS1:  Red Hat Enterprise Linux Server release 5.1 
 Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
   test result:  the result is correct,it can display 5 data and 
count:5.

   OS2: CentOS release 5.4 
Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
   test result: the result is wrong,it can only display 31148 data and it can 
not display count value.
   I'm not sure the result relate to array capacity in different OS.
   Please give me some tips,thanks in advance. 

good luck,

Yang Fei
2010-7-20


[PHP] php array in different OS

2010-07-20 Thread fyang

Dear all,
   I have a simple test code in different OS ,but it give me a different 
result.

   the code as follows:
  ?php
   $n= 5;
   for($i=0;$i$n;$i++)
   {
$data[]=array(,$i,$i/1000);
echo $i,  ,$data[$i][1],br;
   }
   echo count:,count($data);
  ?
  OS1:  Red Hat Enterprise Linux Server release 5.1
Linux 2.6.18-53.el5xen i686 i686 i386 GNU/Linux
  test result:  the result is correct,it can display 5 data and 
count:5.


  OS2: CentOS release 5.4
   Linux 2.6.18-164.el5 x86_64 x86_64 x86_64 GNU/Linux
  test result: the result is wrong,it can only display 31148 data and it 
can not display count value.

  I'm not sure the result relate to array capacity in different OS.
  Please give me some tips,thanks in advance.

good luck,

Yang Fei
2010-7-20 



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



RE: [PHP] Array form processing

2010-06-30 Thread Ford, Mike
 -Original Message-
 From: Ron Piggott [mailto:ron.pigg...@actsministries.org]
 Sent: 29 June 2010 22:22
 
 Am I on the right track?  I don't know what to do with the second
 FOREACH

Sort of.

 
 ?php
 
 foreach($_REQUEST as $key = $val) {
  $$key = $val;
echo $key . :  . $val . br;
 
if ( $val == Array ) {

I would prefer to use is_array() here:

if (is_array($val))

   $i=0;
 

At this point, you've just proved that $val is an array (whichever test you 
use!), so simply foreach it like one. I know you know how to do that as you did 
it with $_REQUEST above!

   foreach ($val) {
   echo $val[$i]br;
   $i++;
   }

foreach ($val as $option) {
   echo $optionbr /\n;
}

 
}
 }
 
 ?

Cheers!

Mike
 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.f...@leedsmet.ac.uk 
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] Array form processing

2010-06-30 Thread tedd

At 4:54 PM -0400 6/29/10, Ron Piggott wrote:

I am trying to process a form where the user uses checkboxes:

input type=checkbox name=painDesc[] value=1 /Sharp
input type=checkbox name=painDesc[] value=2 /Stabbing
input type=checkbox name=painDesc[] value=3 /Jabbing

When I do:

foreach($_REQUEST as $key = $val) {
 $$key = $val;
 echo $key . :  . $val . br;
}

The output is:

painDesc: Array

I need to know the values of the array (IE to know what the user is
checking), not that there is an array.  I hope to save these values to the
database.

Thank you.

Ron


Ron:

Try this:

http://php1.net/b/form-checkbox/

If you want the form to retain the values, try this:

http://php1.net/b/form-checkbox1/


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



[PHP] Array form processing

2010-06-29 Thread Ron Piggott

I am trying to process a form where the user uses checkboxes:

input type=checkbox name=painDesc[] value=1 /Sharp
input type=checkbox name=painDesc[] value=2 /Stabbing
input type=checkbox name=painDesc[] value=3 /Jabbing

When I do:

foreach($_REQUEST as $key = $val) {
 $$key = $val;
 echo $key . :  . $val . br;
}

The output is:

painDesc: Array

I need to know the values of the array (IE to know what the user is
checking), not that there is an array.  I hope to save these values to the
database.

Thank you.

Ron


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



Re: [PHP] Array form processing

2010-06-29 Thread Ashley Sheridan
On Tue, 2010-06-29 at 16:54 -0400, Ron Piggott wrote:

 I am trying to process a form where the user uses checkboxes:
 
 input type=checkbox name=painDesc[] value=1 /Sharp
 input type=checkbox name=painDesc[] value=2 /Stabbing
 input type=checkbox name=painDesc[] value=3 /Jabbing
 
 When I do:
 
 foreach($_REQUEST as $key = $val) {
  $$key = $val;
echo $key . :  . $val . br;
 }
 
 The output is:
 
 painDesc: Array
 
 I need to know the values of the array (IE to know what the user is
 checking), not that there is an array.  I hope to save these values to the
 database.
 
 Thank you.
 
 Ron
 
 


You need to iterate that array, as that holds the values of everything
sent by the browser

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




Re: [PHP] Array form processing

2010-06-29 Thread Shreyas Agasthya
The painDesc array is what that should be iterated.

--Shreyas

On Wed, Jun 30, 2010 at 2:27 AM, Ashley Sheridan
a...@ashleysheridan.co.ukwrote:

 On Tue, 2010-06-29 at 16:54 -0400, Ron Piggott wrote:

  I am trying to process a form where the user uses checkboxes:
 
  input type=checkbox name=painDesc[] value=1 /Sharp
  input type=checkbox name=painDesc[] value=2 /Stabbing
  input type=checkbox name=painDesc[] value=3 /Jabbing
 
  When I do:
 
  foreach($_REQUEST as $key = $val) {
   $$key = $val;
 echo $key . :  . $val . br;
  }
 
  The output is:
 
  painDesc: Array
 
  I need to know the values of the array (IE to know what the user is
  checking), not that there is an array.  I hope to save these values to
 the
  database.
 
  Thank you.
 
  Ron
 
 


 You need to iterate that array, as that holds the values of everything
 sent by the browser

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





-- 
Regards,
Shreyas Agasthya


Re: [PHP] Array form processing

2010-06-29 Thread Ron Piggott
Am I on the right track?  I don't know what to do with the second FOREACH

?php

foreach($_REQUEST as $key = $val) {
 $$key = $val;
 echo $key . :  . $val . br;

 if ( $val == Array ) {
$i=0;

foreach ($val) {
echo $val[$i]br;
$i++;
}

 }
}

?


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



Re: [PHP] Array form processing

2010-06-29 Thread Jim Lucas
Ron Piggott wrote:
 I am trying to process a form where the user uses checkboxes:
 
 input type=checkbox name=painDesc[] value=1 /Sharp
 input type=checkbox name=painDesc[] value=2 /Stabbing
 input type=checkbox name=painDesc[] value=3 /Jabbing
 
 When I do:
 
 foreach($_REQUEST as $key = $val) {
  $$key = $val;
echo $key . :  . $val . br;
 }
 
 The output is:
 
 painDesc: Array
 
 I need to know the values of the array (IE to know what the user is
 checking), not that there is an array.  I hope to save these values to the
 database.
 
 Thank you.
 
 Ron
 
 

Think about it...

You would not ?php echo $_REQUEST; ? and expect to get the value of any form
field would you.  No, you wouldn't.

Given the following form...

form
Titleinput type=text name=title value= /br /
Subjectinput type=text name=subject value= /br /
input type=submit name=submit value=Send it! /
/form

on the processing page, I would access those variables by writing the following.

echo $_REQUEST['title'];
echo $_REQUEST['subject'];

With that said, going back to your issue, you would do this:

if ( $_REQUEST['painDesc']  count($_REQUEST['painDesc']) ) {
  foreach($_REQUEST['painDesc'] as $key = $val) {
echo {$key}:{$val}br /;
  }
}

-- 
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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



[PHP] array key's: which is correct?

2010-06-08 Thread Tanel Tammik
Hi,

which one is correct or better?

$array[3] = '';
or
$array['3'] = '';

$i = 7;

$array[$i] = '';
or
$array[$i] = '';


Br
Tanel 



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



Re: [PHP] array key's: which is correct?

2010-06-08 Thread Paul M Foster
On Tue, Jun 08, 2010 at 04:12:42PM +0300, Tanel Tammik wrote:

 Hi,
 
 which one is correct or better?
 
 $array[3] = '';
 or
 $array['3'] = '';

If the index for (integer) 3, the first example is correct. If the index
is (string) '3', the second example is correct.

 
 $i = 7;
 
 $array[$i] = '';
 or
 $array[$i] = '';
 

There's no reason to use $i. The end result will be the same, but in
the case of $i, you're forcing the PHP interpreter to interpret the
string $i, looking for variables (like $i), and output whatever else
is in the string (which in this case is nothing). Also, if $i is an
integer, you have the same problem as above. In the first case, you get
$array[7]. In the second case, you get $array['7'].

Paul

-- 
Paul M. Foster

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



Re: [PHP] array key's: which is correct?

2010-06-08 Thread Ashley Sheridan
On Tue, 2010-06-08 at 16:12 +0300, Tanel Tammik wrote:

 Hi,
 
 which one is correct or better?
 
 $array[3] = '';
 or
 $array['3'] = '';
 
 $i = 7;
 
 $array[$i] = '';
 or
 $array[$i] = '';
 
 
 Br
 Tanel 
 
 
 


The two indexes are equivalent, although I reckon the integer one will
give better performance over the string.

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




  1   2   3   4   5   6   7   8   9   10   >