Jump to content

Search Array


AE117

Recommended Posts

I have an array I would like to search that is being built from a data base as follows

$result = mysql_query("SELECT id_one, id_two FROM accounts LIMIT 60");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

    printf("ID_ONE: %s   ID_TWO: %s", $row[0], $row[1]);  
echo "<br>";
}

 

What I would like to do is after the array is created echo out a certain row from that array.

Soi if the array looks like this

 

key  id_one  id_two

1        458      444

2          468      455

3        467        466

 

I can search the array for 468 and it would return 455

 

Thanks  I will be posting back if I find any new information my self.

Link to comment
Share on other sites

Hops this helps :

 

 


<?php
$db = mysql_connect('localhost','root','') or die("Database error");
mysql_select_db('test', $db);

$result = mysql_query("set names 'utf8'");

$query = "select * from test";
$result = mysql_query($query);

$returnArr = array();
while($row = mysql_fetch_array($result)){
$returnArr[$row['id_1']] = $row['id_2']; 
}

$searchArr = 100; // Enter the key to be searched

if(array_key_exists($searchArr,$returnArr)){ // This will check the whether key exists or not
echo "Available Value Is :". $returnArr[$searchArr];
}else{
echo "Key Doesnt Exists";
}


?>





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.