Retrieving array/sub-array name for use in php -
i have multi-dimensional array of databases, generated server:
// place db tables 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 can see, database populated refers rss project working on - 1 table channels, items in channel, @ moment issue ('users' table not important)..
what want return array/sub-array names , convert each variables use part of string in sql query, need alias tables need connect with:
(e.g. select * 'webref_rss_items' 'chan_id' = 'test.webref_rss_details.id')
where 'webref_rss_items' variable, 'chan_id' variable , 'test.webref_rss_details.id' 3 variables in concatenated string, although i've heard concatenation in sql query not practice, security-wise.. strange thing of of values, can retreive deepest level, 'id':
echo "{$da_db['test']['webref_rss_details'][0]}"
but 'array' returned or last value of array when try access names!!
the reason php file query within 'public' part of server , have use variables have no connotation original name(s), seems more convenient variables can interchangable , won't using same path time.
edit: idea key names ['db']
['field']
. closest have reached iterate keys , array_fill
in foreach
loop, use range()
inside foreach
, array_combine
both var_dump
combined array so:
foreach($da_db['test'] $key1 => $val) { //put key-names array1 use values $a = array(); $a = array_fill(0, 1, $key1); print($key1.'<br />'); } foreach (range(0, 2) $number) { //array numbers use keys $b = array(); $b = array_fill(0, 1, $number); echo $number.'<br />'; } $c = array_combine($b, $a); //combine both new array print_r($c.'<br />');
i use array_slice
name want! (long winded?) problem result of shows last key => value
; depending on command(print_r, print, echo), shows:
[2] => webref_rss_items
or array.
i hope enough info now.
i've seen similar questions on here, apply 1 value, or 1 level of array, if have seen question before please advise , point me in right direction.
your 2 top level arrays (db name , table name) "named-key" arrays. field level array (value of table name array) "integer key" array. php automatically adds numerical indexes arrays defined values only. "named-key" arrays, can access values using key name defined.
for example:
$array1 = array('zero', 'one', 'two'); echo $array1[2]; // 2 $array2 = array('zero' => 'this zero', 'one' => 'this one'); echo $array2['zero']; // 0 echo $array2[0]; // undefined
php provides function called array_keys()
return integer index array key names. access table array using integer value, can this.
$da_db_test_keys = array_keys($da_db['test']); echo $da_db_test_keys[1];
maybe little bit. wasn't 100% on how planned on accessing values in arrays, if have more questions please try clarify part.
Comments
Post a Comment