Question

I have an XML file xml I am parsing the file with the code -

<?php
$xml=simplexml_load_file('info.xml');

foreach($xml->testcase as $var){

    $var=explode('/',$var->script);

    $module =array($var[2]);

    $testName = array (end($var));

    print_r($module); 

    print_r($testName); 
    }

?>

With this code my output looks like output

But my output should be like two arrays (name say module and testName) containing the $var[2] and end($var)

Was it helpful?

Solution

Use this format

<?php
    $xml=simplexml_load_file('info.xml');
    foreach($xml->testcase as $var){

        $var=explode('/',$var->script);

        $module[] =$var[2];

        $testName[] = end($var);


        }
        echo "<pre>";
        print_r($module);
        echo implode(',',$module);

        print_r($testName);

        echo implode(',',$testName);

        exit;
    ?>

Create Dropdown

<select name="sample">
        <?php       
        foreach($module as $mod)
        {
            ?>
            <option value="<?php echo $mod;?>"><?php echo $mod;?></option>
            <?php
        }       
        ?>
        </select>

Second Alternative

<select name="sample">
        <?php
        $i=1;
        foreach($module as $mod)
        {
            ?>
            <option value="<?php echo $mod;?>"><?php echo $testName[$i];?></option>
            <?php
            $i++;
        }



    ?>
    </select>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top