diff --git a/admin/write-post.php b/admin/write-post.php index f84f6ca8..306ef1a7 100644 --- a/admin/write-post.php +++ b/admin/write-post.php @@ -86,7 +86,7 @@ Typecho_Widget::widget('Widget_Contents_Post_Edit')->to($post);
- * 2, 'banana' => 3), array('apple' => 10, 'banana' => 12));
- * $banana = Typecho_Common::arrayFlatten($fruit, 'banana');
- * print_r($banana);
- * //outputs: array(0 => 3, 1 => 12);
- * ?>
- *
- *
- * @access public
- *
- * @param array $value 被处理的数组
- * @param string $key 需要抽取的键值
+ * @param array $value
+ * @param $key
+ * @deprecated use array_column instead
*
* @return array
*/
- public static function arrayFlatten(array $value, $key)
+ public static function arrayFlatten(array $value, $key): array
{
- $result = [];
-
- if ($value) {
- foreach ($value as $inval) {
- if (is_array($inval) && isset($inval[$key])) {
- $result[] = $inval[$key];
- } else {
- break;
- }
- }
- }
-
- return $result;
+ return array_column($value, $key);
}
/**
diff --git a/var/Widget/Abstract/Contents.php b/var/Widget/Abstract/Contents.php
index 46ed6ac7..295041e4 100644
--- a/var/Widget/Abstract/Contents.php
+++ b/var/Widget/Abstract/Contents.php
@@ -218,7 +218,7 @@ class Widget_Abstract_Contents extends Widget_Abstract
*/
public function applyFields(array $fields, $cid)
{
- $exists = array_flip(Typecho_Common::arrayFlatten($this->db->fetchAll($this->db->select('name')
+ $exists = array_flip(array_column($this->db->fetchAll($this->db->select('name')
->from('table.fields')->where('cid = ?', $cid)), 'name'));
foreach ($fields as $name => $value) {
diff --git a/var/Widget/Abstract/Metas.php b/var/Widget/Abstract/Metas.php
index 41beca7c..212d6422 100644
--- a/var/Widget/Abstract/Metas.php
+++ b/var/Widget/Abstract/Metas.php
@@ -132,13 +132,13 @@ class Widget_Abstract_Metas extends Widget_Abstract
*/
public function merge($mid, $type, array $metas)
{
- $contents = Typecho_Common::arrayFlatten($this->db->fetchAll($this->select('cid')
+ $contents = array_column($this->db->fetchAll($this->select('cid')
->from('table.relationships')
->where('mid = ?', $mid)), 'cid');
foreach ($metas as $meta) {
if ($mid != $meta) {
- $existsContents = Typecho_Common::arrayFlatten($this->db->fetchAll($this->db
+ $existsContents = array_column($this->db->fetchAll($this->db
->select('cid')->from('table.relationships')
->where('mid = ?', $meta)), 'cid');
@@ -250,7 +250,7 @@ class Widget_Abstract_Metas extends Widget_Abstract
public function clearTags()
{
// 取出count为0的标签
- $tags = Typecho_Common::arrayFlatten($this->db->fetchAll($this->db->select('mid')
+ $tags = array_column($this->db->fetchAll($this->db->select('mid')
->from('table.metas')->where('type = ? AND count = ?', 'tags', 0)), 'mid');
foreach ($tags as $tag) {
diff --git a/var/Widget/Archive.php b/var/Widget/Archive.php
index 3bdc750d..4185b4f9 100644
--- a/var/Widget/Archive.php
+++ b/var/Widget/Archive.php
@@ -1748,7 +1748,7 @@ class Widget_Archive extends Widget_Abstract_Contents
$this->_archiveTitle = $this->title;
/** 设置关键词 */
- $this->_keywords = implode(',', Typecho_Common::arrayFlatten($this->tags, 'name'));
+ $this->_keywords = implode(',', array_column($this->tags, 'name'));
/** 设置描述 */
$this->_description = $this->description;
diff --git a/var/Widget/Contents/Attachment/Edit.php b/var/Widget/Contents/Attachment/Edit.php
index cd874729..ef614c05 100644
--- a/var/Widget/Contents/Attachment/Edit.php
+++ b/var/Widget/Contents/Attachment/Edit.php
@@ -269,7 +269,7 @@ class Widget_Contents_Attachment_Edit extends Widget_Contents_Post_Edit implemen
$deleteCount = 0;
do {
- $posts = Typecho_Common::arrayFlatten($this->db->fetchAll($this->select('cid')
+ $posts = array_column($this->db->fetchAll($this->select('cid')
->from('table.contents')
->where('type = ? AND parent = ?', 'attachment', 0)
->page($page, 100)), 'cid');
diff --git a/var/Widget/Contents/Post/Edit.php b/var/Widget/Contents/Post/Edit.php
index eab7f243..867e730f 100644
--- a/var/Widget/Contents/Post/Edit.php
+++ b/var/Widget/Contents/Post/Edit.php
@@ -473,7 +473,7 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
$categories = array_unique(array_map('trim', $categories));
/** 取出已有category */
- $existCategories = Typecho_Common::arrayFlatten($this->db->fetchAll(
+ $existCategories = array_column($this->db->fetchAll(
$this->db->select('table.metas.mid')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
@@ -538,7 +538,7 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
$tags = array_filter($tags, ['Typecho_Validate', 'xssCheck']);
/** 取出已有tag */
- $existTags = Typecho_Common::arrayFlatten($this->db->fetchAll(
+ $existTags = array_column($this->db->fetchAll(
$this->db->select('table.metas.mid')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
diff --git a/var/Widget/Contents/Related.php b/var/Widget/Contents/Related.php
index 1f21f950..24c78d87 100644
--- a/var/Widget/Contents/Related.php
+++ b/var/Widget/Contents/Related.php
@@ -32,7 +32,7 @@ class Widget_Contents_Related extends Widget_Abstract_Contents
$this->parameter->setDefault('limit=5');
if ($this->parameter->tags) {
- $tagsGroup = implode(',', Typecho_Common::arrayFlatten($this->parameter->tags, 'mid'));
+ $tagsGroup = implode(',', array_column($this->parameter->tags, 'mid'));
$this->db->fetchAll($this->select()
->join('table.relationships', 'table.contents.cid = table.relationships.cid')
->where('table.relationships.mid IN (' . $tagsGroup . ')')
diff --git a/var/Widget/Metas/Category/Admin.php b/var/Widget/Metas/Category/Admin.php
index 367617db..170e133f 100644
--- a/var/Widget/Metas/Category/Admin.php
+++ b/var/Widget/Metas/Category/Admin.php
@@ -22,7 +22,7 @@ class Widget_Metas_Category_Admin extends Widget_Metas_Category_List
$select = $this->db->select('mid')->from('table.metas')->where('type = ?', 'category');
$select->where('parent = ?', $this->request->parent ? $this->request->parent : 0);
- $this->stack = $this->getCategories(Typecho_Common::arrayFlatten(
+ $this->stack = $this->getCategories(array_column(
$this->db->fetchAll($select->order('table.metas.order', Typecho_Db::SORT_ASC)), 'mid'));
}
diff --git a/var/Widget/XmlRpc.php b/var/Widget/XmlRpc.php
index 90cf8407..a1359969 100644
--- a/var/Widget/XmlRpc.php
+++ b/var/Widget/XmlRpc.php
@@ -1513,8 +1513,8 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
/** 对文章内容做截取处理,以获得description和text_more*/
[$excerpt, $more] = $this->getPostExtended($post);
/** 只需要分类的name*/
- $categories = Typecho_Common::arrayFlatten($post->categories, 'name');
- $tags = Typecho_Common::arrayFlatten($post->tags, 'name');
+ $categories = array_column($post->categories, 'name');
+ $tags = array_column($post->tags, 'name');
$postStruct = [
'dateCreated' => new IXR_Date($this->options->timezone + $post->created),
@@ -1570,8 +1570,8 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
/** 只需要分类的name*/
/** 可以用flatten函数处理 */
- $categories = Typecho_Common::arrayFlatten($posts->categories, 'name');
- $tags = Typecho_Common::arrayFlatten($posts->tags, 'name');
+ $categories = array_column($posts->categories, 'name');
+ $tags = array_column($posts->tags, 'name');
$postStructs[] = [
'dateCreated' => new IXR_Date($this->options->timezone + $posts->created),
@@ -1804,7 +1804,7 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
return new IXR_Error($e->getCode(), $e->getMessage());
}
- $post->setCategories($postId, Typecho_Common::arrayFlatten($categories, 'categoryId'),
+ $post->setCategories($postId, array_column($categories, 'categoryId'),
'publish' == $post->status);
return true;
}
@@ -1916,7 +1916,7 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
return new IXR_Error($e->getCode(), $e->getMessage());
}
- $categories = Typecho_Common::arrayFlatten($post->categories, 'name');
+ $categories = array_column($post->categories, 'name');
$content = '