Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php注意事项:数组到字符串的转换_Php_Arrays_Api_Multidimensional Array - Fatal编程技术网

php注意事项:数组到字符串的转换

php注意事项:数组到字符串的转换,php,arrays,api,multidimensional-array,Php,Arrays,Api,Multidimensional Array,大家好,我对php和堆栈溢出都是新手,但我对php有一定的了解,我想做的是从api函数返回的数组调用中获取一个值 $character->getSlot('head'); 使用print\r可提供以下信息: Array ( [icon] => http://url to image location [name] => Wizard's Petasos [slot] => head ) 如果我回显$character->getSlot('head','name);

大家好,我对php和堆栈溢出都是新手,但我对php有一定的了解,我想做的是从api函数返回的数组调用中获取一个值

$character->getSlot('head');
使用print\r可提供以下信息:

Array ( 
[icon] => http://url to image location
[name] => Wizard's Petasos 
[slot] => head )
如果我回显$character->getSlot('head','name);在第19行给我C:\xampp\htdocs\test\index.php并返回单词数组

下面是index.php的一节

<?php
include('api.php');

// Call API
$API = new LodestoneAPI();

// Search for: Demonic Pagan on Sargatanas
$character = $API->get(array(
"name" => "Darka Munday",
"server" => "Ragnarok"
));

// Basic character data
echo "Character ID: " . $character->getID(); 
$ID = $character->getID();
$API->parseProfile($ID);
$Character = $API->getCharacterByID($ID);
echo $character->getSlot('head', 'name');
解决这个问题的方法可能非常简单,我也很笨,但任何帮助都会很好:)提前谢谢

public function getSlot($Slot) { 
     return $this->Gear['equipped']['slots'][$Slot]; 
}
getSlot
方法只有一个参数。您正在传递另一个参数,但
getSlot
方法返回数组

您可以这样做:

public function getSlot($Slot, $param = null) {
    if(empty($param)) {
          return $this->Gear['equipped']['slots'][$Slot]; 
    } else {
        if(isset($this->Gear['equipped']['slots'][$Slot][$param] )) {
            return $this->Gear['equipped']['slots'][$Slot][$param];
        } else {
            return null;
        }
    }     
}

如果版本<5.4*

$slot = $character->getSlot('head');
echo $slot['name'];
getSlot
方法只有一个参数。您正在传递另一个参数,但
getSlot
方法返回数组

您可以这样做:

public function getSlot($Slot, $param = null) {
    if(empty($param)) {
          return $this->Gear['equipped']['slots'][$Slot]; 
    } else {
        if(isset($this->Gear['equipped']['slots'][$Slot][$param] )) {
            return $this->Gear['equipped']['slots'][$Slot][$param];
        } else {
            return null;
        }
    }     
}

如果版本<5.4*

$slot = $character->getSlot('head');
echo $slot['name'];

getSlot()
是一个方法调用,而不是数组本身
echo$character->getSlot('head')['name']
将在PHP5.4+中工作,否则,您必须将
$character->getSlot()
的结果存储在一个变量中,然后从中检索
'name'
键。ty工作得很好,我告诉过您这是一件非常简单的事情。lol
getSlot()
是一个方法调用,而不是数组本身
echo$character->getSlot('head')['name']
将在PHP5.4+中工作,否则,您必须将
$character->getSlot()
的结果存储在一个变量中,然后从中检索
'name'
键。ty工作正常我告诉过您这是一件非常简单的事情,哈哈