检索数组/子数组名称以在 php 中使用
我有一个多维数据库数组,它是从我的服务器生成的:
// 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]
如您所见,当前填充的数据库指的是我正在处理的一个 RSS 项目 - 一张表对于通道,另一个对于该通道中的项目,目前这是另一个问题(“用户”表现在并不重要)..
我想要做的是返回数组/子数组名称并将每个名称转换为变量用作 SQL 中字符串的一部分查询,还需要我需要连接的表的别名:
(e.g. SELECT * FROM 'webref_rss_items' WHERE 'chan_id' = 'test.webref_rss_details.id')
其中“webref_rss_items”是一个变量,“chan_id”是一个变量,“test.webref_rss_details.id”是连接字符串中的 3 个变量,尽管我听说过从安全角度来看,SQL 查询中的串联并不是一个好的做法。奇怪的是,对于所有这些值,我所能检索到的只是最深的级别, “id”:
echo "{$da_db['test']['webref_rss_details'][0]}"
但是当我尝试访问名称时,返回“Array”或数组的最后一个值!
这样做的原因是带有查询的 PHP 文件将位于服务器的“公共”部分内,并且希望使用与原始名称没有任何含义的变量,而且变量似乎更方便可以互换,我不会一直使用相同的路径。
编辑:我的想法是获取从 ['db']
到 ['field']
的键名称。我最接近的是在 foreach
循环中迭代键和 array_fill
,在另一个 foreach
中使用 range()
>, array_combine
然后 var_dump
组合数组,如下所示:
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 />');
我可以使用 array_slice
来获取我想要的名称! (啰嗦?) 问题是这样的结果只显示最后一个key =>;值
;根据命令(print_r、print、echo),它显示:
[2] => webref_rss_items
或 大批。
我希望目前的信息已经足够了。
我在这里看到过类似的问题,但通常适用于一个值或数组的一个级别,但如果您之前见过这个问题,请建议并为我指出正确的方向。
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 another 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的两个顶级数组(数据库名称和表名称)是“命名键”数组。字段级数组(表名数组的值)是一个“整数键”数组。 PHP 会自动为仅使用值定义的数组添加数字索引。对于“named-key”数组,您只能使用您定义的键名称来访问它们的值。
例如:
PHP 提供了一个名为 array_keys() 的函数,它将返回一个包含所有键名称的整数索引数组。因此,您可以使用整数值访问表数组,您可以这样做。
也许这会对你有一点帮助。我并不是 100% 了解您计划如何访问数组中的值,因此如果您有任何其他问题,请尝试澄清该部分。
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:
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.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.