diamondnular
Members-
Posts
23 -
Joined
-
Last visited
Never
Profile Information
-
Gender
Not Telling
diamondnular's Achievements
Newbie (1/5)
0
Reputation
-
[SOLVED] sort an array by the values of keys in another array
diamondnular replied to diamondnular's topic in PHP Coding Help
This code works! It takes generally half of the running time of my code. I think it is much more efficient than mine. Thanks Barand. D. -
[SOLVED] sort an array by the values of keys in another array
diamondnular replied to diamondnular's topic in PHP Coding Help
Arhhh, how can I make that simple mistake? Anyway, thanks ProjectFear. What about the other question? Can I archive the same thing with sort, asort, usort or something similar? Thanks, D. -
Hi folks, I have two arrays: $e = Array ( [0 ] => Array ( [id] => 3 [value] => a ) [1] => Array ( [id] => 2 [value] => b ) [2] => Array ( [id] => 5 [value] => c ) ) and $d = Array ( [0 ] => 5 [1] => 2 [2] => 3 ) What I want to do is to sort array $e with values of 'id' of its elements to be the same order as array $d, meaning that after sorting, new array will be $f = Array ( [0 ] => Array ( [id] => 5 [value] => c ) [1] => Array ( [id] => 2 [value] => b ) [2] => Array ( [id] => 3 [value] => a ) ) Here is what I did: <?php $a = array('id' => 3,'value' =>'a'); $b = array('id' => 2,'value' =>'b'); $c = array('id' => 5,'value' =>'c'); $d = array('5','2','3'); print_r($d); $e = array(); array_push($e,$a); array_push($e,$b); array_push($e,$c); print_r($e); $f = array(); foreach ($d as $key) { foreach ($e as $x) { if ($x[id] == $key) array_push($f,$x); } } print_r($f); ?> The code does work, and gave me the result I want. But I have two questions here: 1. I got a notice: Use of undefined constant id - assumed 'id'. That should be $x[id] == $key How do I fix this notice? 2. More important: I think there must be an easier way to do the above, using asort or usort or some function that I maybe not know of. The above is doable, but it is too handy, and not very efficient in my opinion. You guys masters in php have any advice? Remember, I am just novice in php Thanks, D.
-
[SOLVED] preg_match, preg_match_all and preg_grep
diamondnular replied to diamondnular's topic in Regex Help
Thanks Effigy. D. -
[SOLVED] preg_match, preg_match_all and preg_grep
diamondnular replied to diamondnular's topic in Regex Help
Because character classes have their own set of metacharacters. Outside of a character class ^ serves as an anchor, inside it serves as negation. If you want to match all beginning and end tags /<[^>]+>/ will suffice. Got it now, thanks Effigy. Do you know of a good and full reference for PHP REGEX? D. -
[SOLVED] preg_match, preg_match_all and preg_grep
diamondnular replied to diamondnular's topic in Regex Help
My mistake. It should read [\/]*. Now I think I understand a little more about REGEX . -
[SOLVED] preg_match, preg_match_all and preg_grep
diamondnular replied to diamondnular's topic in Regex Help
Got it now. In order to pull out all the < () > <?php $html = "<b>bold text</b><c>bold text</c><a href=howdy.html><img src='test.jpg' />click me</a>"; preg_match_all("/<[^>]*>/", $html, $matches, PREG_PATTERN_ORDER); print_r($matches); ?> ^> means there is NO addition > in the match. Why this page (http://php-regex.blogspot.com/2008/01/php-regex-cheat-sheet.html) says that ^ is start of the subject??? Anybody corrects me? In order to pull out the img tag only (which is what I am trying to do) <?php $html = "<b>bold text</b><c>bold text</c><a href=howdy.html><img src='test.jpg' />click me</a>"; preg_match_all("/<[^>]*\/>/", $html, $matches, PREG_PATTERN_ORDER); print_r($matches)."\n"; ?> Anybody knows which REGEX reference I should use for preg_match? The page above seems to not work for me (or I am too retarded???). D. -
[SOLVED] preg_match, preg_match_all and preg_grep
diamondnular replied to diamondnular's topic in Regex Help
$html is not an array, so you would not use preg_grep. You want to find all occurrences, so you would use preg_match_all since it indicates that it is global. Use str_replace if the string is static; if not, use preg_replace. Thanks Effigy, that is why I am playing with preg_match_all now. Just wonder with your guys experience so that I can learn from you. But I have to admit that PHP regex really drives me crazy. <?php $html = "<b>bold text</b><c>bold text</c><a href=howdy.html>click me</a>"; preg_match_all("/<[\/*]\w>/", $html, $matches, PREG_SET_ORDER); ?> the [\/*] means zero or more \/, so both <b> and </b> will match. But I ran the code, and the output are just </b>,</c>,</a> . What I am doing wrong here? -
[SOLVED] preg_match, preg_match_all and preg_grep
diamondnular replied to diamondnular's topic in Regex Help
I already read all above and others as well before asking here (as I said examples there are not enough for me). And I think I have general ideas how they work. Thanks for you input anyway . -
Hi folks, Sorry if I am too noob. I am trying to understand how all these functions work, and examples in php.net are not enough for me. What are the MAIN difference between these functions? For example, with $html below, what function I should use to find ALL html tags (<b>, </b>, <img ... />)? Also, if I want to replace img tag <img ... />, str_replace is the best function to use here? $html = "<b>bold text</b><a href=howdy.html><img src='test.jpg' />click me</a>"; Thanks a bunch, D.
-
[SOLVED] how to check th-run inside a function?
diamondnular replied to diamondnular's topic in PHP Coding Help
Thank you very much Thorpe . D. -
[SOLVED] how to check th-run inside a function?
diamondnular replied to diamondnular's topic in PHP Coding Help
Wow, it is so simple that I can not figure it out Thanks a bunch Thorpe. Just another question: can we implement this for the th-running? For example, the check will run if the th-run is third, and will not if others. Thanks, D. -
Hi folks, I am coding a page, and the page will call one php function several times. Here is how it works: <?php function display() { // check if run the first // first run task echo 'FIRSTRUN - '; // if not the first, then neglect echo 'test' . "\n"; } ?> so for example, if the function is called 5 times when the page is loaded, the result will be FIRSTRUN - test test test test test Anybody has an idea that will be possible to do? Note that the th-running check is implemented each time the page is loaded, not just the very first time when the function is called. Thanks, D.
-
[SOLVED] how to return variable variable?
diamondnular replied to diamondnular's topic in PHP Coding Help
Got it sorted now . I made a typo when typing in the codes, and it seemed taking me forever to find it without your helps. Thanks for prompt helps, guys D. -
[SOLVED] how to return variable variable?
diamondnular replied to diamondnular's topic in PHP Coding Help
Uhm, interesting. I just tried to run with just the php file alone, and it works too! The problem arose when I tried to modify some codes in a module in Joomla 1.5.3, and with the similar code above, my server returns error: [Mon Jul 07 19:15:33 2008] [error] [client 127.0.0.1] PHP Catchable fatal error: Object of class stdClass could not be converted to string in .../default.php on line 12, referer: http://127.0.0.1/joomla/ Maybe this is some kind of restrictions in Joomla engine, not the problem of the php code.