【问题标题】:php Notice: array to string conversionphp 注意:数组到字符串的转换
【发布时间】:2013-09-06 12:32:25
【问题描述】:

大家好,我是 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 并返回单词 Array

这里是 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');

还有api部分以防万一

// GEAR
public function setGear($Array)
{
$this->Gear['slots'] = count($Array);
$GearArray = NULL;

// Loop through gear equipped
    $Main = NULL;
                    foreach($Array as $A)
            {
                // Temp array
                $Temp = array();

                // Loop through data
                $i = 0;
                foreach($A as $Line)
                {
                        // Item Icon
                        if (stripos($Line, 'socket_64') !== false) { $Data = trim(explode('&quot;', $A[$i + 1])[1]); $Temp['icon'] = $Data; }
                        if (stripos($Line, 'item_name') !== false) { $Data = trim(str_ireplace(array('>', '"'), NULL, strip_tags(html_entity_decode($A[$i + 2])))); $Temp['name'] = htmlspecialchars_decode(trim($Data), ENT_QUOTES); }
                        if (stripos($Line, 'item_name') !== false) {
                        $Data = htmlspecialchars_decode(trim(html_entity_decode($A[$i + 3])), ENT_QUOTES);
                        if (
                                strpos($Data, " Arm") !== false ||
                                strpos($Data, " Grimoire") !== false ||
                                strpos($Data, " Tool") !== false
                                        )
                                        { $Main = $Data; $Data = 'Main'; }
                    $Temp['slot'] = strtolower($Data);
                                }

                                // Increment
                                $i++;
            }

            // Slot manipulation
                                $Slot = $Temp['slot'];
                                    if (isset($GearArray[$Slot])) { $Slot = $Slot . 2; }

                                    // Append array
                                    $GearArray['numbers'][] = $Temp;
                                    $GearArray['slots'][$Slot] = $Temp;
                                    }

                                    // Set Gear
                                    $this->Gear['equipped'] = $GearArray;

                                    // Set Active Class
                                    $classjob = str_ireplace('Two-Handed ', NULL, explode("'", $Main)[0]);
                                    $this->Stats['active']['class'] = $classjob;
                                    if (isset($this->Gear['soul crystal'])) { $this->Stats['active']['job'] = str_ireplace("Soul of the ", NULL, $this->Gear['soul crystal']['name']); }
                                    }
                                    public function getGear()           { return $this->Gear; }
    public function getEquipped($Type)  { return $this->Gear['equipped'][$Type]; }
    public function getSlot($Slot)      { return $this->Gear['equipped']['slots'][$Slot]; }

这个问题的解决方案可能真的很简单,我真的很笨,但任何帮助都会很棒:)提前谢谢

【问题讨论】:

  • getSlot() 是一个方法调用,而不是数组本身。 echo $character-&gt;getSlot('head')['name'] 将在 PHP 5.4+ 中工作,否则,您必须将 $character-&gt;getSlot() 的结果存储在变量中,然后从中检索 'name' 键。
  • 完美的工作我告诉过你这是一些非常简单的事情哈哈

标签: php arrays api multidimensional-array


【解决方案1】:
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;
        }
    }     
}

echo $character->getSlot('head')['name'] // If version is >= 5.4.*

如果版本

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

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多