[PHP] Re: Simple Array Question...

2006-10-05 Thread Colin Guthrie
Russell Jones wrote:
 lets say I have an array...
 
 $myArray = array (
 
 firstkey = first val,
 secondkey = second val
 
 )
 
 
 Can I still call these by their numeric order? ie, echo $myArray[0] shoudl
 print out first val...


No, but you can do:
$myArrayNumeric = array_values($myArray);
echo $myArrayNumeric[0];

Col.

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



[PHP] Re: Simple array question, array delete

2005-04-06 Thread Satyam
Anyway, as the replies already show, you have to have a key to identify the 
record to be deleted, otherwise, I don't see there can be a way.  Why don't 
use the data as key as well?

Satyam

Ryan A [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Hey,
 I have a $data_recs array like this:

 12
 445
 45655
 4
 343
 etc

 when the user gives me a number, i have to check if its in the array and
 delete that entry...how do i do that?
 I have looked at the manual but have gotten confused with array
 pop,splice,array_key_exists etc

 Thanks,
 Ryan



 -- 
 No virus found in this outgoing message.
 Checked by AVG Anti-Virus.
 Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005 

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



[PHP] Re: Simple array question

2003-10-23 Thread pete M
$array = array(
'fruit1' = 'apple',
'fruit2' = 'orange',
'fruit3' = 'grape',
'fruit4' = 'apple',
'fruit5' = 'apple');
// this cycle echoes all associative array
// key where value equals apple
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'br';
}
next($array);
}


René fournier wrote:
Is there a simple way to return the key (name) of one element in an 
array? I looked up key() in the docs, but there are no examples or notes...

Thanks.

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


[PHP] Re: Simple array question

2003-10-22 Thread Rob Adams
René fournier [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Is there a simple way to return the key (name) of one element in an
 array? I looked up key() in the docs, but there are no examples or
 notes...

key() will do it, but you have to have the internal pointer already on the
element you want.  If you're not using next(), prev(), etc, then what you
probably want to use it array_search().

  -- Rob




 Thanks.

 ...Rene


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



[PHP] Re: Simple Array question

2003-07-29 Thread Irvin Amoraal
Normally array values start at 0 (zero), but let's say sh1=1 and sh2=2. then
you could echo the values out like this: echo ID 1: $id[1]brID 2:
$id[2]br;

Irvin.
_

Ryan A [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Hi,
 If i am posting something like this from a form:
  input type=checkbox name='id[sh1]' value=3
  input type=checkbox name='id[sh2]' value=4

 How do i get the value of id[]?
 eg:
 I dont want the sh1 and sh2, i just want the 3 and 4

 The reason i dont wan the sh1 and sh2 is those are dynamic and will be
 changing, I just need the values (in this case 3 and 4) to do some
 comparasion and assignment operations

 Kindly reply.

 Thank,
 -Ryan




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



Re: [PHP] Re: Simple Array question

2003-07-29 Thread Ryan A
Hi,
Thanks for replying.
I tried that and have 2 numbers like so:

input name=id[sh123] type=hidden value=32
input name=id[sh1sd] type=hidden value=563

and tried to echo it like so:
?php
$id=$_POST['id'];
foreach($_POST['id'] AS $row)
echo $row;
?
Echo of one is ?php echo $row[0]; ? and echo of two is:b ?php echo
$row[1]; ?/bbrbr
Echo of one is ?php echo $id[0]; ? and echo of two is:b ?php echo
$id[1]; ?/b

the output i got was:
**
32563Echo of one is 5 and echo of two is: 6

Echo of one is and echo of two is:
*


AS you can see its totally wrong.Any other ideas?

cheers,
-Ryan


 Normally array values start at 0 (zero), but let's say sh1=1 and sh2=2.
then
 you could echo the values out like this: echo ID 1: $id[1]brID 2:
 $id[2]br;

 Irvin.





  Hi,
  If i am posting something like this from a form:
   input type=checkbox name='id[sh1]' value=3
   input type=checkbox name='id[sh2]' value=4
 
  How do i get the value of id[]?
  eg:
  I dont want the sh1 and sh2, i just want the 3 and 4
 
  The reason i dont wan the sh1 and sh2 is those are dynamic and will be
  changing, I just need the values (in this case 3 and 4) to do some
  comparasion and assignment operations
 
  Kindly reply.
 
  Thank,
  -Ryan
 



 --
 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: Simple Array question (conclusion)

2003-07-29 Thread Ryan A
I GOT IT

this is where the data was coming from:
 input name=id[sh123] type=hidden id=id[sh123] value=32
  input name=id[sh1sd] type=hidden id=id[sh1sd] value=563

and this is where i take the values and dump it into an array/variables that
i can call when and where i please:

?php
$nn=0;
foreach($id as $nname = $pppno)
{$asdf[$nn]=$pppno;
 $nn++;  }
?
brbr?php echo $asdf[0]; ?br
?php echo $asdf[1]; ?


I just took out some of the breaks so it will take less space but you get
the idea.

Thank you to everyone who tried to help, but maybe i didnt explain the
problem well enough that you didnt get the answer or just didnt want to :-D

Cheers,
-Ryan



- Original Message -
From: Irvin Amoraal [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Tuesday, July 29, 2003 11:59 PM
Subject: [PHP] Re: Simple Array question


 Normally array values start at 0 (zero), but let's say sh1=1 and sh2=2.
then
 you could echo the values out like this: echo ID 1: $id[1]brID 2:
 $id[2]br;

 Irvin.
 _

 Ryan A [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
  Hi,
  If i am posting something like this from a form:
   input type=checkbox name='id[sh1]' value=3
   input type=checkbox name='id[sh2]' value=4
 
  How do i get the value of id[]?
  eg:
  I dont want the sh1 and sh2, i just want the 3 and 4
 
  The reason i dont wan the sh1 and sh2 is those are dynamic and will be
  changing, I just need the values (in this case 3 and 4) to do some
  comparasion and assignment operations
 
  Kindly reply.
 
  Thank,
  -Ryan
 



 --
 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] Re: Simple Array question

2003-07-29 Thread John W. Holmes
Ryan A wrote:
Hi,
Thanks for replying.
I tried that and have 2 numbers like so:
input name=id[sh123] type=hidden value=32
input name=id[sh1sd] type=hidden value=563
and tried to echo it like so:
?php
$id=$_POST['id'];
foreach($_POST['id'] AS $row)
echo $row;
?
Echo of one is ?php echo $row[0]; ? and echo of two is:b ?php echo
$row[1]; ?/bbrbr
Echo of one is ?php echo $id[0]; ? and echo of two is:b ?php echo
$id[1]; ?/b
the output i got was:
**
32563Echo of one is 5 and echo of two is: 6
Echo of one is and echo of two is:
*
AS you can see its totally wrong.Any other ideas?
No... you're getting exactly what you ask for. You rely on this list to 
much.

1. You have no braces so your foreach() is taking the next line as the 
part that should be executed on each loop.

2. Loop 1 sets $row = 32. Then it's displayed

3. Loop 2 sets $row = 563. Then it's displayed

4. So now $row = 563. When you display $row[0], you're asking for the 
first character of $row, so 5 is displayed. $row[1] displays 6.

5. Now you go back to $id. Well, $id = array('sh123' = 32, 'sh1sd' = 
563). So, when you try to display $id[0] and $id[1], there are no 
cooresponding values, so nothing is displayed.

Now, how do you do this correctly?

foreach($_POST['id'] as $key = $value)
{ echo The random key is $key, the value is $value.br /; }
which will give you:

The random key is sh123, the value is 32.
The random key is sh1sd, the value is 563.
What do you want, besides that?

--
---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

PHP|Architect: A magazine for PHP Professionals  www.phparch.com





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


Re: [PHP] Re: Simple Array question (conclusion)

2003-07-29 Thread John W. Holmes
Ryan A wrote:

I GOT IT

this is where the data was coming from:
 input name=id[sh123] type=hidden id=id[sh123] value=32
  input name=id[sh1sd] type=hidden id=id[sh1sd] value=563
and this is where i take the values and dump it into an array/variables that
i can call when and where i please:
?php
$nn=0;
foreach($id as $nname = $pppno)
{$asdf[$nn]=$pppno;
 $nn++;  }
?
brbr?php echo $asdf[0]; ?br
?php echo $asdf[1]; ?
I just took out some of the breaks so it will take less space but you get
the idea.
Thank you to everyone who tried to help, but maybe i didnt explain the
problem well enough that you didnt get the answer or just didnt want to :-D
Seriously?

You could just replace the above with:

$asdf = array_values($_POST['id']);

I think you need to be cut off from the list for a while. Trial by fire, 
I say!!

--
---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

PHP|Architect: A magazine for PHP Professionals  www.phparch.com





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


[PHP] John-Re: [PHP] Re: Simple Array question (conclusion)

2003-07-29 Thread Ryan A
Hey John,
I guess you are right, I do rely on this list quite a bit but am not the
only one.
Am learning by my mistakes though and finding solutions...they may not be
the best solutions but they still work, everyone has to learn.
Thanks for replying to the question though. And i didnt know about the
array_values thingsee? the more you reply to me the more i learn :-D
Cheers,
-Ryan



 Ryan A wrote:

  I GOT IT
 
  this is where the data was coming from:
   input name=id[sh123] type=hidden id=id[sh123] value=32
input name=id[sh1sd] type=hidden id=id[sh1sd] value=563
 
  and this is where i take the values and dump it into an array/variables
that
  i can call when and where i please:
 
  ?php
  $nn=0;
  foreach($id as $nname = $pppno)
  {$asdf[$nn]=$pppno;
   $nn++;  }
  ?
  brbr?php echo $asdf[0]; ?br
  ?php echo $asdf[1]; ?
 
 
  I just took out some of the breaks so it will take less space but you
get
  the idea.
 
  Thank you to everyone who tried to help, but maybe i didnt explain the
  problem well enough that you didnt get the answer or just didnt want to
:-D

 Seriously?

 You could just replace the above with:

 $asdf = array_values($_POST['id']);

 I think you need to be cut off from the list for a while. Trial by fire,
 I say!!

 --
 ---John Holmes...

 Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

 PHP|Architect: A magazine for PHP Professionals  www.phparch.com






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