1

i have an xml file:

    <?xml version="1.0" encoding="utf-8" ?>
    <transaction dsxml_version="1.08">
    <action>action1</action>
    <action>action2</action>
    </transaction>

If i use simplexml i can access the first "action" with die following code

    $xml = simplexml_load_string($xml_content);
    echo $xml->action; // Write "action1"
    echo $xml->action[0]; // Write "action1"
    echo $xml->action[1]; // Write "action2"

Now i create an array and a try to access it on the same way. But it doesent work.

We have a huge php skipt which use simple xml which contains a logical error. If i can emulate simple xml i can fix this error on one position

3
  • 1
    Clarify the problem please. You say "Now i create an array and a try to access it on the same way." Is this about SimpleXML or is it about arrays? Please show the code that doesnt work. Commented Oct 18, 2011 at 14:42
  • i want to build a class which can used instead of simple xml. but this class must contain the the functions. So $xml->action must return the first arrayelement. Commented Oct 18, 2011 at 14:47
  • 1
    no offense but why dont you just fix the error instead of coding around the error? Commented Oct 18, 2011 at 15:06

5 Answers 5

1

try this:

current($xml->action);
Sign up to request clarification or add additional context in comments.

Comments

1

echo array_shift(array_slice($xml->action, 0, 1));

or if you're not worried about damaging the original array, $xml->action you can use the following

echo array_shift($xml->action);

Using array_shift will guarantee you get the first element if it's numbered or associative.

Comments

1

You can create a fake or mock object that simulates the behaviour you are looking for:

$action = new SimpleXMLArrayMock($action_array);

$xml->action = $action;

echo "\nFake:\n";
echo $xml->action, "\n";    // Write "action1"
echo $xml->action[0], "\n"; // Write "action1"
echo $xml->action[1], "\n"; // Write "action2"


/**
 * Mock SimpleXML array-like behavior
 */
class SimpleXMLArrayMock extends ArrayObject
{
    private $first;
    public function __construct(array $array)
    {
        $this->first = (string) $array[0];
        parent::__construct($array);
    }
    public function __toString()
    {
        return $this->first;
    }

}

Demo

Comments

0

use xpath so you can find the element

Comments

0

This would work:

foreach($myarray as $key => $val)
{
    print $val;
    //if you don't need the rest of the elements:
    break;
}

Added:

You can even make it a function:

function get_first_element($myarray)
{
    foreach($myarray as $key => $val)
    {
        return $val;
    }
}

3 Comments

Yes sure, but so i have to change my complete script. $xml->action is an array. And i want to access the first element if no index is given. Simple XML use this function, but i don't have any idea how they do it.
@user1001336 If you replace $myarray with $xml->action in my example above, wouldn't that solve your problem? I don't see how this makes you change your entire script?
@user1001336 Added function to make it easier implementing in your code.

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.