php class, passing variable into function, referencing the variable
I have a class which pulls in Twitter 开发者_开发百科feeds and amalgamates them, they are put into an array ,sorted and combined. I then need to convert the 'published' time from unix to human .
Within my class construct I have:
function __construct($inputs) {
$this->inputs = $inputs;
$this->mergeposts();
$this->sortbypublished($this->allPosts,'published');
$this->unixToHuman('problem here');
$this->output();
}
SortbyPublished is
function sortbypublished(&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
arsort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$this->sorted = $ret;
}
unixToHuman is :
public function unixToHuman($unixtime) {
$posts['published'] = date('Y-m-d H:i:s', $unixtime);
}
My problem is I cannot work out what I need to enter into :
$this->unixToHuman('HERE');
Part of this I believe is due to my lack of understanding of PHP terminology, which is making it hard to find anything in the manual . Am I trying to reference the 'published' array?
What I need is the correct version of :
$this->sorted['published']
I hope this makes sense , any help at all , especially with the terminology greatly appreciated.
It looks like unixToHuman wants a timestamp. So you could use date(), or the timestamp of whatever time you want to convert to human readable time.
$this->unixToHuman(date());
First off, the unixToHuman
method needs to return a value, so let's do this:
public function unixToHuman($post) {
$post['published'] = date('Y-m-d H:i:s', $post['published']);
return $post;
}
Then we can pass in our rows one at a time in your __construct
method:
foreach ($this->sorted AS $idx => $row) {
$this->sorted[$idx] = $this->unixToHuman($row);
}
精彩评论