Jump to content

[SOLVED] array problem


Goose87

Recommended Posts

Ok, well I have a list of items, and they are all displayed down the page in a table...

 

The problem I'm having is knowing which one they have pressed.  When they press one of the buttons, it changes the url to ....?id=4 for example.

 

I then take the number 4, and I'm trying to get that to know what type of thing that is in the database.

 

At the moment i have

 

$array1=array(1, 4, 8, 12);

$array2=array(2, 5, 9, 13);

$array3=array(3,6,10,14);

$array4=array(4,7,11,15);

 

I then have:

 

if(isset($_GET['id'])) {

 

* which works *

 

and then I have..

 

if(isset($array1[$_GET['id']])) {

 

* do something * 

} elseif(isset($array2[$_GET['id'])) {

 

* do something...

 

ETC. ETC..

 

The first part of getting the "id" from the url works fine, but the array part just isnt working.  Any advice would be grately appreciated.

Link to comment
https://forums.phpfreaks.com/topic/79732-solved-array-problem/
Share on other sites

erm, i think so..

basically if the ID is 4 for example, i want that to do the code that is within the if(isset($array1) part, and if the ID is like 6, it'll only implement the code that is related to the $array that is related to that.

 

Do you get what i mean?  I didn't want to post all of my code becuase there is a lot of extra parts included in it.

Link to comment
https://forums.phpfreaks.com/topic/79732-solved-array-problem/#findComment-403773
Share on other sites

Im not sure if its going to make a difference but you are trying to compare an integer in your array to the $_GET['id'] value which is more than likely a string.  You could try adding intval() to your $_GET['id'] value to convert the GET value from string to integer before the comparison takes place like:-

 


if(isset($array1[intval($_GET['id'])])) {

* do something *  

Link to comment
https://forums.phpfreaks.com/topic/79732-solved-array-problem/#findComment-403778
Share on other sites

If you can change the array to be two dimensional, you can do something like this:

<?php
$large_array=array(1=>array(1, 4, 8, 12),
                2=>array(2, 5, 9, 13),
                3=>array(3,6,10,14),
                4=>array(4,7,11,15));
$id = $_GET['id'];
$found = false;
$found_index = 0;
for ($i=1; $i<=count($large_array) && !$found;$i++)
        if (in_array($id,$large_array[$i])) {
                $found_index = $i;
                $found = true;
        }
echo $id . ' was found in array ' . $found_index .  "<br>";
print_r($large_array[$found_index]);
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/79732-solved-array-problem/#findComment-403800
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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