Current location: Hot Scripts Forums » Programming Languages » PHP » Can I access array elements this way?


Can I access array elements this way?

Reply
  #1 (permalink)  
Old 08-15-04, 12:11 PM
jack_mcs jack_mcs is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Can I access array elements this way?

Given
$array = array("a", "b", "c");
$idx = getPos($url);

Should I be able to do this:
echo $array[$idx];

I tried the above but it doesn't ouput a value in the array. $idx is a substr taken from a return url and is within the arrays range. How do I access the array using $idx?

Jack
Reply With Quote
  #2 (permalink)  
Old 08-16-04, 02:45 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
If I'm right, there shouldn't be any problem as far as your getPos() will actually return valid index for that array. Maybe you could use $_GET['var'] to get your value more easily?

Assuming:
-you have the following code in array.php
-you call it with your browser like: array.php?index=1

The following code (hopefully) does your trick:

PHP Code:

<?php


$array 
= array("a""b""c");

/* check for numeric index value in the URL*/
if( isset($_GET['index']) AND is_numeric($_GET['index']) ) {
    
/* is_numeric accepts also floats -> cast index to integer */
    
$index = (int) $_GET['index'];
    
/* see if given index is valid in $array */
    
if( array_key_exists($index$array) ) {
        echo 
"element at $index$array[$index]\n";
    }
    else {
        echo 
"invalid index: $index\n";
    }
}
else {
    echo 
"no numeric index given.\n";
}
?>

Last edited by darkfreak; 08-16-04 at 02:50 AM.
Reply With Quote
  #3 (permalink)  
Old 08-16-04, 10:41 PM
jack_mcs jack_mcs is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
The function getPos() returns a string. I'm somewhat new to php and was under the impression that strings and numbers were treated the same. That, apparently, is not true in this situation. As soon as I read your suggestion of using is_numeric I realized the problem. I added a cast so the code now reads $array[(int)$idx]; and it works. Thanks for the help.

Jack
Reply With Quote
  #4 (permalink)  
Old 08-17-04, 12:06 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
No problem Actually I started thinking that again after I replied to you, and I had a feeling that the problem might be just there (no casting done), altough I also thought that casting would have been done automatically.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i return the maximum of the array elements? toezaurus81 C/C++ 1 06-17-04 03:30 PM
file into an array, then appending array only if no duplicates djpennin Perl 2 05-17-04 01:04 PM
How do I write an array to a file? bark PHP 2 03-30-04 12:28 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM


All times are GMT -5. The time now is 04:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.