1

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>';
              } ?>
20
  • 1
    I cannot spot any "dayz" in your array... So $dayzplayers is empty, the loop won't loop. Commented Jan 23, 2013 at 13:41
  • I specify multiple games in the array, thats how I'm specifying the server query specifically. Commented Jan 23, 2013 at 13:42
  • If you do a print_r($v); inside the foreach, what do you get? Commented Jan 23, 2013 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. Commented Jan 23, 2013 at 13:47
  • 1
    there seems to a special character [SOHplayer] => Necro; analyse it. Commented Jan 23, 2013 at 13:48

2 Answers 2

1

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>';

Sign up to request clarification or add additional context in comments.

1 Comment

@Necro. I have amended my answer with a reccomendation
0

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

9 Comments

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 Open the dump in an editor where you can see non-graphic characters.
What happens if you echo $player['score'];
It returns the score. But yet when I try 'player' nothing. The array comes back null.
echo (string) $player['player']; or if it indeed does have a SOH character echo var_export($player['player']); See if that helps
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.