douchengfei3985 2015-02-06 06:11
浏览 71
已采纳

数组而不是数组元素

I'm trying to turn Minecraft names into UUIDs, which can be converted into a "name history". I have the name->UUID part down pat, but the name history is getting an error.

Running this code, the $uuid variable contains an array of all the UUIDs I'm trying to convert into a name history - I then try to run all of them through the API, and depending on how many past names the user has, add » icons to signify a change. Unfortunately, with test data of something like this:

Foo
Bar (changed to Baz)

it outputs

Array | Array » Array |

, not the

Foo | Bar » Baz |

it's supposed to. I know I'm probably getting the array as an object, but I don't know where I would be doing that or how to get the element instead.

(and here's the code)

$uuid_real = json_decode($uuid, TRUE);
foreach($uuid_real as $uuid_totest){

$toadd = json_decode(file_get_contents('https://api.mojang.com/user/profiles/' . $uuid_totest['id'] . '/names'), TRUE);
if(count($toadd)==1){
    $results .= $toadd['0'] . " | ";
}elseif(count($toadd)==2){
    $results .= $toadd['0'] . " » " . $toadd['1'] . " | ";
}elseif(count($toadd)==3){
    $results .= $toadd['0'] . " » " . $toadd['1'] . " » " . $toadd['2'] . " | ";
}

Here is var_dump for $toadd with some currently online users:

array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(10) "lottie1664"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(12) "wingmanfoutz"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(11) "bigfoot2991"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(13) "mrstampycat05"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(10) "MEHLAWLARZ"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(10) "metboy2002"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(10) "SILVERMAN2"
  }
}
array(2) {
  [0]=>
  array(1) {
    ["name"]=>
    string(11) "salesman200"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(8) "Quartzic"
    ["changedToAt"]=>
    int(1423055736000)
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(13) "MercenaryCrow"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(8) "fishmeal"
  }
}
  • 写回答

1条回答 默认 最新

  • duandaishi9268 2015-02-06 06:24
    关注

    It is because $toadd[0] $toadd[1] and $toadd[2] are arrays not strings, please provide here var_dump($toadd) and we can help you

    EDIT:

    So as you can see there is no string under $toadd[x] but an array looking like this :

     array(1) {
        ["name"]=>
        string(10) "lottie1664"
      }
    

    so modify your code and use :

    $toadd[0]['name']
    $toadd[1]['name']
    $toadd[2]['name']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。