0

I am having a hard time trying to understand why I can't compare the values of two arrays in PHP. If I echo both of these during the loop using "echo $description->ItemDesriptionName;" and "echo $item->ItemName;" the values seem to show as the same, but when I try to compare them using if, nothing works. What am I missing?

<?php

$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$categories = $xml->Menu->Categories;
$items = $xml->Menu->Categories->Items->ItemObject;
$itemdescription = $xml->Menu->Options->Description->DescriptionObject;

foreach($items as $item) {

    echo $item->ItemName . ' - ' . $item->Price . '</br>';

        foreach ($itemdescription as $description) {

            if ($description->ItemDescriptionName == $item->ItemName) {
                echo 'We have a match!';
                //where I would echo $description->ItemDescription;
            }
        } 
}

?>

Here is the XML file

<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Menu>
        <Categories>
            <Name>Category 1</Name>
            <Items>
                <ItemObject>
                    <ItemName>Item 1</ItemName>
                    <Price>1</Price>
                </ItemObject>
                <ItemObject>
                    <ItemName>Item 2</ItemName>
                    <Price>3</Price>
                </ItemObject>               
            </Items>
        </Categories>
        <Options>
            <Description>
                <DescriptionObject>
                    <ItemDescriptionName>Item 1</ItemDescriptionName>
                    <ItemDescription>A Great item</ItemDescription>
                </DescriptionObject>
                <DescriptionObject>
                    <ItemDescriptionName>Item 2</ItemDescriptionName>
                    <ItemDescription>A Great item as well</ItemDescription>
                </DescriptionObject>                
            </Description>
        </Options>
    </Menu>
</Root>
2
  • Your IF has a typo here, does it also have a typo in your source? Description on the ItemDescriptionName property is missing an 'n' Commented Oct 27, 2016 at 0:18
  • The source didn't but it still does not work. Thanks Commented Oct 27, 2016 at 0:24

4 Answers 4

2

compare as string and you have typo in ItemDescriptioName (ItemDescriptionName)

if ( (string)$description->ItemDescriptionName == (string)$item->ItemName) {
Sign up to request clarification or add additional context in comments.

Comments

1

Convert to string and then compare

<?php
$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$menu  = $xml->Menu;
$categories = $xml->Menu->Categories;
$items = $xml->Menu->Categories->Items->ItemObject;
$itemdescription = $xml->Menu->Options->Description->DescriptionObject;
foreach($items as $item) {


         $itemname = $item->ItemName;
        foreach ($itemdescription as $description) {
            $descriptionname = $description->ItemDescriptionName ;
            echo $itemname."   ---- ".$descriptionname."<br/>";

            if((string)$itemname === (string)$descriptionname){
                echo "Yes its matched";
            }
        } 
}

?>

Working fine for me

Comments

1

The properties like $description->ItemDescriptionName are SimpleXMLElement objects. So you do not compare strings but two objects.

SimpleXMLElement objects implement the magic method __toString(). They can be cast to string automatically, but a compare between to objects will not trigger that. You can force it:

if ((string)$description->ItemDescriptionName === (string)$item->ItemName) {
  ...

Comments

0

Can you access them directly instead using an accordant index?

...

$items = $xml->Menu->Categories->Items->ItemObject;
$itemdescription = $xml->Menu->Options->Description;

$i = 0;
foreach ($items as $item) {
  echo $i.' '.$item->ItemName . ' - ' . $item->Price;
  echo $itemdescription->DescriptionObject[$i]->ItemDescriptionName[0];
  echo ' ';
  echo $itemdescription->DescriptionObject[$i]->ItemDescription[0];
  echo '</br>';
  $i++;
}

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.