Jump to content

Help With Array: Need to set array value depending on value with higher priority


Recommended Posts

Currently, I am trying to make my URL shortener script able to track UTM variables from Google Analytics. I also want to provide my own (campaign/keyword/source/etc...) variables.

 

I am trying to sift through $_GET, pick out "approved" key/value pairs and set each key with the condition that UTM variables take higher precedence (I only want to store one value in the database).

 

My code is currently:

 

//Parse $_GET and only get the key/pairs that we need and store them in global $url_params for use through out the script
function set_url_params($allowed=NULL) { // $allowed is arg to add new values in future dev 
global $url_params;

$allowed = array( 
    'l', // Redirect key (e.g http://example.com?l=a1dFd7)
    'utm_campaign', 
    'campaign', 
    'utm_source' , 
    'source', 
    'utm_medium', 
    'medium', 
    'utm_term', 
    'term', 
    'keyword',
    'kw',
    );

    $approved = array_intersect_key($_GET, array_flip($allowed));

    foreach($approved as $key => $value) {
        strip_tags(urldecode(trim($value)));
        $url_params[$key] = $value;
    }

    //Assign variables to global $url_params variable so other functions can use them.
    //NOTE: Google Analytics UTM parameters take precedence over script values.
    $url_params['l'] = isset($approved['l']) ? $approved['l'] : NULL ;
    $url_params['campaign'] = isset($approved['utm_campaign']) ? $approved['utm_campaign'] : $approved['campaign'];
    $url_params['source'] = isset($approved['utm_source']) ? $approved['utm_source'] : $approved['source'];
    $url_params['medium'] = isset($approved['utm_medium']) ? $approved['utm_medium'] : $approved['medium'];
    $url_params['term'] = isset($approved['utm_term']) ? $approved['utm_term'] : $approved['term'];
    $url_params['keyword'] = isset($approved['keyword']) ? $approved['keyword'] : $approved['kw'];
    // Just in case $url_params doesn't have a 'keyword' set, we will use 'term' instead.
    $url_params['keyword'] = isset($url_params['keyword']) ? $url_params['keyword'] : $url_params['term'];

}

 

I basically want to find a cleaner way of doing this without all the isset()'s. I also get NOTICE errors (running in E_ALL) for undefined variables that I would like to unset so I don't get those errors.

 

Any help would be appreciated. Thanks.

 

        strip_tags(urldecode(trim($value)));

That line does nothing.

 

 

global $url_params;

Don't use globals, pass arguments in and return the data you want.

 

 

with the condition that UTM variables take higher precedence (I only want to store one value in the database).
Use if(isset()) for this.  If the UTM is set, use it.  Otherwise, use your own.

 

There's less confusing ways to do what you're doing, but this code is valid.

 

-Dan

@ManicDan: I am currently using global $url_params because I haven't finished my script yet. I am not sure where I might need that data so I pushed it into the global scope for now.

 

I would like to condense all the is_set()'s into a foreach statement but I am unsure how to order things so 'utm_campaign' takes higher priority over 'campaign'.

 

The key/values are taken from $_GET and I want to condense the values into my 'campaign', 'source', 'term', etc... keys and unset all the UTM variable. So if the URL was index.php?utm_campaign=testing&utm_source=google.com&source=google. I would want the $url_params array to look like:

 

$url_params['campaign'] = 'testing';
$url_params['source'] = 'google.com';

 

Since 'utm_source' is set, I want it to override 'source'.

 

I can't seem to get past:

 


    foreach($approved as $key => $value) {
        strip_tags(urldecode(trim($value))); // Why doesn't this work? I want to strip tags, replace %20's with 'spaces' and remove any HTML tags.

       if(is_set($key)) {
           $url_params[$key] = $value;
      }
}

 

This is fine if I want to have both 'utm_campaign' and 'campaign' set. I just need some help on making the foreach statement do the replacing.

 

Thanks for the help.

Your line doesn't work because you need to return the value of a function call to something.

 

This is a working version of your function with test code.  There's one special case that I could have gotten around with fancy logic but it would have been confusing so it's hard-coded.

 

 

<?php

function set_url_params( $get, $allowed = null )
{
  $allowed = array( 
    'l' => 'l', // Redirect key (e.g http://example.com?l=a1dFd7)
    'utm_campaign' => 'campaign', 
    'campaign' => 'campaign', 
    'utm_source' => 'source', 
    'source' => 'source', 
    'utm_medium' => 'medium', 
    'medium' => 'medium', 
    'utm_term' => 'term', 
    'term' => 'term', 
    'keyword' => 'keyword',
    'kw' => 'keyword',
    );
    
  $url_params = array();
  foreach ( $allowed as $key => $destination )
  {
    if ( !isset( $url_params[$destination] ) && isset($get[$key]) )
    {
      $url_params[$destination] = trim(strip_tags(urldecode($get[$key])));
    }
  }
  
  //one more check for kw:
  if ( !isset( $url_params['keyword'] ) && isset($get['term']) )
  {
    $url_params['keyword'] = trim(strip_tags(urldecode($get['term'])));
  }
    
  return $url_params;
}

//test it:
$_GET = array(
    'kw' => 'foo',
    'l' => 'http://example.com?l=a1dFd7', 
    'source' => '456xyz', 
    'utm_campaign' => 'abc123Google', 
    'campaign' => 'abc123', 
    'utm_source' => '456xyzGoogle', 
    'utm_medium' => 'oilOnGoogle', 
    'medium' => 'oilOnPaper', 
    'utm_term' => 'phpGoogle', 
    'term' => 'php', 
    'keyword' => 'fooBar',
);
print_r(set_url_params($_GET));

$_GET = array(
    'l' => 'http://example.com?l=a1dFd7', 
    'utm_campaign' => 'abc123Google', 
    'utm_medium' => 'oilOnGoogle', 
    'utm_term' => 'phpGoogle', 
    'term' => 'php', 
    'keyword' => 'fooBar',
);
print_r(set_url_params($_GET));


$_GET = array(
    'l' => 'http://example.com?l=a1dFd7', 
    'source' => '456xyz', 
    'utm_campaign' => 'abc123Google', 
    'campaign' => 'abc123', 
    'medium' => 'oilOnPaper', 
    'utm_term' => 'phpGoogle', 
    'term' => 'php', 
);
print_r(set_url_params($_GET));

$_GET = array(
    'kw' => 'foo',
    'l' => 'http://example.com?l=a1dFd7', 
    'source' => '456xyz', 
    'campaign' => 'abc123', 
    'medium' => 'oilOnPaper', 
    'term' => 'php', 
    'keyword' => 'fooBar',
);
print_r(set_url_params($_GET));

@ManicDan: I appreciate the help. I am fairly new to PHP and have been trying to find a clean way to do this. I have been racking my brain for about a week and I couldn't come up with a solution for it, hence my question. I do my best to not come off as a "Help Vampire" by looking through others code to see if I can find a better way of doing something. I just couldn't find any snippets that talked about this problem or was about prioritizing values in an array.

 

 

 

 

@ManicDan: I appreciate the help. I am fairly new to PHP and have been trying to find a clean way to do this. I have been racking my brain for about a week and I couldn't come up with a solution for it, hence my question. I do my best to not come off as a "Help Vampire" by looking through others code to see if I can find a better way of doing something. I just couldn't find any snippets that talked about this problem or was about prioritizing values in an array.

The "Help Vampire" link is a part of my standard signature and not directed at you.  Did you try to code I gave you? 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.