I am going through this process of making my code more efficient but have hit another wall开发者_如何学Go with rendering my results differently.
I have a script which runs like so
$vNArray ['Brandon'] = $item[3];
$vNArray['Smith']= $item[4];
$vNArray ['Johnson']= $item[5];
$vNArray ['Murphy']= $item[6];
$vNArray ['Lepsky']= $item[7];
etc.
foreach ($vNArray as $key => $value){
if(!empty($value)){
$result .= "\t\t\t\t<li><strong>$key</strong>" .$value. "</li>\n";
}
So far so good, but sometimes the result must render differently so that I can have
$result .= "\t\t\t\t<li><a href="thislink.com">$key</a>" .$value. "</li>\n";
or
$result .= "\t\t\t\t<li id=\"$key\" ><strong>$key</strong>" .$value. "</li>\n";
The way I would like to set this is by drilling down my array list and plucking some of the arrays which I would set to output in that format, however as I am very new to PHP I don't know how to target the specific arrays and keys, I have tried stuff like $key[1] but it thinks I'm going through the letters of the name as opposed to the array index.
Is there a way to simplify this so that I can select the value or key from my array above and render it differently?
Cheers
Can you define the criteria under which you want to render differently? Perhaps then some code sample could be provided. Based on what you've said thoughs something along the lines of the following is what you are after.
if(!empty($value)){
if( test case one ){
$result .= "\t\t\t\t<li><a href="thislink.com">$key</a>".$value."</li>\n";
} else if ( test case two ) {
$result .= "\t\t\t\t<li id=\"$key\" ><strong>$key</strong>".$value."</li>\n";
} else {
$result .= "\t\t\t\t<li><strong>$key</strong>".$value."</li>\n";
}
}
I'm not sure that I have understood, but let's see.
You might create an array with the list of keys that need a specific output, like:
$display_id=array('Brandon', 'Murphy');
Then in your foreach:
foreach ($vNArray as $key => $value){
if(!empty($value)){
if (in_array($key, $display_id)) {
$result .= "\t\t\t\t<li id=\"$key\"><strong>$key</strong>$value</li>\n";
} else {
$result .= "\t\t\t\t<li><strong>$key</strong>$value</li>\n";
}
}
}
This means that if the currently displayed key is in the $display_id array, it will be displayed differently. You can add as many ifs and arrays as you need.
精彩评论