Jump to content

Recommended Posts

<?php

$todo = array( 
1 => "9AM Wake Up.",
2 => "10AM Drink make hot tea.",
3 => "11AM Finish reading CNET news.",
4 => "12AM Shower and get dressed.",
5  => "1PM Finish watering the garden.",
6  => "2PMTake a nap.",
7  => "3PM Dream about something fantastic.",
8  => "4PM Wake up from nap.",
9  => "5PM Go to work.",
);


for ($i = 1; $i <= 20 ; $i++) { // notice the 20 in order to see the whole readout of array_search.
	//echo $i ."<br>";
	$key = array_search($i, $todo); 
	echo $key ."<br>";

}//end of for

?>

 

Print out is exactly.

 

5

6

7

8

9

 

 

 

1

2

3

4

 

What would be causing this?

 

Link to comment
https://forums.phpfreaks.com/topic/172337-solved-array_search-for-loop-question/
Share on other sites

1

2

3

4

5

6

7

8

9

I get that when I do

echo $i ."<br>";

I'm just unsure why

the following doesn't give it in order and adds additional <br> instead.

$key = array_search($i, $todo); 
	echo $key ."<br>";

 

I'm just trying to understand it is all really.

I did before I posted but didnt see anything direct enough for me I suppose. I'll check again. Thanks guys.

 

Also, I have to add, I've learned about as much or more from this forum than I have from my PHP book and a class I took a while ago. Thanks for the help again.

I did before I posted but didnt see anything direct enough for me I suppose. I'll check again. Thanks guys.

 

Also, I have to add, I've learned about as much or more from this forum than I have from my PHP book and a class I took a while ago. Thanks for the help again.

 

array_search() searches an array's values.  The first instance of the value '1' is the 5th element of the array, whose value is "1PM Finish watering the garden."

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.