473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

array name

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
12 17569
> 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
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
>
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
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
> 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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: James | last post by:
Hi, Have posted before, but will simplify problem here. For original post go to http://forums.devshed.com/t80025/s.html I have setup 2 arrays like so in my one page script: $carers =...
5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
2
by: sicapitan | last post by:
I'm trying to build some xml from an array with the help from another array. In one array I have $cells Array ( =CELL0
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
1
by: neoblitz | last post by:
Hi Guys, I'm very new to NuSoap. I'm able to call a webservice which actually returned me results.. But I'm having trouble to read the response or I simply don't understand how to read the response.....
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.