开发者

Retrieving array/sub-array name for use in php

开发者 https://www.devze.com 2023-04-11 20:33 出处:网络
I have a multi-dimensional array of databases, which was generated from my server: // place db tables into array

I have a multi-dimensional array of databases, which was generated from my server:

// place db tables into array
$da_db = array(
'test' => array(
    // test.users
    'users' => array('fname','lname','info'),
    // test.webref_rss_details
    'webref_rss_details' => array('id','title','link','description','language','image_title','image_link','item_desc','image_width','image_height','image_url','man_Edit','webmaster','copyright','pubDate','lastBuild','category','generator','docs','cloud','ttl','rating','textInput','skipHours','skipDays'),
    // test.webref_rss_items
    'webref_rss_items' => array('id','title','description','link','guid','pubDate','author','category','comments','enclosure','source','chan_id')
),
'db_danaldo' => array(
    //code here
),
'frontacc' => array(
    //code here
)

[array][db][table][field]

As you can see, the database currently populated refers to an RSS project I am working on - one table for Channels, another for Items in that channel, at the moment that is another issue ('Users' table for now is not important)..

what I want to do is to return the array/sub-array names and convert each into variables for use as part of a string in an SQL Query, also need an alias for the tables I need to connect with:

(e.g. SELECT * FROM 'webref_rss_items' WHERE 'chan_id' = 'test.webref_rss_details.id')

where 'webref_rss_items' is a variable, 'chan_id' is a variable and 'test.webref_rss_details.id' are 3 variables in a concatenated string, although I've heard the concatenation in an SQL Query is not good practice, security-wise.. the strange thing is of all of those values, all I can retreive is the deepest level, 'id':

echo "{$da_db['test']['webref_rss_details'][0]}"

but get 'Array' returned or the last value of an array when I try to access the names!!

The reason for this is that the PHP file with the query will be within the 'public' part of the server and would like to have use variables with have no connotation to the original name(s), also it seems more convenient as the variables can be interchangable and I won't be using the same path all the time.

EDIT: My idea is to get key names from ['db'] to ['field']. The closest I have reached is to iterate keys and array_fill in a foreach loop, use range() inside a开发者_JS百科nother foreach, array_combine both then var_dump combined array like so:

foreach($da_db['test'] as $key1 => $val) { //put key-names into array1 to use as values
$a = array();
$a = array_fill(0, 1, $key1);
print($key1.'<br />');
}   

foreach (range(0, 2) as $number) {         //array for numbers to use as keys
$b = array();
$b = array_fill(0, 1, $number);
    echo $number.'<br />';
}

$c = array_combine($b, $a);                //combine both for new array
print_r($c.'<br />');

I could the use array_slice to get the name I want! (long winded?) The problem is that the result of this only shows the last key => value; depending on command(print_r, print, echo), it shows:

[2] => webref_rss_items 

OR Array.

I hope that is enough info for now.

I've seen similar questions on here, but normally apply to one value, or one level of an array, but if you have seen this question before please advise and point me in right direction.


Your two top level arrays (db name and table name) are "named-key" arrays. The field level array (value of table name array) is an "integer key" array. PHP automatically adds numerical indexes for arrays that are defined with values only. For "named-key" arrays, you can only access their values by using the key name you defined.

For example:

$array1 = array('zero', 'one', 'two');
echo $array1[2]; // two

$array2 = array('zero' => 'this is zero', 'one' => 'this is one');
echo $array2['zero']; // this is zero
echo $array2[0]; // undefined

PHP provides a function called array_keys() that will return you an integer index array with all the key names. So you access your table array using an integer value, you can do this.

$da_db_test_keys = array_keys($da_db['test']);
echo $da_db_test_keys[1];

Maybe this will help you a little bit. I wasn't 100% on how you planned on accessing the values in your arrays, so if you have any more questions please try to clarify that part.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号