dagnasty Posted October 31, 2006 Share Posted October 31, 2006 I have 3 questions that I've been unable to find understandable google results for.1. I have a variable that contains the HTML of a page and was wondering if it'd be better to unset() it after I'm done with it, or leave it as is. Will the time spent doing the unset() not be worthwhile beceause of the time it takes the execute the function on a variable the string size of an HTML page?2. I have a multidimensional array and want to count the (not sure with terminology, so I'll use and X) X part of this array, how would I go about it?ex: $array[0][X];3. This question has something to do with regexp but I don't feel it's worth posting a query in the regexp section. When doing a preg_match is there a way to only get the information between a pattern instead of returning the pattern and the stuff between it. ex:[code]$lookhere = "stuff here <tag>the good stuff</tag> stuff there";preg_match("/<tag>([^`]*?)<\/tag>/", $lookhere, $output);[/code]this would turn out "<tag>the good stuff</tag>" but what I want is just "the good stuff"Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/ Share on other sites More sharing options...
scliburn Posted October 31, 2006 Share Posted October 31, 2006 $output['0'] = "<tag>the good stuff</tag>"but$output['1'] = "the good stuff"output is now an array. Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117466 Share on other sites More sharing options...
Barand Posted October 31, 2006 Share Posted October 31, 2006 Ans 2)[code]<?php$a = array ( array (1,2,3,4,5), array (6,7,8), array (9,0));$counts = array();foreach ($a as $k => $v) { $counts[$k] = count($v); $total += $counts[$k];}echo '<pre>', print_r($counts, true), $total, '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117468 Share on other sites More sharing options...
Caesar Posted October 31, 2006 Share Posted October 31, 2006 [quote author=scliburn link=topic=113388.msg460715#msg460715 date=1162322012]$output['0'] = "<tag>the good stuff</tag>"but$output['1'] = "the good stuff"output is now an array.[/quote]Or:[quote]3. This question has something to do with regexp but I don't feel it's worth posting a query in the regexp section.When doing a preg_match is there a way to only get the information between a pattern instead of returning the pattern and the stuff between it.[/quote][code]<?php$lookhere = "stuff here <tag>the good stuff</tag> stuff there";preg_match('/<tag>(.*)</tag>/',$lookhere,$output);// $output will equal: Array ( [0] => the good stuff [1] => the good stuff )echo"$output[0]";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117495 Share on other sites More sharing options...
scliburn Posted October 31, 2006 Share Posted October 31, 2006 ans#1actually opinion. Just unset it. It's sitting in memory otherwise. I aggregate data all day long, and resources are extremely valuable to me.If it's not hurting anything leave alone. If you have resource intensive scripts, unset it. I didn't realize it took any real processing time to unset a scalar. Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117502 Share on other sites More sharing options...
sasa Posted October 31, 2006 Share Posted October 31, 2006 ans2[code]<?phpfunction MyCount($a) { if (is_array($a)) { $out = 0; foreach ($a as $b) $out += MyCount($b); return $out; } else return 1;}$a = array ( array (1,2,3,4,5, array(1,5,9) ), array (6,7,8), array (9,0));echo MyCount($a);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117508 Share on other sites More sharing options...
mainewoods Posted November 1, 2006 Share Posted November 1, 2006 anser2[code]<?php //get the number of elements in X in $array[0][X]; $numelements = count($array[0]); //should do it?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117643 Share on other sites More sharing options...
mainewoods Posted November 1, 2006 Share Posted November 1, 2006 answer1it should auto unset itself after the php page finishes execution so you'll probably have no gain by unsetting it manually. Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117645 Share on other sites More sharing options...
dagnasty Posted November 1, 2006 Author Share Posted November 1, 2006 Thanks guys. Your information was very helpful.The script is a VERY VERY VERY resource intensive one.'nother quick question:Does doing an unset on an array or a multidimensional array completely unset it? Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117782 Share on other sites More sharing options...
joshi_v Posted November 1, 2006 Share Posted November 1, 2006 It will depends on your code. see the examples.[code]<?php// destroy a complete array.unset ($foo);// destroy a single element of an arrayunset ($bar['quux']);// destroy more than one variableunset ($foo1, $foo2, $foo3);?>[/code]Regards,Joshi. Quote Link to comment https://forums.phpfreaks.com/topic/25734-php-efficiency-and-multidimension-array-count/#findComment-117801 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.