-1

I'm having a problem with this php file. When nicknames or addresses are values that i want to display, it appears a error called Array. I want to be able to return all user information on php variables.

<?php
  $str = '{
    "users": [
        {
            "steamid": "[X:X:XXXXXXXXX]",
            "teamspeakid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "nicknames": [
                "XXXXXXXXX"
            ],
            "addresses": [
                "XX.XX.XXX.XX"
            ]
        },
        {
            "steamid": "[Y:Y:YYYYYYYYY]",
            "teamspeakid": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
            "nicknames": [
                "YYYYYYYYY"
            ],
            "addresses": [
                "YY.YY.YYY.YY"
            ]
        },
        {
            "steamid": "[Z:Z:ZZZZZZZZZ]",
            "teamspeakid": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
            "nicknames": [
                "ZZZZZZZZZ"
            ],
            "addresses": [
                "ZZ.ZZ.ZZZ.ZZ"
            ]
        }

    ]
}';

$json = json_decode($str);
foreach($json->users as $item)
{
    if($item->steamid == "[X:X:XXXXXXXXX]")
    {
        echo $item->addresses;
    }
}

?>
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
DalexHD
  • 63
  • 1
  • 7
  • 2
    That's probably because `users[i].addresses` is an array. Try `$item->addresses[0]` or loop thru `$item->addresses` and display each value. – CollinD Jun 03 '17 at 15:03
  • 1
    Possible duplicate of [How to solve PHP error 'Notice: Array to string conversion in...'](https://stackoverflow.com/questions/20017409/how-to-solve-php-error-notice-array-to-string-conversion-in), especially the 3rd answer has a bunch of examples that should get you going. – Jenne Jun 03 '17 at 15:06

1 Answers1

0

addresses is array by one item. Edit to:

foreach($json->users as $key=>$item)
{
    if($item->steamid == "[X:X:XXXXXXXXX]")
    {
        echo $item->addresses[0];
    }
}
Ali Hesari
  • 1,821
  • 5
  • 25
  • 51