dimensional Array by Value

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be. Array ( [0] => Array ( [hashtag] => a7e87329b5eab8578f4f1098a152d6f4 [title] => Flower [order] => 3 ) [1] => Array ( [hashtag] => b24ce0cd392a5b0b8dedc66c2

按值排列二维数组

我怎样才能排序这个数组的“订单”键的值? 尽管这些值目前是连续的,但并不总是如此。 Array ( [0] => Array ( [hashtag] => a7e87329b5eab8578f4f1098a152d6f4 [title] => Flower [order] => 3 ) [1] => Array ( [hashtag] => b24ce0cd392a5b0b8dedc66c25213594 [title] => Free [order] =>

Get first key in a (possibly) associative array?

What's the best way to determine the first key in a possibly associative array? My first thought it to just foreach the array and then immediately breaking it, like this: foreach ($an_array as $key => $val) break; Thus having $key contain the first key, but this seems inefficient. Does anyone have a better solution? You can use reset and key : reset($array); $first_key = key($array)

在(可能)关联数组中获取第一个键?

确定可能的关联数组中的第一个键的最好方法是什么? 我首先想到的仅仅是对数组进行foreach,然后立即将其分解,如下所示: foreach ($an_array as $key => $val) break; 因此让$ key包含第一个键,但这看起来效率低下。 有没有人有更好的解决方案? 您可以使用reset和key : reset($array); $first_key = key($array); 它基本上和你最初的代码一样,但是开销少一点,而且更明显的是发生了什么。 只需记住调用reset

How do you remove an array element in a foreach loop?

I want to loop through an array with foreach to check if a value exists. If the value does exist, I want to delete the element which contains it. I have the following code: foreach($display_related_tags as $tag_name) { if($tag_name == $found_tag['name']) { // Delete element } } I don't know how to delete the element once the value is found. How do I delete it? I have t

你如何在foreach循环中移除一个数组元素?

我想用foreach循环访问一个数组来检查值是否存在。 如果该值确实存在,我想删除包含它的元素。 我有以下代码: foreach($display_related_tags as $tag_name) { if($tag_name == $found_tag['name']) { // Delete element } } 一旦找到值,我不知道如何删除元素。 我如何删除它? 我必须使用foreach来解决这个问题。 有可能是foreach替代品,欢迎您分享。 如果您还获得了密钥,则可以像这样删除该项目

Best way to parse an invalid HTML in PHP

Is there a better approach to parse an invalid HTML then applying Tidy on it? Side Note : There are some situation when you can't have Tidy available. Regexp is also not recommended I understood for parsing html. I would try something like this: http://php.net/manual/en/domdocument.loadhtml.php From that page: The function parses the HTML contained in the string source. Unlike loadi

在PHP中解析无效HTML的最佳方法

有没有更好的方法来解析无效的HTML,然后在其上应用Tidy? 注意:有些情况下,您无法使用Tidy。 正则表达式也不推荐我理解为解析HTML。 我会尝试这样的:http://php.net/manual/en/domdocument.loadhtml.php 从该页面: 该函数分析字符串源中包含的HTML。 与加载XML不同, HTML不需要加载格式良好的HTML 。 这个函数也可以静态调用来加载和创建一个DOMDocument对象。 已知SimpleHTMLDOM比PHP的本地DOM功能更宽松。

PHP/Regex: simple regex for bbcode [s] or [strike] fails to work

For a silly bbcode parser I wanted to add two definitions into one, my original definition was this for preg_replace: '#[s](.*?)[/s]#si', '<strike>\1</strike>' And this works, I wished for the user to be able to use either [s] or [strike] to initiate text in that format, so I naturally added something like this thinking it would work: '#[(s|strike)](.*?)[/(s|strike)]#si', '<stri

PHP / Regex:bbcode [s]或[strike]的简单正则表达式无法正常工作

对于一个愚蠢的bbcode解析器,我想将两个定义添加到一个中,我的原始定义是preg_replace: '#[s](.*?)[/s]#si', '<strike>\1</strike>' 而且这个工作,我希望用户能够使用[s] [strike]以这种格式发起文本,所以我自然而然地添加了这样的东西,认为它会起作用: '#[(s|strike)](.*?)[/(s|strike)]#si', '<strike>\1</strike>' 不幸的是,这并不是你所期望的,而是[s]和[strike] (正确使用)都是: s

style </ in <script> tag

Update : html5lib (bottom of question) seems to get close, I just need to improve my understanding of how it's used. I am attempting to find an HTML5-compatible DOM parser for PHP 5.3. In particular, I need to access the following HTML-like CDATA within a script tag: <script type="text/x-jquery-tmpl" id="foo"> <table><tr><td>${name}</td></tr></ta

样式</ in <script>标签

更新 : html5lib (问题的底部)似乎接近,我只需要提高我对它的使用方式的理解。 我正在尝试为PHP 5.3找到一个兼容HTML5的DOM解析器。 特别是,我需要在脚本标记中访问以下类似HTML的CDATA: <script type="text/x-jquery-tmpl" id="foo"> <table><tr><td>${name}</td></tr></table> </script> 大多数解析器会过早结束解析,因为当HTML 4.01在<script>标记内

A simple program to CRUD node and node values of xml file

I decided to save setting in xml file since field takes only one value. My XML file is like <?xml version="1.0" encoding="UTF-8"?> <setting> <setting1>setting1 value</setting1> <setting2>setting2 value</setting2> <setting3>setting3 value</setting3> .... .... .... </setting> Can anyone suggest me a simple php scrip

一个简单的程序来CRUD节点和xml文件的节点值

我决定将设置保存在xml文件中,因为字段只有一个值。 我的XML文件就像 <?xml version="1.0" encoding="UTF-8"?> <setting> <setting1>setting1 value</setting1> <setting2>setting2 value</setting2> <setting3>setting3 value</setting3> .... .... .... </setting> 任何人都可以建议我一个简单的PHP脚本来读取,编辑,添加和删除节点和

getting all values from h1 tags using php

I want to receive an array that contains all the h1 tag values from a text Example, if this where the given input string: <h1>hello</h1> <p>random text</p> <h1>title number two!</h1> I need to receive an array containing this: titles[0] = 'hello', titles[1] = 'title number two!' I already figured out how to get the first h1 value of the string but I need

使用php获取来自h1标签的所有值

我想要接收包含文本中所有h1标签值的数组 例如,如果这里给定的输入字符串: <h1>hello</h1> <p>random text</p> <h1>title number two!</h1> 我需要收到一个包含这个数组的数组: titles[0] = 'hello', titles[1] = 'title number two!' 我已经想出了如何获得字符串的第一个h1值,但我需要给定字符串中所有h1标签的所有值。 我目前正在使用它来接收第一个标签: function getTextB

DOMDocument in php

I have just started reading documentation and examples about DOM, in order to crawl and parse the document. For example I have part of document shown below: <div id="showContent"> <table> <tr> <td> Crap </td> </tr> <tr> <td width="172" valign="top"><a href="link"><img height="91" border=

DOM中的DOMDocument

我刚开始阅读有关DOM的文档和示例,以便抓取和解析文档。 例如,我有如下所示的文档的一部分: <div id="showContent"> <table> <tr> <td> Crap </td> </tr> <tr> <td width="172" valign="top"><a href="link"><img height="91" border="0" width="172" class="" src="img"></a></td>

Grabbing the href attribute of an A element

Trying to find the links on a page. my regex is: /<as[^>]*href=("'??)([^"' >]*?)[^>]*>(.*)</a>/ but seems to fail at <a title="this" href="that">what?</a> How would I change my regex to deal with href not placed first in the a tag? Reliable Regex for HTML are difficult. Here is how to do it with DOM: $dom = new DOMDocument; $dom->loadHTML($html); fore

抓取A元素的href属性

试图找到页面上的链接。 我的正则表达式是: /<as[^>]*href=("'??)([^"' >]*?)[^>]*>(.*)</a>/ 但似乎失败了 <a title="this" href="that">what?</a> 我如何改变我的正则表达式来处理不是首先放在标签中的href? HTML的可靠正则表达式很困难。 以下是如何使用DOM做到这一点: $dom = new DOMDocument; $dom->loadHTML($html); foreach ($dom->getElementsByTagName('a') as $no