Apostolos Kou
Apostolos Kou

Reputation: 85

incrementing variable in SimpleXML

I have a problem with the following code. The variable $entry never becomes greater than 1. I want it to increment to be able to take all keywords and put them in a single variable. I can't find the reason why $entry is being incremented. Thanks in advance! :-)

function objectsIntoArray($arrObjData, $entry, $arrSkipIndices = array()) {

`$arrData = array();`
$kwords=array();

// if input is object, convert into array

if (is_object($arrObjData)) {

    $arrObjData = get_object_vars($arrObjData);

}

if (is_array($arrObjData)) {


    foreach ($arrObjData as $index => $value) {

    if ($index=="keywordterm"&&$index!="0"){
        $kword=$arrObjData[$index];
        //echo "arrObjData[$index]: ".$kword."</br></br>";
        $kwords[$entry]=$kword;
        //echo "keywords: ".$kwords."</br></br>";
        //echo "keywords[$entry]: ".$kwords[$entry]."</br></br>";
        $entry++;

    }
        if (is_object($value) || is_array($value)) {

            $value = objectsIntoArray($value, $entry, $arrSkipIndices); // recursive call

        }

        if (in_array($index, $arrSkipIndices)) {
            continue;
        }

        $arrData[$index] = $value;
        //echo "$arrData[$index]: ".$arrData[$index]."</br>";
    }


}

return $arrData;
}

`$entry=0;

$xmlUrl = "9424.xml"; // XML feed file/URL

$xmlStr = file_get_contents($xmlUrl);

$xmlObj = simplexml_load_string($xmlStr);

$arrXml = objectsIntoArray($xmlObj, $entry);`

With the 1st execution it shows: keywords[0]: telecommunication computing

With the 2nd it shows: keywords[0]: multi-agent systems

You see? It's 0 again....

A little code from the xml:

<keywordset keywordtype="Inspec">
      <keyword>
        <keywordterm><![CDATA[telecommunication computing]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[multi-agent systems]]></keywordterm>
       </keyword>
      <keyword>
        <keywordterm><![CDATA[state estimation]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[control engineering computing]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[telecommunication control]]></keywordterm>
      </keyword>
    </keywordset>

Upvotes: 0

Views: 138

Answers (1)

craig1231
craig1231

Reputation: 3867

Im not entirely sure what you are trying to achieve here, but one thing I have noticed is that you are attempting to change the params without a reference to an object.

Change

function objectsIntoArray($arrObjData, $entry, $arrSkipIndices = array()) { 

To

function objectsIntoArray(&$arrObjData, &$entry, $arrSkipIndices = array()) { 

It may, or may not help; but its something to try.

Example of what I mean...

function IncNumber($num)
{
    $num++;
}

$num = 0;
IncNumber($num);
// $num will still be 0



// Using & to declare a reference to the object
function IncNumber2(&$num)
{
    $num++;
}
IncNumber2($num);
// $num will be 1

Upvotes: 1

Related Questions