Jump to content

PHP efficiency and multidimension array count


dagnasty

Recommended Posts

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.
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

ans#1

actually 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.
Link to comment
Share on other sites

ans2[code]<?php
function 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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.