SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Modifying array keys

    Hi everyone,

    I'm trying to loop through an array and modify the array keys:

    PHP Code:
    foreach($dates as &$key => $value) {

        if(
    $key == $today) {
            
    $key 'Today';
        } elseif(
    $key == $yesterday) {
            
    $key 'Yesterday';
        } elseif(
    date('W'strtotime($key)) == $thisweek) {
            
    $key date('l'strtotime($key));
        } else {
            
    $key date('l jS F');
        }


    When I run this code I get a blank page (despite error_reporting being set to E_ALL at the top - how helpful).

    When I remove the & from in front of $key it runs, but obviously doesn't modify the key of the $dates array. It would seem that PHP doesn't allow you to pass the keys by reference, but I can't believe that this is the problem.

    Anyone know what's up here?

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cannot do like that. $key and $value are temporary variables generated in the loop, they do not point back to original array

    Try something like this
    Code PHP:
    foreach($dates as &$key => $value) {
        if($key == $today) {
            unset($dates[$today]);
            $dates['Today'] = $value;
        }
    .........
    .........
    }

    I am not sure why you doing something like this

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    You cannot make a key a reference like that, resulting in a "Fatal error: Key element cannot be a reference" message (if errors are being displayed).

    Can you paste more of the code to give us an idea of what you've got and what you're trying to achieve?
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification - I thought it was unlikely that $value could be passed by reference but not $key, but I was wrong

    I know of another way (albeit more long-winded) that I can go about doing this, so I'll go down that route instead.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for why you didn't get an error message, keep in mind php first parses and compiles your script before executing it, one file at a time. If you have a serious error, then php never makes it past the parsing/compiling stage, which means nothing gets executed.

    When you try to set configuration options in the script, such as the error_reporting level, or ini options, those are interpreted at runtime. Which means php needs to actually make it to the execution phase for that file

    If you can't change the config on the php.ini or webserver level, you can do a trick. Make a new simple script which wraps the problematic script, changing the config before including the other script. includes are parsed at runtime, so it works
    PHP Code:
    error_reporting(E_ALL);
    ini_set('display_errors'1);

    include 
    'problemscript.php'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •