Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 2 of 2

Thread: Array comparison problem

  1. #1

    Array comparison problem

    It's been awhile since I posted on these forums
    but now I have a rather complicated problem and I'm hoping anyone can help me:
    I'm trying to code a testing system, where the user will enter data into a HTML form, which posts it to a PHP script
    which is supposed to compare the entered data with the correct data stored within a XML file.
    The php page is supposed to echo which os the answers are right and which are wrong, using array_diff() and array_intersect()
    The PHP code itself looks something like this:


    <?php
    $exercise = $_POST['exercise'];
    $nose = $_POST['nose'];
    $gold = $_POST['gold'];
    $black = $_POST['black'];
    $northwest = $_POST['northwest'];
    $character = $_POST['character'];
    $chicken = $_POST['chicken'];
    $tomarket = $_POST['tomarket'];
    $translate= array (0 => Array($exercise), 1 => Array($nose), 2 => Array($gold), 3 => Array($black), 4 => Array($northwest), 5 => Array($character), 6 => Array($chicken), 7 => Array($tomarket));
    $dom =
    domxml_open_mem(file_get_contents('test_2.xml'));

    foreach ($dom->get_elements_by_tagname('palabra') as
    $element) {
    $attr = $element->attributes();
    foreach ($element->child_nodes() as $e) {
    if (isset($e->tagname)) {
    if ($e->tagname == 'uno') {
    $node = $e->first_child();
    $uno = htmlspecialchars
    ($node->node_value());
    } elseif ($e->tagname == 'dos') {
    $node = $e->first_child();
    $dos =
    htmlspecialchars($node->node_value());
    } elseif ($e->tagname == 'tres') {
    $node = $e->first_child();
    $tres =
    htmlspecialchars($node->node_value());
    } elseif ($e->tagname == 'cuatro') {
    $node = $e->first_child();
    $cuatro =
    htmlspecialchars($node->node_value());
    } elseif ($e->tagname == 'cinco') {
    $node = $e->first_child();
    $cinco =
    htmlspecialchars($node->node_value());
    } elseif ($e->tagname == 'seis') {
    $node = $e->first_child();
    $seis =
    htmlspecialchars($node->node_value());
    } elseif ($e->tagname == 'siete') {
    $node = $e->first_child();
    $siete =
    htmlspecialchars($node->node_value());
    } elseif ($e->tagname == 'ocho') {
    $node = $e->first_child();
    $ocho =
    htmlspecialchars($node->node_value());
    }
    }
    }
    $spanish= array (a => Array($uno), b => Array($dos), c => Array($tres), d => Array($cuatro), e => Array($cinco), f => Array($seis), g => Array($siete), h => Array($ocho));
    $correct = array_intersect($translate, $spanish);
    $wrong = array_diff($translate, $spanish);
    echo "<p>You got the following words correct: <font color \"00FF00\">";
    echo print_r ($correct);
    echo "</font></p>";
    echo "<p>But you got these words wrong!: <font color \"FF0000\">";
    echo print_r ($wrong);
    echo "</font></p>";

    }
    ?>



    However the array comparison functions don't seem to be working;
    Can anyone please tell me why? Is there some syntax error I'm making?

  2. #2
    You are creating keyed arrays wrong. The key needs to have quotes around it.

    PHP Code:
    $correctArray = array('myKey' => 'myValue'); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012