0

currently, im having problem to parse xml node in array using condition where parse with <mo> as separator

this is my array(0)

Array([0] => <mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn>);

i want to parse like this

Array[0] => <mi>x</mi>
Array[1] =><mo>+</mo><mn>2</mn>
Array[2]=><mo>=</mo><mn>3</mn>

this is my coding

<?
$result(0)="<mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn>";
$result1= new simplexml_load_string($result);
$arr_result=[];
foreach($result1 as $key => $value){
     $exp_key = explode('<', $key);
    if($key[0] == 'mo'){
         $arr_result[] = $value;
    }
print_r($arr_result);
}


if(isset($arr_result)){
    print_r($arr_result);
}
?>

thanks in advance !

Patrick Q
  • 6,373
  • 2
  • 25
  • 34

1 Answers1

0

The approach with XML seems excessive since what you really want is to pull out substrings of a string based on a delimiter.

Here is a working example. It works by finding the position of <mo> and cutting off that section, then searching for the next <mo> in the remain string.

<?php
$result(0)="<mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn>";
$res = $result(0);
$arr_result=[];
while($pos = strpos($res, "<mo>", 1)) {
    $arr_result[] = substr($res, 0, $pos); // grab first match
    $res = substr($res, $pos); // grab the remaining string
}
$arr_result[] = $res; // add last chunk of string

print_r($arr_result);

?>

Your code above has several issues. First:

$result1= new simplexml_load_string($result); // simplexml_load_string() is a function not a class

Second:

$key and $value do not contain the '<' and '>' so, this part: $exp_key = explode('<', $key); will never do anything and isn't needed.

Third:

If your code did work it would only return array('+', '=') because you are appending the data inside the mo element to the result array.

Tim
  • 2,139
  • 13
  • 18
  • there are problem to call result as it in array form. how to call array by array – jai wilshere May 25 '18 at 00:09
  • Could you please reword your question? – Tim May 25 '18 at 01:48
  • currently i have an array contain xml node as shown above is result(0). there are another array that i do not put here. im not sure either it store as string or xml in that array. $result = []; foreach ($xml1->children() as $child) { if ($child->getName() === "mspace") { $result[++$counter] = []; continue; } $result[$counter][] = $child->saveXML(); } $result = array_map(function($x){ return implode("", $x); }, $result); print_r($result); coding for &result – jai wilshere May 25 '18 at 02:13
  • OK, so just make another string from the string in the first position of the array; `$res = $result(0);` and change the rest of the code accordingly. – Tim May 25 '18 at 02:18
  • if i wan to iterate more than one index? its work when i declare directly one index. but when i do loop for another index it seem does not work coding below for ($i=0; $i < $length ; $i++) { $res=$result[$i]; while($pos = strpos($res, "",1)) { $arr_result[] = substr($res, 0, $pos); // grab first match $res = substr($res, $pos); // grab the remaining string } } $arr_result[] = $res; // add last chunk of string print_r($arr_result); – jai wilshere May 25 '18 at 03:00