/    Sign up×
Community /Pin to ProfileBookmark

Lost value of array after if compare

I have this code

[CODE]foreach ($result2 as $rodic1 => $rodic_hodnot1)
{
foreach ($etc_uloziste as $id => $etc_id)
{
foreach ($etc_id as $id2 => $etc_id2)
{
//echo $etc_id2.”n”;
if($rodic1 ==$id)
{
//echo $etc_id2.”n”;
}
}
}
}[/CODE]

where the $etc_id2 is like library or room and $id is book
[book] => Array
(
[0] => library
[1] => room
)
But problem is when i //echo $etc_id2.”n” after if($rodic1 ==$id) i get only [0] => library.
My question in here is why i cant get [1] => room . And how can i change mzý code to get it .
Thank you for read my post and help

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyApr 16.2014 — Is there any way you could post the [B][I]$result2[/I][/B] array object? Debugging your issue would be a lot easier if I had the data being used that seems to present the problem.
Copy linkTweet thisAlerts:
@hang_tranauthorApr 16.2014 — I have my $result2 in there http://pastebin.com/CMngg682 Sup3rkirby's

Thanl you
Copy linkTweet thisAlerts:
@Sup3rkirbyApr 16.2014 — I'm sorry but it looks like I wasn't fully paying attention to your nested loops. It seems you have 2 arrays you are looping through: [B]$result2[/B] and [B]$etc_uloziste[/B]. Could you share the [B]$etc_uloziste[/B] array as well so I can compare these two arrays?
Copy linkTweet thisAlerts:
@hang_tranauthorApr 16.2014 — And this is my $etc_uloziste http://pastebin.com/M5dR4jsC
Copy linkTweet thisAlerts:
@Sup3rkirbyApr 16.2014 — Alright, bare in mind that I don't really understand the data structure you have in the sense that I have no idea what this data is for and what it means. Thus I'm not really sure what you are trying to achieve here or what the goal for this data is.

From what I can tell though, there isn't anything 'wrong' with your code. This is just a matter of you need to loop through these arrays differently to get the result you want. Which goes back to the fact that I don't know exactly what you are trying to achieve so I can't really tell you what your code should look like.

I can however make a suggestion that could help you see exactly what's going on in your code and thus help you tweak it to your needs.
[code=php]
foreach($result2 as $rodic1 => $rodic_hodnot1) {

foreach($etc_uloziste as $id => $etc_id) {

foreach($etc_id as $id2 => $etc_id2) {

echo $rodic1 . " == " . $id . " (" . $etc_id2 . ") <br />";
}
}
}
[/code]

Now to explain what this is and how it might help. You are comparing [B]$rodic1[/B] and [B]$id[/B] in your [B][I]if()[/I][/B] statement before you echo out [B]$etc_id2[/B] on the page. If you run this, it will echo out every single [B][I]if()[/I][/B] statement so you can see exactly what the code compares, and in parenthesis it echoes out the value that would be shown on the page if the statement is true.

Since you are expecting to see 2 values from part of your array but are only seeing one of them instead, running this will show you every value (including the ones you expect to see), but will show the [I]if()[/I] statement being used to show or not show that value. So you should be able to look for '[1] => room' (in parenthesis) and find out which [B][I]if()[/I][/B] statement is being used. There you will be able to see why it doesn't match and thus why you don't see it on the page.


Keep in mind this is just a way to help you debug your own problem. Because I know nothing about this data or what you are trying to achieve I can't be of much more help at the moment. If you could explain what it is you are trying to do then I could work out the issue myself and help you organize your loops.
Copy linkTweet thisAlerts:
@hang_tranauthorApr 16.2014 — thank you it help me
Copy linkTweet thisAlerts:
@hang_tranauthorApr 16.2014 — I think i have the problem to loop find something and if match and it dont loop more to the end
×

Success!

Help @hang_tran spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...