homepage Welcome to WebmasterWorld Guest from 54.204.58.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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




msg:4388037
 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?

 

httpwebwitch




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

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 =
Array(
"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>";
}

httpwebwitch




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

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.

rocknbil




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

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. :-)

tec4




msg:4388257
 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']))) {
continue;
}
$i++;
$ret[$i][$row['url']] = true;
$ret[$i][$row['title']] = true;

}
return($ret);

}


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!

tec4




msg:4388265
 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)?

httpwebwitch




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


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

tec4




msg:4388286
 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 :)

httpwebwitch




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

@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

$data[$row[$column]]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved