By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

array name

P: n/a
hi all,

I've got a simple array named my_array :
my_array = array();

I'd like to display the name of my array => print .... I can't find !
how is it possible ?

many thanks in advance,
--
roland
Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
> I've got a simple array named my_array :
my_array = array();


oups... it should be :
$my_array = array();

sorry,
--
roland
Jul 17 '05 #2

P: n/a
roland:
hi all,

I've got a simple array named my_array :
my_array = array();

I'd like to display the name of my array => print .... I can't find !
how is it possible ?

many thanks in advance,


echo "my_array";

André Nęss
Jul 17 '05 #3

P: n/a
>
echo "my_array";

André Nęss


ah ah !
No, I'd like to find a way to get the name of the array dynamicaly.
something like echo $my_array.get_the_name_of_the_array();

--
roland
Jul 17 '05 #4

P: n/a
roland:

echo "my_array";

André Nęss


ah ah !
No, I'd like to find a way to get the name of the array dynamicaly.
something like echo $my_array.get_the_name_of_the_array();


But if you type $my_array you already know the name of the variable. What is
it *really* you are trying to achieve? When people ask these kinds of
questions it's almost always because they are on the wrong track.

André Nęss
Jul 17 '05 #5

P: n/a
> But if you type $my_array you already know the name of the variable. What
is
it *really* you are trying to achieve? When people ask these kinds of
questions it's almost always because they are on the wrong track.

OK, in fact, I summed up my coding pb in order not to have a too long
message.

Actually, I've got a function with the following prototype :
function my_function ($my_array);
$my_array is an array
As this function is called from different files, the array name changes.
I'd like to avoid :
function my_function ($my_array, $array_name);
I find it "dirty"...
I'd like to use the first prototype and being able to get the name of the
array $my_array in the function.

Is that clearer to you ?
thanks for your help,
--
roland
Jul 17 '05 #6

P: n/a
"roland" <ro****@no-spam.org> wrote:
But if you type $my_array you already know the name of the variable.
What

is
it *really* you are trying to achieve? When people ask these kinds of
questions it's almost always because they are on the wrong track.

Actually, I've got a function with the following prototype :
function my_function ($my_array);
$my_array is an array
As this function is called from different files, the array name
changes. I'd like to avoid :
function my_function ($my_array, $array_name);
I find it "dirty"...
I'd like to use the first prototype and being able to get the name of
the array $my_array in the function.


Hi Roland,

It's clear what you want to achieve, but why do you need to know the
variable's name?

(BTW, without somehow passing the name of the variable to your function it
is not possible)

JOn
Jul 17 '05 #7

P: n/a
roland wrote:
But if you type $my_array you already know the name of the variable.
What is it *really* you are trying to achieve? When people ask these
kinds of questions it's almost always because they are on the wrong
track.

OK, in fact, I summed up my coding pb in order not to have a too long
message.

Actually, I've got a function with the following prototype :
function my_function ($my_array);
$my_array is an array
As this function is called from different files, the array name
changes. I'd like to avoid :
function my_function ($my_array, $array_name);
I find it "dirty"...
I'd like to use the first prototype and being able to get the name of
the array $my_array in the function.

Is that clearer to you ?
thanks for your help,


[Obi Wan tone on]
You dont need to know the name of the variable.
[Obi Wan tone off]
You are going on the wrong track somewhere indeed. Why would you
possibly need the name of the variable? Give us the bigger picture of
what you are doing, and we can better show you a way to do it without
odd twinkets.

--
Suni

Jul 17 '05 #8

P: n/a
Roland,

what about:

$arraysByName['myArray'] = array();

//mak the call
myFunction(arraySliceByKey($arraysByName, 'myArray', 1));

//define the function
function myFunction($arrayOfOneArrayByName)
{
forEach($arrayOfOneArrayByName as $arrayName => $anArray) {
//your statements
}
}

function arraySliceByKey($array, $key, $length)
{
return array_slice( $array, array_search($key, array_keys($array)),
$length);
}
BTW, I could not find a function to get the index of a key in an array
(needed for array_slice).
array_search and array_keys will be slow if $arraysByName is large. The
following would be a lot faster:

function arraySliceOneByKey($array, $key)
{
return array($key => $array);
}

Greetings,

Henk Verhoeven,
www.metaclass.nl

"roland" <ro****@no-spam.org> wrote in message
news:3f**********************@news.free.fr...
But if you type $my_array you already know the name of the variable.
What is
it *really* you are trying to achieve? When people ask these kinds of
questions it's almost always because they are on the wrong track.

OK, in fact, I summed up my coding pb in order not to have a too long
message.

Actually, I've got a function with the following prototype :
function my_function ($my_array);
$my_array is an array
As this function is called from different files, the array name changes.
I'd like to avoid :
function my_function ($my_array, $array_name);
I find it "dirty"...
I'd like to use the first prototype and being able to get the name of the
array $my_array in the function.

Is that clearer to you ?
thanks for your help,
--
roland

Jul 17 '05 #9

P: n/a
If you absolutely must know the identity of your variables...

function my_function ($my_array) {
$caller_info = array_shift(debug_backtrace());
$lines = file($caller_info['file']);
$line = $lines[$caller_info['line'] - 1];
if(preg_match('/my_function\\s*\\(\$(\\w+)/', $line, $matches)) {
$name_of_my_array = $matches[1];
echo $name_of_my_array;
}

}

"roland" <ro****@no-spam.org> wrote in message news:<3f**********************@news.free.fr>...
But if you type $my_array you already know the name of the variable. What

is
it *really* you are trying to achieve? When people ask these kinds of
questions it's almost always because they are on the wrong track.

OK, in fact, I summed up my coding pb in order not to have a too long
message.

Actually, I've got a function with the following prototype :
function my_function ($my_array);
$my_array is an array
As this function is called from different files, the array name changes.
I'd like to avoid :
function my_function ($my_array, $array_name);
I find it "dirty"...
I'd like to use the first prototype and being able to get the name of the
array $my_array in the function.

Is that clearer to you ?
thanks for your help,

Jul 17 '05 #10

P: n/a
Thank you all for your posts.
The 2 lasts seems very interesting... I'll try them asap.
Even thought, I'm not sure I'm trying the right way, but the real code is
quite long to explain.

Thanks again,
--
roland
Jul 17 '05 #11

P: n/a
pretty useless as the variable inside the function and the one you pass as
parameter to the function doesn't need to have the same name, so, I can't
see any use to know the name of the variable ... Or did I miss something :S

Savut

"roland" <ro****@no-spam.org> wrote in message
news:3f**********************@news.free.fr...
But if you type $my_array you already know the name of the variable.
What is
it *really* you are trying to achieve? When people ask these kinds of
questions it's almost always because they are on the wrong track.

OK, in fact, I summed up my coding pb in order not to have a too long
message.

Actually, I've got a function with the following prototype :
function my_function ($my_array);
$my_array is an array
As this function is called from different files, the array name changes.
I'd like to avoid :
function my_function ($my_array, $array_name);
I find it "dirty"...
I'd like to use the first prototype and being able to get the name of the
array $my_array in the function.

Is that clearer to you ?
thanks for your help,
--
roland

Jul 17 '05 #12

P: 1
Hi,

I think something like this will help:

[PHP] function __construct($fileArray, $postArray, $uploadDir)
{

/*
*Get the name of the file array
*/
if(count($fileArray) == 1) //We are dealing with a single file
{
foreach($fileArray as $key => $val)
{
$arrayName = $key;
}
}

echo $fileArray[$arrayName]['name'];
}[/PHP]
Jul 11 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.