【发布时间】:2014-04-17 15:09:40
【问题描述】:
我想使用函数array_push 来填充一个空数组,但是我得到一个错误,参数 1 应该是数组但给出了 null,这是我的代码:
public $docs = array("ant ant bee", "dog bee dog hog dog ant dog", "cat gnu eel fox");
public function terms(){
$temp = array();
$terms = array(array());
$docs = $this->docs;
for($i = 0; $i < sizeof($docs); $i++){
$temp[$i] = preg_split('/ /', $docs[$i], null, PREG_SPLIT_NO_EMPTY);
}
for($i = 0; $i < sizeof($temp); $i++){
for($j = 0; $j < sizeof($temp[$i]); $j++){
for($k = 0; $k < sizeof($temp[$i]); $k++){
if($temp[$i][$j] != $terms[$i][$k])
array_push($terms[$i], $temp[$i][$k]);
}
}
}
return $terms;
}
}
【问题讨论】:
-
你想要什么结果?这看起来过于复杂。
-
您希望每个新元素(以空格分隔)都位于新级别的数组中吗?你想要多少级?不能真正得到你想做什么xd
-
错误很明显,只要按照你的代码,你会看到
$terms[1]没有设置。 -
@aleation in
$temp我想在拆分后存储文档字符串,然后在$terms我想从每个子数组存储唯一的字符串,所以每个重复的字符串都应该存储一次对于每个子数组 -
@user3194430 你应该添加你想要的结果数组的样子。
标签: php arrays multidimensional-array push