0

hi all im new ish to php and new to stack overflow but do have some knowledge in php what im trying to do is get one value from my returned array call from an api function is

$character->getSlot('head');

using print_r gives me:

Array ( 
[icon] => http://url to image location
[name] => Wizard's Petasos 
[slot] => head )

if i echo $character->getSlot('head', 'name); gives me C:\xampp\htdocs\test\index.php on line 19 and just returns the word Array

here is the section of 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');

and the section of the api just in case

// 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]; }

the solution to this is probably really simple and im being really dumb but any help would be great :) thanks in advance

2
  • 3
    getSlot() is a method call, not an array itself. echo $character->getSlot('head')['name'] will work in PHP 5.4+, otherwise, you must store the result of $character->getSlot() in a variable then retrieve the 'name' key from it. Commented Sep 6, 2013 at 12:34
  • ty that worked perfect i told you it was some thing very simple lol Commented Sep 6, 2013 at 12:43

1 Answer 1

3
public function getSlot($Slot) { 
     return $this->Gear['equipped']['slots'][$Slot]; 
}

getSlot method have only one argument. You are passing another argument but getSlot method returns array.

You can do like this :

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

OR

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

If version < 5.4.*

$slot = $character->getSlot('head');
echo $slot['name'];
Sign up to request clarification or add additional context in comments.

Comments

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.