小编Mic*_*und的帖子

PHP按字段排序数组?

可能重复:
如何通过内键对多维数组
进行排序如何在php中对数组数组进行排序?

如何对数组进行排序: $array[$i]['title'];

数组结构可能如下:

array[0](
'id' => 321,
'title' => 'Some Title',
'slug' => 'some-title',
'author' => 'Some Author',
'author_slug' => 'some-author');

array[1](
'id' => 123,
'title' => 'Another Title',
'slug' => 'another-title',
'author' => 'Another Author',
'author_slug' => 'another-author');
Run Code Online (Sandbox Code Playgroud)

那么数据是否基于数组中的标题字段ASC顺序显示?

php arrays sorting

36
推荐指数
1
解决办法
8万
查看次数

WordPress delete_option(); 通配符功能?

如何删除以特定前缀开头的WordPress数据库中的所有选项名称?

我假设我们需要指定一个前缀,获取以该前缀开头的所有选项,然后删除找到的每个选项.

以下是用于获取和删除数据库中的选项的前缀和WP函数的示例.

<?php
$prefix = 'cpt_';
$getOpt = get_option($prefix);
foreach($getOpt as $toDelete){
    $deleteOpt = delete_option($prefix);
    if(!$deleteOpt){
        echo 'Failure.';
    }
    if($deleteOpt){
        echo 'Success.';
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

资源:

php wordpress wildcard prefix

4
推荐指数
1
解决办法
1508
查看次数

如何使用PHP获取午夜时间直到午夜

场景:已将记录输入数据库.

我想弄清楚以下等式:

  1. 如何获得自添加记录以来的小时数.
  2. 如何记录自添加记录以来剩下多少小时.

鉴于这些时间:

  • 日期/时间:2012-08-22 20:11:20
  • 时间戳:1345684280
  • 午夜今夜:2012-08-23 00:00:00
  • 午夜时间戳:1345698000

我觉得我走在正确的轨道上.只需要一些适当的数学来进行计算?我数学很糟糕.任何帮助或指导将不胜感激.我不是在寻找有人为我完成这件事.只是寻找关于我做错的建议,或者我如何做得更好.也许解释实现我的目标所必需的数学公式.

这是我到目前为止:

class tools{

    public function __construct(){
    }

    public function check_time($time, $request){
        $time = strtotime($time);
        if($request == 'since'){
            $theTime = time() - $time;
            $prefix = 'Since:';
        } elseif($request == 'until'){
            $midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
            $theTime = $midnight - $time;
            $prefix = 'Until:'; 
        }

        $tokens = array (
            31536000 => 'year',
            2592000 => 'month',
            604800 => 'week',
            86400 => 'day',
            3600 => 'hour',
            60 => …
Run Code Online (Sandbox Code Playgroud)

php math datetime timestamp equation

4
推荐指数
1
解决办法
6844
查看次数

将对象转换为数组

我正在使用WordPress,因为我不相信它可以对对象细节进行排序,我想知道如何将我的对象转换为数组,以便排序是可能的.

任何帮助或指导将不胜感激.

我正在使用WP函数get_categories();

$ category的完整内容是:

$category->term_id
$category->name
$category->slug
$category->term_group
$category->term_taxonomy_id
$category->taxonomy
$category->description
$category->parent
$category->count
$category->cat_ID
$category->category_count
$category->category_description
$category->cat_name
$category->category_nicename
$category->category_parent
Run Code Online (Sandbox Code Playgroud)

php arrays oop wordpress

3
推荐指数
3
解决办法
9568
查看次数

解析HTML并获取所有h3之后的h2之前的下一个h2使用PHP

我期待在文章中找到第一个h2.一旦找到,找到所有h3,直到找到下一个h2.冲洗并重复,直到找到所有标题和副标题.

在您立即将此问题标记或关闭为重复解析问题之前,请注意问题标题,因为这与基本节点检索无关.我已经把那部分搞定了.

我正在使用DOMDocument解析HTML DOMDocument::loadHTML(), DOMDocument::getElementsByTagName()DOMDocument::saveHTML()检索文章的重要标题.

我的代码如下:

$matches = array();
$dom = new DOMDocument;
$dom->loadHTML($content);
foreach($dom->getElementsByTagName('h2') as $node) {
    $matches['heading-two'][] = $dom->saveHtml($node);
}
foreach($dom->getElementsByTagName('h3') as $node) {
    $matches['heading-three'][] = $dom->saveHtml($node);
}
if($matches){
    $this->key_points = $matches;
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个类似的输出:

array(
    'heading-two' => array(
        '<h2>Here is the first heading two</h2>',
        '<h2>Here is the SECOND heading two</h2>'
    ),
    'heading-three' => array(
        '<h3>Here is the first h3</h3>',
        '<h3>Here is the second h3</h3>',
        '<h3>Here is the third h3</h3>',
        '<h3>Here …
Run Code Online (Sandbox Code Playgroud)

php parsing dom html-parsing domdocument

3
推荐指数
1
解决办法
2327
查看次数