【问题标题】:How to preg_match inside array / arraylist如何在数组/数组列表中进行 preg_match
【发布时间】:2019-02-02 13:32:52
【问题描述】:

我可以使用 pregmatch 获取 arraylist 中的数组值吗? 我有这样的数组

Array(
    [0] => Array(
        [judul] => extrahorror[data] => Array(
            [url] => url - horror[embed] => embed - horror
        )
    ) [1] => Array(
        [judul] => extraadventure[data] => Array(
            [url] => url - adventure[embed] => embed - adventure
        )
    ) [2] => Array(
        [judul] => music[data] => Array(
            [url] => url - music[embed] => embed - music
        )
    )
)

我希望 preg_match 的标题包含“额外”并获取 data->urldata->embed

我正在浏览第 1 个堆栈历史记录,如果有人能找到,请告诉我或回答这个问题

【问题讨论】:

  • 您的确切预期输出是多少?
  • array([0] =>array([title]=>{filtered title} [url]=>url [embed]=>embed )) 有可能变成这样吗?跨度>
  • 你能显示你的php代码@HandriandProExcalibur
  • 我从 google api v2 得到了这个数组,然后我用这个代码解析它 foreach($all_files->items as $files){ $links[] = array('title' => $files- >title, 'data'=>array('url' => $files->selfLink, 'embed' => $files->embedLink)); } print_r($links);不,我想在解析pastebin.com/xZL59Pjx之后获取特定标题包含一些用于过滤此数据的单词,并且在我解析它之前使用它pastebin.com/fnf2vztT

标签: php arrays arraylist


【解决方案1】:

一个循环就可以搞定

$res = [];
foreach($arr as $x) {
    // if a title contains 'extra'
    if( preg_match('~extra~', $x['judul'])) {
        // collect to an inner array
        $x['data']['judul'] = $x['judul'];
        // add to the result array
        $res[] = $x['data'];
    }
}

print_r($res);

demo

【讨论】:

  • 我认为它不是我的数组,因为我的数组像这样pastebin.com/xZL59Pjx
  • 现在试试,如果不行,把你的数组放在 var_export()
  • O_o 它的工作就像一个魔术 XD ty 先生,你能解释一下 var_export() 的用法吗?
  • 我做的唯一改变——用judul替换title :)))
  • 我如何才能在数组结果而不是 url 上制作 judul?不要改变它的主题。让它再次成为标题,否则我再次通过堆栈获得 -rep。
猜你喜欢
  • 2021-05-18
  • 2011-12-09
  • 1970-01-01
  • 2021-02-11
  • 2016-12-05
  • 2011-07-02
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多