Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2010, 10:31 PM   PM User | #1
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Thumbs up Custom Array Function

I thought I would post this function I created for others to use.

PHP Code:
<?php
function custom_array($str){
  
/* Config Options */
  
$array_prefix  "[:]"// Custom array start:finish ':' split them up
  
$array_split   ":"// Array split (i:0;) ':' splits the key and var
  
$var_end       ";"// Var end (i:0;) ';' ends the current var
  /* Config Options */
  
list($as,$af)  = split(':',$array_prefix); // Splitting up the array prefix's
  
if(substr($str,0,1) == $as && substr($str,-1) == $af){ // Checks custom array start/finish
    
$array = array(); // Starts the array
    
foreach(split($var_end,substr($str,1,-1)) As $create){ // Extracting each array query
      
if($create != ""){ // Checks if array vars contain something
        
list($key,$value) = split($array_split,$create); // Splits custom array vars
        
if($value != ""){ // Checks if array value contain something
          
$array[$key] = $value// Sets array key / value
        
}
      }
    }
    return 
$array// Returns the final array
  
}
}
$entry "[i:0;j:23;ii:1;]"// My custom array string
$entry custom_array($entry); // array([i] => 0 [j] => 23 [ii] => 1);
print $entry[j]; // returns '23'
?>
Hope people like my function which you can mod to how you want your custom array to look
e.g. ([i::0],[j::23],[ii::1])
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P

Last edited by DJCMBear; 04-07-2010 at 11:59 PM..
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:31 AM.


Advertisement
Log in to turn off these ads.