Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question Comparing multi-array keys with other multi-array keys.

    So I got this piece of script puzzled out:

    PHP Code:
    foreach($menuxml as $key => $menuitem) {
        if( !
    in_array($menuitem$menufile)) {
            unset(
    $menuxml[$key]);
        }

    Where $menuxml is filled from a piece of script reading a xml file and $menufile from a script reading files.

    Now I have that if one $menuitem mismatches, the entire $menuxml is empty.
    And I figured out that every $menuitem has to be compared with every entry in $menufile. But how do I get that done? I got $menufile in there instead of it's entries.

    Content of $menuxml and $menufile:
    $menuxml:
    Code:
    Array
    (
        [0] => SimpleXMLElement Object
            (
                [0] => Start
            )
    
        [1] => SimpleXMLElement Object
            (
                [0] => Dummy page
            )
    
        [2] => SimpleXMLElement Object
            (
                [0] => lorem ipsum
            )
    $menufile:
    Code:
    Array
    (
        [0] => Dummy%20page%202
        [1] => Dummy%20page
    )
    My goal: if $menuxml entry is not in a $menufile entry, unset that $menuxml entry.

    Correct me in anyway you see fit
    Last edited by Pcfr43k; 02-25-2013 at 10:15 PM. Reason: Little code fix

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    723
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you could try 2 loops?

    im not sure if [0] => Dummy page will be found as [1] => Dummy%20page

    PHP Code:

    foreach($menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2 {
            if (
    $v1 $v2) {
                    unset(
    $menuxml[$k1]);
            }
        }

    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    you could try 2 loops?

    im not sure if [0] => Dummy page will be found as [1] => Dummy%20page

    PHP Code:

    foreach($menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2 {
            if (
    $v1 $v2) {
                    unset(
    $menuxml[$k1]);
            }
        }

    Well, it kinda worked. I changed the XML content in Dummy%20page, etc...
    Put the ')' behind the first $v2, and it did run, but got out empty again.
    I think this is happening:

    Once 'Array( [0] => Dummy%20page%202' is coming in, is checks with the first child of $menuxml: Array( [0] => SimpleXMLElement Object( [0] => Start ), instead of the child of SimpleXMLElement Object.

    So how do I get the child-child? Or am I wrong there?

    Edit:
    I rebuilded my $menuxml so it has the data as a direct child, and it still gets erased...
    This is $menuxml at first:
    Code:
    Array
    (
        [0] => Start
        [1] => Dummy%20page
        [2] => Dummy%20page%202
    )
    And after this:
    PHP Code:
    foreach($menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2) {
            if (
    $v1 $v2) {
                    unset(
    $menuxml[$k1]);
            }
        }

    It turns into this:
    Code:
    Array
    (
    )
    What is going wrong?
    Is it the id ([0], [1], etc...) that mismatches?
    Last edited by Pcfr43k; 02-26-2013 at 09:58 AM. Reason: Tried another way

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    723
    Thanks
    20
    Thanked 84 Times in 84 Posts
    maybe something like:

    PHP Code:
    foreach($menuxml[0] as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2) {
            if (
    $v1 $v2) {
                    unset(
    $menuxml[$k1]);
            }
        }

    Last edited by Arcticwarrio; 02-26-2013 at 10:26 AM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    723
    Thanks
    20
    Thanked 84 Times in 84 Posts
    PHP Code:
    $menuxml[0]["SimpleXMLElement Object"][0
    = "Start"

    so this should work:

    PHP Code:
    foreach($menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2) {
            if (
    $v1 $v2) {
                    unset(
    $menuxml[$k1]["SimpleXMLElement Object"][0]);
            }
        }

    Last edited by Arcticwarrio; 02-26-2013 at 10:24 AM. Reason: added PHP tags
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #6
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I will try that one, and i have an idea... maybe it will work.

    Edit:
    Both don't seem to work...
    I putted my code here: --link deleted by poster--
    You can see the nice build up if you see the source code of the page.
    Last edited by Pcfr43k; 02-26-2013 at 11:00 AM. Reason: Tested items | link deleted

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    723
    Thanks
    20
    Thanked 84 Times in 84 Posts
    sorry i had an equals mising :S

    PHP Code:
    <?php
    $menuxml 
    = array(
        
    => array("SimpleXMLElement Object" => array(=> "Start")),
        
    => array("SimpleXMLElement Object" => array(=> "Dummy%20page")),
        
    => array("SimpleXMLElement Object" => array(=> "lorem ipsum"))
        );
        
    $menufile = array(
        
    => "Dummy%20page%202",
        
    => "Dummy%20page"
    );

    foreach(
    $menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2) {
            if (
    $menuxml[$k1]["SimpleXMLElement Object"][0] == $v2) {
                    unset(
    $menuxml[$k1]["SimpleXMLElement Object"][0]);
            }
        }
    }

    var_dump($menuxml);

    ?>
    outputs:

    array(3) { [0]=> array(1) { ["SimpleXMLElement Object"]=> array(1) { [0]=> string(5) "Start" } } [1]=> array(1) { ["SimpleXMLElement Object"]=> array(0) { } } [2]=> array(1) { ["SimpleXMLElement Object"]=> array(1) { [0]=> string(11) "lorem ipsum" } } }
    Last edited by Arcticwarrio; 02-26-2013 at 10:36 AM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #8
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Well, it seems it keeps clearing the entire array in my full script, so I begin to think something in my full script is causing it...

    I luckily have a teacher at my school who can browse trough my whole code, so I will try that.

    Arcticwarrio; thank you for your help, now I understand some more about it and might be of support later on.

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    723
    Thanks
    20
    Thanked 84 Times in 84 Posts
    YOU STILL HAVE 1 = IN THE MENU.TXT LINK YOU POSTED:

    PHP Code:
    foreach($menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2) {
            if (
    $v1 $v2) {
                    unset(
    $menuxml[$k1]);
            }
        }

    .

    if ($v1 = $v2) { should be if ($v1 == $v2) {
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #10
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by Arcticwarrio View Post
    YOU STILL HAVE 1 = IN THE MENU.TXT LINK YOU POSTED:

    PHP Code:
    foreach($menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2) {
            if (
    $v1 $v2) {
                    unset(
    $menuxml[$k1]);
            }
        }

    .

    if ($v1 = $v2) { should be if ($v1 == $v2) {
    Tnx!, I had overwritten it XD.
    It works now

  • #11
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    723
    Thanks
    20
    Thanked 84 Times in 84 Posts
    sorry wrong quote also...

    this is the one that works lol

    PHP Code:

    foreach($menuxml as $k1 => $v1) {
        foreach(
    $menufile as $k2 => $v2) {
            if (
    $menuxml[$k1]["SimpleXMLElement Object"][0] == $v2) {
                    unset(
    $menuxml[$k1]["SimpleXMLElement Object"][0]);
            }
        }

    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Tags for this Thread

    Posting Permissions

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