开发者

Frustration at trying to output two specific values from an Array

I am doing my head in trying to do a simple retrieval for specific arrays within a script so I have an original associative array:

$vNArray ['Brandon']  = $item[3]; 
$vNArray['Smith']= $item[4]; 
$vNArray ['Johnson']= $item[5];
$vNArray ['Murphy']= $item[6];
$vNArray ['Lepsky']= $item[7];

Which outputs a common result for most values:

foreach ($vNArray as $key => $value){

 if(!empty($value)){
    $result  .= "\t\t\t\t<li><strong>$key</strong>"  .$value.   "</li>\n";
 }

But then I want two of these arrays to render differently so I added another script suggested by someone:

$display_id=array('Brandon', 'Murphy');

foreach ($vNArray as $key => $value){
  if(!empty($value)){
    //Looks into the display_id array and renders it differently
    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";
    }
  }

}

The problem is that i want the result for these arrays to contain both within the first result but when I tried to output $result .= "\t\t\t\t$key[1]".$value[1]." \n";

PHP thinks that the index is the value's character index, so I'm having major syntax issues like id="/" r.

I have also tried

$result  .= "\t\t\t\t<li id=\"". $display_id['Brandon']$value.\""><strong>$key[1]</strong>". $display_id['Murphy']$value." </li>\n";

But I am still getting wrong syntax issues...like

syntax error, unexpected T_VARIABLE

Or some other error like this.

Could someone please help?

EDITED


I have made the syntax corrections but I still need to specify 开发者_如何学Cthe index:

The result from

result  .= "\t\t\t\t<li id=\"". $display_id['Brandon'] . $value."\"><strong>" .   $key[1] . "</strong>". $display_id['Murphy'] . $value." </li>\n";

Needs to be (Note how each value is on the same output depending on what I'm targeting):

<li id="Brandon Value"><strong>Brandon</strong> Murphy Value</li>

Right now it ignores the index value of . $display_id['Brandon'] . $value. or . $display_id['Murphy'] . $value." all together and just repeats:

<li id="Brandon Value"><strong>Brandon</strong> Brandon Value</li>
<li id="Murphy Value"><strong>Murphy</strong> Murphy Value</li>


Just do $key, forget the [1] bit. Same with $value.


Each value needs to be concatenated with another, so for example:

echo $a . $b . $c . $d . $e;

Notice the . contact that joins each variable with the the next / prev variable, so your line:

$display_id['Brandon']$value

should look like:

$display_id['Brandon'] . $value
                       ^

I would do the following though.

$result  .= sprintf('<li id="%s"><strong>%s</strong> %s</li>',$display_id['Brandon'] . $value,$key[1],$display_id['Murphy'] . $value);

Also using sprintf also make your code more readable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜