Jump to content

PHP and wildlife


ChenXiu
Go to solution Solved by Barand,

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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

image.png.a894200f2341825fb125443124285cb9.png

 

  • Great Answer 1
Link to comment
Share on other sites

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


 

Link to comment
Share on other sites

@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 😀)

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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               |
+------+---------+-----------------+

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.