Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to return a value from a multidimensional array, but it doesn't seem to be working.

Array -

[players] => Array
    (
        [0] => Array
            (
                [player] => Necro
                [score] => 0
                [deaths] => 0
                [gq_name] => 
                [gq_kills] => 
                [gq_deaths] => 0
                [gq_score] => 0
                [gq_ping] => 
            )

    )

PHP Foreach

<?php  
$dayzplayers = $results["dayz"]["players"];
          foreach($dayzplayers as $k => $v) {
                  echo ' <b>'.$v["player"].'</b>';
              } ?>
share|improve this question
1  
I cannot spot any "dayz" in your array... So $dayzplayers is empty, the loop won't loop. –  arkascha Jan 23 '13 at 13:41
    
I specify multiple games in the array, thats how I'm specifying the server query specifically. –  Necro. Jan 23 '13 at 13:42
    
If you do a print_r($v); inside the foreach, what do you get? –  m4t1t0 Jan 23 '13 at 13:42
1  
@Jason: no, he posted the debug output above. It is an array, he just didn't post his real data structure. –  arkascha Jan 23 '13 at 13:47
1  
there seems to a special character [SOHplayer] => Necro; analyse it. –  SparKot ॐ Jan 23 '13 at 13:48

2 Answers 2

The ['player'] index appears to have an invisible control character in the key SOH (Start of Heading)

Try echo ' <b>'.$v[chr(1) . "player"].'</b>'; instead of echo ' <b>'.$v["player"].'</b>';

share|improve this answer
    
What would you recommend doing? I tried doing ob_flush(); –  Necro. Jan 23 '13 at 14:03
    
@Necro. I have amended my answer with a reccomendation –  neelsg Jan 23 '13 at 14:23

If the data is what you posted in the first listing, this should work:

foreach($dayzplayers as $player) {
    echo $player[chr(1).'player'];
}

as per http://codepad.org/kUYueGVh

share|improve this answer
    
Its so weird. Because when I do a var_dump on $player, it shows the array and clearly shows "player", yet when I do the above code. No output =/ check esclan.org/esclan/dayz# to see. –  Necro. Jan 23 '13 at 13:50
    
@Necro Open the dump in an editor where you can see non-graphic characters. –  SparKot ॐ Jan 23 '13 at 13:56
    
What happens if you echo $player['score']; –  Tuim Jan 23 '13 at 14:01
    
It returns the score. But yet when I try 'player' nothing. The array comes back null. –  Necro. Jan 23 '13 at 14:02
    
echo (string) $player['player']; or if it indeed does have a SOH character echo var_export($player['player']); See if that helps –  Tuim Jan 23 '13 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.