Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1571 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

can i get the name of the array????

Thread title: can i get the name of the array????
Closed Thread    
    Thread tools Search this thread Display Modes  
06-13-2007, 11:33 AM
#1
montyauto is offline montyauto
montyauto's Avatar
Status: Junior Member
Join date: Apr 2006
Location:
Expertise:
Software:
 
Posts: 68
iTrader: 0 / 0%
 

montyauto is on a distinguished road

  Old  can i get the name of the array????

i'm having lots of arrays with values and i have a funtion which does some operations based on the array passed as an argument..

(i.e) i want to check the name of the array and then do operations based on that.

So i want to get the name of the array passed into the funtion .

for eg:


if $cutomer = array('sefdsf','sdfsdf','sdfsdf');
i want to get the value 'customer'

is there any function to do this???

06-13-2007, 11:56 AM
#2
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

PHP Code:
<?php

/*
    Getting variable names
    From: http://uk.php.net/manual/en/language.variables.php#49997
*/

$fruits = array('apple''banana''orange');
$colours = array('blue''red''yellow');

echo 
myfunc($fruits); // Output: We have fruits!
echo myfunc($colours); // Output: We have colours!



// Note the pass by reference (&$item), this is important
function myfunc(&$item)
{
    switch(
vname($item))
    {
        case 
'fruits':
            return 
'We have fruits!';
        case 
'colours':
            return 
'We have colours!';
        default:
            return 
'Unknown variable';
    }
}

function 
vname(&$var$scope false$prefix 'unique'$suffix 'value')
{
    
$vals = ($scope) ? $scope $GLOBALS;
    
$old $var;
    
$var $new $prefix rand() . $suffix;
    
$vname FALSE;
    foreach(
$vals as $key => $val)
        if(
$val === $new
            
$vname $key;
    
$var $old;
    return 
$vname;
}

?>

06-18-2007, 10:47 AM
#3
vesselin is offline vesselin
Status: I'm new around here
Join date: Feb 2007
Location: Bulgaria
Expertise:
Software:
 
Posts: 20
iTrader: 0 / 0%
 

vesselin is on a distinguished road

  Old

In PHP several variables can share the same object. For example:

$customer = array('sefdsf','sdfsdf','sdfsdf');
$customer_2 = $customer;
$customer_3 = $customer;

All the 3 variables $customer, $customer_2 and $customer_3 will not only contain the same value, but they will also point internally to the same object. So it is not possible to tell definitely which variable is the owner of the array.

You will need to save the owner inside the variable explicitly. Something like this:

Code:
$customer = array(
    "owner" => "customer",
    "values" => array ('sefdsf','sdfsdf','sdfsdf')
);

06-22-2007, 12:20 AM
#4
bluesaga is offline bluesaga
Status: Member
Join date: Feb 2007
Location:
Expertise:
Software:
 
Posts: 137
iTrader: 1 / 100%
 

bluesaga is on a distinguished road

  Old

Originally Posted by vesselin View Post
In PHP several variables can share the same object. For example:

$customer = array('sefdsf','sdfsdf','sdfsdf');
$customer_2 = $customer;
$customer_3 = $customer;

All the 3 variables $customer, $customer_2 and $customer_3 will not only contain the same value, but they will also point internally to the same object. So it is not possible to tell definitely which variable is the owner of the array.
[/CODE]
This is actually incorrect, these variables are all completely different, however they are all the same. $customer is not referenced in any way, thus they do not point to the same object they simply create clones of it.

$customer_2 = &$customer.

means it creates a pointer to the other variable, and doesnt simply clone it. So if $customer_2 is changed so is $customer.

Anyway, enough of the lesson...

To OP:
Salathe's code would of worked, if you know how to use it, to simplify it:
PHP Code:

$cutomer 
= array('sefdsf','sdfsdf','sdfsdf');

print 
vname($customer);

//Should print "customer" if the function works :)


function vname(&$var$scope false$prefix 'unique'$suffix 'value')
{
    
$vals = ($scope) ? $scope $GLOBALS;
    
$old $var;
    
$var $new $prefix rand() . $suffix;
    
$vname FALSE;
    foreach(
$vals as $key => $val)
        if(
$val === $new
            
$vname $key;
    
$var $old;
    return 
$vname;

Closed Thread    


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

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed