Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Check if item is in array, grab corresponding value and echo it



1:02 pm on Nov 17, 2011 (gmt 0)

Hi Guys/Gals,

Changing the structure of an array a bit from and can not figure out how to get & check the data properly for the life of me.

Array Looks Like:

Array ( [1] => Array ( [http://example1.com] => 1 [My Search Title] => 1 ) [2] => Array ( [http://example2.com] => 1 [Another Search Title] => 1 )[3] => Array ( [http://example3.com] => 1 [Yet Another Title] => 1 ) )

What I'm trying to do:

Basically, I am trying if the URL is the current URL of a web page. If so, then get the corresponding Title to that URL and echo it to the page - else place default title for user.

Have tried combinations such as:

array_key_exists("http://www.example1.com,$session->usersavedpages)) {
echo "In Array";
} else {
echo "Not In Array";

Have also tried in_array and a few other functions but no luck.

But no luck so far..any suggestions?


4:50 pm on Nov 17, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

looks like your array is deeper than it needs to be, and arranged in a weird way

I'd arrange the array in a much simpler way

$titles =
"http://example1.com" => "title of page 1",
"http://example2.com" => "title of page 2",
"http://example3.com" => "title of page 3",

then you can get the title like this:

if (isset($titles[$url])){
echo "<title>htmlspecialchars($titles[$url])</title>";
} else {
echo "<title>default title</title>";


4:54 pm on Nov 17, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Are you stuck with the data formatted like that? Or is that something you can change?

You've got all these little arrays within an array, and you're using keys where values would be more appropriate. It means PHP has to iterate through the array in order to find if any of them contain an array with a key that matches. That's backward. Even if you employ functions like array_key_exists(), the work of iterating the array is still being done and it's highly inefficient... and awkward.


5:31 pm on Nov 17, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

This might not work but offhand I'd try

if (in_array(array_key_exists("http://www.example1.com,$session->usersavedpages))) {
echo "ta da";

Never tried a nested array function but it **should** work if it doesn't. :-)

So you're checking if a value is in list array and that value is actually checking an associative_array for the key. If it's found, it will be "in the outer array" and should return true. Sorry don't know how else to verbalize it. :-)


8:51 pm on Nov 17, 2011 (gmt 0)

@httpwebwitch- I definitely have the ability to change the array. I am building it from a mysql query to store the array with the users Saved Pages (URL, Title).

Current Function (new the this so may be shaky..):

function getUserSavedPages($userid){
$q = "SELECT url,title FROM ".TBL_SAVED_SEARCHES." WHERE userid = '$userid'";

$result = mysql_query($q, $this->connection);

/* Error occurred, return given name by default */
if(!$result || (mysql_numrows($result) < 1)){
return NULL;

$ret = Array();
$i = 0;
while($row = mysql_fetch_assoc($result)) {
if(!strlen(trim($row['url']))) {
$ret[$i][$row['url']] = true;
$ret[$i][$row['title']] = true;



I've been toying with getting the structure how you put in your example but have not been able to modify it to look like your example for the array (I'm sure I'm just overlooking something probably).

Have tried stacking this it like:

$ret[$row['url']][$row['title']] = true;

But ends out coming out like this:

Array ( [http://www.example1.com] => Array ( [Title of Page 1] => 1 )

which still seems to be deeper than desired.

@rocknbil - sounds like an interesting solution and am going to give it a try, for if nothing else - a pure learning experience...If possible I'll try to optimize the array thought - sounds like I've got a pretty poor one so far so no harm in improving :P

Thanks for explaining it - it is a bit confusing but got to be confused to learn :) will report my findings on it either way though

Thank you both for your quick responces!


9:11 pm on Nov 17, 2011 (gmt 0)

@rocknbil - did a quick try the code you put as above as I was what the results would yield and it showed:

Warning: in_array() expects at least 2 parameters, 1 given in /home/...... on line 95

Then tried adding the ($session->usersavedpages) in the below code as a second parameter to the in_array fucntion as follows:

if (in_array(array_key_exists("http://www.example1.com",$session->usersavedpages)),$session->usersavedpages) {
echo "ta da";

but got:

Parse error: syntax error, unexpected ',' in /home/.... on line 95

Perhaps it does not work because array_key_exists() returns a Boolean value and not a string (which is required for the in_array function to work as the first parameter)?


9:36 pm on Nov 17, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

$ret[$row['url']] = $row['title'];


9:48 pm on Nov 17, 2011 (gmt 0)

httpwebwitch, got to make it look so easy? :\ lol

That worked like a charm and used your code above to retrieve the tile...golden :)


7:43 am on Nov 20, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

@tec4, arrays *are* easy. I think you need to change the way you conceive of them... the original example is made up of complex structures where the value is always 1, or true. As though the existence of an array element having a value of true makes it substantial. But instead what you've got is a complex beast that has to be iterated in weird ways to get anything useful from it, at great cost (memory and CPU and slow execution). That struck me as ... someone didn't explain to you how arrays are supposed to work.

An associative array is a collection of keys and values. Each key is a unique "position" in the array, and it has a value. That value could be any type of thing - a string, integer, another array, or even an object or a resource like a database connection.

When you're designing data architecture, the KEY is the unique - primary - identifier that you (hopefully) have. In your case, you have the page URL. The value will be the datum you want to get by looking up the key. In your case, the value is page's title.

The [] construct is PHP's way of saying "value for the key of". Eg $x = $array[$key] means "look in $array for the element with a key of $key, and get the value of it."

When you retrieve a row of data using a command like mysql_fetch_array(), the result is a set of key=>value pairs, where the key is the column name, and the value is the datum that is stored in that column. Hence, $row[$column].

So if you are retrieving the key of an array from a database row, in english that's "get the value from $data where the key is the value of the column $column in $row". It's awkward to say in english, so be thankful that in PHP we just say


Featured Threads

Hot Threads This Week

Hot Threads This Month