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
https://forums.phpfreaks.com/topic/223661-search-array/
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
https://forums.phpfreaks.com/topic/223661-search-array/#findComment-1156142
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.