Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-25-2013, 10:14 PM   PM User | #1
Pcfr43k
New Coder

 
Join Date: Jan 2010
Location: The Netherlands
Posts: 53
Thanks: 10
Thanked 0 Times in 0 Posts
Pcfr43k is an unknown quantity at this point
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
Pcfr43k is offline   Reply With Quote
Old 02-26-2013, 08:42 AM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 09:31 AM   PM User | #3
Pcfr43k
New Coder

 
Join Date: Jan 2010
Location: The Netherlands
Posts: 53
Thanks: 10
Thanked 0 Times in 0 Posts
Pcfr43k is an unknown quantity at this point
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
Pcfr43k is offline   Reply With Quote
Old 02-26-2013, 09:59 AM   PM User | #4
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
maybe something like:

PHP Code:
foreach($menuxml[0] 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.

Last edited by Arcticwarrio; 02-26-2013 at 10:26 AM..
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 10:22 AM   PM User | #5
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
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]);
        }
    }

__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.

Last edited by Arcticwarrio; 02-26-2013 at 10:24 AM.. Reason: added PHP tags
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 10:24 AM   PM User | #6
Pcfr43k
New Coder

 
Join Date: Jan 2010
Location: The Netherlands
Posts: 53
Thanks: 10
Thanked 0 Times in 0 Posts
Pcfr43k is an unknown quantity at this point
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
Pcfr43k is offline   Reply With Quote
Old 02-26-2013, 10:33 AM   PM User | #7
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
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" } } }
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.

Last edited by Arcticwarrio; 02-26-2013 at 10:36 AM..
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 10:54 AM   PM User | #8
Pcfr43k
New Coder

 
Join Date: Jan 2010
Location: The Netherlands
Posts: 53
Thanks: 10
Thanked 0 Times in 0 Posts
Pcfr43k is an unknown quantity at this point
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.
Pcfr43k is offline   Reply With Quote
Old 02-26-2013, 11:13 AM   PM User | #9
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 12:16 PM   PM User | #10
Pcfr43k
New Coder

 
Join Date: Jan 2010
Location: The Netherlands
Posts: 53
Thanks: 10
Thanked 0 Times in 0 Posts
Pcfr43k is an unknown quantity at this point
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
Pcfr43k is offline   Reply With Quote
Old 02-26-2013, 01:30 PM   PM User | #11
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Reply

Bookmarks

Tags
array, compare, multi

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:53 AM.


Advertisement
Log in to turn off these ads.