ChenXiu Posted March 12, 2023 Share Posted March 12, 2023 Noah is having trouble with his PHP code $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); $received = array( '3 cows', '7 fish' ); foreach($ark as $expected) { echo $expected; foreach($received as $line) { if($expected == $line) { echo ' ARRIVED!'; } break; } echo '<hr>'; } The result should be: 3 cows ARRIVED! 7 fish ARRIVED! 3 cows 1 cat What can Noah do to fix his code? Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/ Share on other sites More sharing options...
Barand Posted March 12, 2023 Share Posted March 12, 2023 Remove the break Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606477 Share on other sites More sharing options...
ChenXiu Posted March 12, 2023 Author Share Posted March 12, 2023 Without the break, it puts "arrived!" after everything 3 horses arrived! 7 fish arrived! 3 horses arrived! 1 cat I'm trying to get PHP to move onto the next array value when a match is found. This is way *way* harder than it looks. Maybe arrays aren't the way to do this. Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606478 Share on other sites More sharing options...
Barand Posted March 12, 2023 Share Posted March 12, 2023 The cat hasn't arrived and the others have. What's wrong with that? Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606479 Share on other sites More sharing options...
jodunno Posted March 12, 2023 Share Posted March 12, 2023 try removing the received/arrived loop and go back to the basics. An if block or two can go a long way with logic and critical thinking. Let's look at verbose and redundant basic code: <?php $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); $received = array( '3 cows', '7 fish' ); $reckoning = count($received) -1; $line = 0; $endOfTheLine = 0; foreach($ark as $expected) { echo $expected; if (!$endOfTheLine) { if ($line <= $reckoning) { if ($received[$line] === $expected) { echo ' ARRIVED!'; $line++; } } else { $endOfTheLine = 1; } } echo '<br>'; } ?> you should see the results that you are seeking. Now you just need to apply that logic to the inner loop to control what is to be expected as an arrival. Best wishes, John Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606482 Share on other sites More sharing options...
Solution Barand Posted March 12, 2023 Solution Share Posted March 12, 2023 or $ark = array( '3 gerbils', '7 fish', '3 gerbils', '1 cat' ); $received = array( '3 gerbils', '7 fish' ); foreach($ark as $j => $expected) { echo $expected; foreach($received as $k => $line) { if($expected == $line) { echo " ARRIVED"; unset ($received[$k], $ark[$j]); // cross them off the lists } } echo "<hr>"; } giving... 1 Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606483 Share on other sites More sharing options...
ChenXiu Posted March 12, 2023 Author Share Posted March 12, 2023 I'll repost my original post: The ark has 14 animals on it: 6 cows, 7 fish, and a cat (keeps the mice away) $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); When the Ark arrived, Billy made a checklist: $received = array( '3 cows', '7 fish' ); As you see, his checklist has only 10 animals: 3 cows, and 7 fish. (story has it that the cat and 3 of the cows had a scheme to escape, right from the beginning. But that's another story....) So the PHP script should yield this: 3 cows ARRIVED! 7 fish ARRIVED! 3 cows 1 cat .... again, this is way *way* harder than it looks, because arrays are so finicky. When I saw your post, I immediately thought how easy this would be using mySQL: Simply read the textfile line by line into a temp mySQL table, and use "limit 1" for the queries. But I want to see if this can be cracked using PHP arrays... Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606484 Share on other sites More sharing options...
ChenXiu Posted March 12, 2023 Author Share Posted March 12, 2023 Hmm.... I see 2 replies while I was typing my last post. I'll try them both. Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606485 Share on other sites More sharing options...
ChenXiu Posted March 12, 2023 Author Share Posted March 12, 2023 @Barand, thank you! That thing you do -- that putting the "key-back-into-the-array-thing" -- $received[$k] -- got me again! I still don't know how you know when to do this. But it works perfect! Thank you again! Your code appears to work perfectly! Had you not come up with a solution, my next step would have been to use mySQL :-) @jodunno, thank you! Your code worked. The logic you used makes perfect sense now.... but was out of my grasp this past week I've been trying to figure this on out. (I'm sure unintentional, but it is amusing how your variable name "$reckoning" sorta fits right in there with Noah and $ark 😀) 1 Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606486 Share on other sites More sharing options...
jodunno Posted March 12, 2023 Share Posted March 12, 2023 or you can cut the additional three variables of a redundant second loop and get straight to the kill shot. Snipe the Snipe. <?php $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); $received = array( '3 cows', '7 fish' ); $line = 0; foreach($ark as $expected) { echo $expected; echo !empty($received[$line]) && $received[$line] === $expected ? ' ARRIVED!' : ''; $line++; echo '<br>'; } ?> so much less code and clutter. hamster wheeling is always confusing, difficult and often redundant. Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606487 Share on other sites More sharing options...
Barand Posted March 12, 2023 Share Posted March 12, 2023 SQL solution TABLE: ark TABLE: received +----+------+-----+ +----+------+-----+ | id | item | qty | | id | item | qty | +----+------+-----+ +----+------+-----+ | 1 | cow | 6 | | 1 | cow | 3 | | 2 | fish | 7 | | 2 | fish | 7 | | 4 | cat | 1 | +----+------+-----+ +----+------+-----+ SELECT a.item , coalesce(r.qty,0) as Arrived , a.qty - coalesce(r.qty,0) as `Lost in transit` FROM ark a LEFT JOIN received r USING (item) +------+---------+-----------------+ | item | Arrived | Lost in transit | +------+---------+-----------------+ | cow | 3 | 3 | | fish | 7 | 0 | | cat | 0 | 1 | +------+---------+-----------------+ Quote Link to comment https://forums.phpfreaks.com/topic/316001-php-and-wildlife/#findComment-1606488 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.