CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Comparing multi-array keys with other multi-array keys. (http://www.codingforums.com/showthread.php?t=288285)

Pcfr43k 02-25-2013 10:14 PM

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 :)

Arcticwarrio 02-26-2013 08:42 AM

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]);
        }
    }



Pcfr43k 02-26-2013 09:31 AM

Quote:

Originally Posted by Arcticwarrio (Post 1316005)
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?

Arcticwarrio 02-26-2013 09:59 AM

maybe something like:

PHP Code:

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



Arcticwarrio 02-26-2013 10:22 AM

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]);
        }
    }



Pcfr43k 02-26-2013 10:24 AM

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.

Arcticwarrio 02-26-2013 10:33 AM

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" } } }

Pcfr43k 02-26-2013 10:54 AM

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.:thumbsup:

Arcticwarrio 02-26-2013 11:13 AM

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) {

Pcfr43k 02-26-2013 12:16 PM

Quote:

Originally Posted by Arcticwarrio (Post 1316034)
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 :)

Arcticwarrio 02-26-2013 01:30 PM

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]);
        }
    }




All times are GMT +1. The time now is 07:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.