Jump to content

[SOLVED] Problems with array item, trying to split the item...


kyleabaker

Recommended Posts

Hey all. I'm fairly new to php, been here a few times now for help with great results! But now I'm stuck and I'm not sure exactly what the problem is, but I'll try my best to explain it to you.

 

On my website I'm trying to make a search thru my mySQL database, so it any keywords are found then I'm adding the details to the next item in an array that I create. I've tested the array and it is storing the information into the array properly because it echoes out all of the information correctly. My problem is that, at the beginning of each item in the array I have information that tells me what part of the database it was pulled from and the id number and also how many times the a keyword was found in that specific database entry. So you have something like this..

 

Picture of the Array{

news.id=1.hits=3.data from the item here

comments.id=5.hits=2.data from the item here

links.id=7.hits=5.data from the item here

blah.id=8.hits=1.data from the item here

}

 

My problem is that when I am trying to split the array item [split(".",array,2)] it's not working as I am thinking. I know the problem is obviously something in my code. I'm wanting to basically split the first 3 parts that are divided by "." so I can get length since the id will vary and use the combined lengths + the number of periods to set the left point of substr() then set the right point to be the length of the array item. That should effectively leave the data remaining so I want to print the data out with an echo as the result, but for some reason when I test to see if it is news or comments, etc it never matches for the first split at "."'s....so my results don't echo cause nothing matches. I hope this is somewhat understandable.

 

Any help would be great. I think an item in an array is basically a string, but I'm not sure why it is not handling like I have in my head. I may also be using split incorrectly. I looked at the php manuals but I might still have it wrong. Thanks in advanced!

split() is a regex based function, and setting the pattern to "." will match any character.  Here's your solution:

 

<?php
foreach ($yourArray as $key=>$val) {
     $val = array_splice(explode(chr(46), $val), 0, 2);
}
?>

 

The array will now have values without the first two period-separated areas.

Thanks Glyde! The "." was my only problem, so once I replaced that with chr(46) it started working perfectly! Now I just have to figure out how to sort the array by the tags that I have in them, like the number of hits. So the hits are stored as the 3rd part (type.id.hits.data) so can I have it sort the array that way? Then my search results will make more sense.

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.