tags:

views:

39

answers:

3

I'm trying to call to a specific part of an array with the key # and it's not working. I can output the array and see it...

Array
(
    [6] => Array
        (
            [0] => [email protected]
            [1] => [email protected]
        )

    [7] => Array
        (
            [0] => [email protected]
            [1] => [email protected]
        )

    [8] => Array
        (
            [0] => [email protected]
            [1] => [email protected]
        )

)

This array is $emailDB. I can call to the array manually with $emailDB[7] and it works, but if my call is dynamic like this it won't work...

<?php
$value = 7;
print_r($emailDB[$value]);
?>

I've never had an issue like this with an array so it's very odd. What really sucks is I'm under deadline with a form not working on a client's site...joy.

We tried this with no luck...

<?php
$value = 7;
print_r($emailDB[intval($value)]);
?>

I thought intval() would assist but it did not.

+2  A: 

Well, you are echoing an Array, which I assume is printing "Array" onto your screen. If you want to echo the actual contents of the array, you need to use print_r($array) or echo print_r($array, true). You can also try putting the value in quotes, like $emailDB["{$value}"] to see if that works, I sometimes have troubles with integers not going into things properly.

animuson
sorry...I mean print_r()Either way, I still cannot access the value when the key is dynamically passed. Manually typed it works. Weird.
Will Ashworth
I updated the original post. That was a quick type up in attempt to describe the issue short of having to post the long, actual code.
Will Ashworth
I didn't know about the $emailDB["{$value}"] method. Excellent and thank you for sharing this. The MySQL fixes when re-written seem to have resolved all of the issues and the application is much cleaner than working from the flat file.
Will Ashworth
A: 

You're post implies a bug in php itself, which I highly doubt. What's more likely is that what you posted doesn't properly represent the code you're running.

Why don't try this. Make a brand new empty php file. Hardcode the array keys and values and assign them to the $emailDB variable, and then try

$value = 7;
print_r($emailDB[$value]);

You will see you don't have the problem that you claim. You have now started the debugging process, and now you can look at the working, and non working code to compare the difference.

chris
I agree with you. It had to have been something whacky with how we were pulling in the data somehow. It was a tab-separated file we were exploding.I just re-wrote the whole thing entirely and imported the data into MySQL and all was well.I hindsight, I have a sneaking suspicion it was a trim() command that was needed and likely nothing more. Dammit...too late, but I learned something about checking over the code for those types of things.
Will Ashworth
A: 

I agree with you all. It had to have been something whacky with how we were pulling in the data somehow. It was a tab-separated file we were exploding. I just re-wrote the whole thing entirely and imported the data into MySQL and all was well.

In hindsight, I have a sneaking suspicion it was a trim() command that was needed and likely nothing more. Dang it...too late, but I learned something about checking over the code for those types of things.

Will Ashworth