Php 我怎么能不';在本页中是否不使用全局变量?
在php文档中,我创建了这个函数Php 我怎么能不';在本页中是否不使用全局变量?,php,Php,在php文档中,我创建了这个函数 function getPrices($url) { global $priceList; // declare global . point of this. $src = file_get_contents_curl($url); $dom = new DOMDocument(); $selector = new DOMXPath($dom); $results = $selector->query('//t
function getPrices($url) {
global $priceList; // declare global . point of this.
$src = file_get_contents_curl($url);
$dom = new DOMDocument();
$selector = new DOMXPath($dom);
$results = $selector->query('//table/tr/td/span');
foreach($results as $node) {
array_push($priceList, $node->nodeValue);
}
}
在这一页的底部,我称之为“几个”
$priceList = array();
getPrices("http://finance.naver.com/item/sise_day.nhn?code=005930");
getPrices("http://finance.naver.com/item/sise_day.nhn?code=005930&page=2");
getPrices("http://finance.naver.com/item/sise_day.nhn?code=005930&page=3");
并展示它
echo $priceList[1];
echo $priceList[2];
echo $priceList[3];
问题是我使用的是CMS类型的Joomla、Wordpress,它们不支持使用全局变量,因此我不知道如何在不使用全局变量的情况下实现这一点。我怎样才能做到?我需要很多页来报废,所以我很害怕。如果我只删掉一页
返回功能,
及
但我不知道有多少报废案例。请帮助我…您可以从函数返回部分结果,并将它们放入完整的结果数组中
<?php
$result = [];
$result = array_merge($result, getSomeValues());
$result = array_merge($result, getSomeValues());
$result = array_merge($result, getSomeValues());
var_export($result);
function getSomeValues() {
static $i = 0;
// returning a partial result of three elements
return [ $i++, $i++, $i++ ];
}
一般来说,无论如何都不应该使用全局变量。这是个坏习惯。这里有一种方法可以重组它:
function getPrices($url) {
// this is just a local scoped temp var
$priceList = array();
$src = file_get_contents_curl($url);
$dom = new DOMDocument();
$selector = new DOMXPath($dom);
$results = $selector->query('//table/tr/td/span');
foreach($results as $node) {
array_push($priceList, $node->nodeValue);
}
// return the price list
return $priceList;
}
// here is your real price list
$priceList = array();
// array of urls
$urls = array(
"http://finance.naver.com/item/sise_day.nhn?code=005930",
"http://finance.naver.com/item/sise_day.nhn?code=005930&page=2",
"http://finance.naver.com/item/sise_day.nhn?code=005930&page=3"
// etc..
);
// loop through the urls and assign the results to the price list
foreach ($urls as $url) {
$priceList[] = getPrices($url);
}
现在,您可以使用
$priceList
作为一个数组来执行任何操作。或者,如果您希望立即输出。。您可以跳过将其放入$priceList
并在上面的循环中进行输出您可以将部分结果存储为结果数组的元素。这样,您就可以保留产生结果的“什么”的一些信息。
(您甚至可以使用url作为数组索引)
是否可以将其存储在会话中?为什么$i
声明为静态?我希望getSomeValue()返回[0,1,2]、[3,4,5]和[6,7,8]<代码>静态$i
是我所知道的实现这一点的最短方法;-)我会选择兰德():德兰德()也是我的第一选择。但我希望它可以复制;-)rand()使用固定的salt值?
array (
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
)
function getPrices($url) {
// this is just a local scoped temp var
$priceList = array();
$src = file_get_contents_curl($url);
$dom = new DOMDocument();
$selector = new DOMXPath($dom);
$results = $selector->query('//table/tr/td/span');
foreach($results as $node) {
array_push($priceList, $node->nodeValue);
}
// return the price list
return $priceList;
}
// here is your real price list
$priceList = array();
// array of urls
$urls = array(
"http://finance.naver.com/item/sise_day.nhn?code=005930",
"http://finance.naver.com/item/sise_day.nhn?code=005930&page=2",
"http://finance.naver.com/item/sise_day.nhn?code=005930&page=3"
// etc..
);
// loop through the urls and assign the results to the price list
foreach ($urls as $url) {
$priceList[] = getPrices($url);
}
<?php
$result = [];
$result[] = getSomeValues();
$result[] = getSomeValues();
$result[] = getSomeValues();
// now $result is an array of arrays of (some scalar value)
// so your iteration has to be changed
foreach( $results as $presult ) {
foreach( $presult as $element ) {
..do something with $element
}
}
// or you can "hide" the nesting with
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($result));
foreach($it as $e ) {
echo $e, ', ';
} // see http://docs.php.net/spl
function getSomeValues() {
static $i = 0;
return [ $i++, $i++, $i++ ];
}