Access PHP array element with a function?

I'm working on a program that uses PHP's internal array pointers to iterate along a multidimensional array. I need to get an element from the current row, and I've been doing it like so: $arr[key($arr)]['item'] However, I'd much prefer to use something like: current($arr)['item'] // invalid syntax I'm hoping there's a function out there that I've missed in my scan

使用函数访问PHP数组元素?

我正在研究一个使用PHP的内部数组指针沿着多维数组迭代的程序。 我需要从当前行获取一个元素,并且我一直这样做: $arr[key($arr)]['item'] 不过,我更愿意使用类似于以下内容的东西: current($arr)['item'] // invalid syntax 我希望有一个功能在那里,我已经错过了我的文档扫描,使我能够访问元素如下: getvalue(current($arr), 'item') 要么 current($arr)->getvalue('item') 有什么建议么? 我非常怀疑是否

Is it possible to reference a specific element of an anonymous array in PHP?

This is probably a simple question, and I'm afraid the answer might be "no", but... Here's a simple piece of code: function func1() { $bt = debug_backtrace(); print "Previous function was " . $bt[1]['function'] . "n"; } Now... Can this be done without the temporary variable? In another language, I might expect to be able to say: function func1() { print "Previous fun

有可能在PHP中引用匿名数组的特定元素吗?

这可能是一个简单的问题,恐怕答案可能是“不”,但是...... 这是一段简单的代码: function func1() { $bt = debug_backtrace(); print "Previous function was " . $bt[1]['function'] . "n"; } 现在......这可以在没有临时变量的情况下完成吗? 用另一种语言,我可能期望能够说: function func1() { print "Previous function was " . (debug_backtrace())[1]['function'] . "n"; } 唉,在PHP中,这导致了一个错

Calling PDO::setAttribute within namespaces

I want to use the built-in PDO class to connect to a mysql database. If I understand it right, the PDO methods don't throw exceptions by default. Is this correct? So I try to change the error mode: $pdo = new PDO($dsn, $user, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); My problem is that I'm using namespaces in my classes so I have to write: $pd

在名称空间中调用PDO :: setAttribute

我想使用内置的PDO类连接到一个mysql数据库。 如果我理解正确,PDO方法默认不会抛出异常。 它是否正确? 所以我尝试改变错误模式: $pdo = new PDO($dsn, $user, $password); $ pdo-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); 我的问题是我在我的类中使用命名空间,所以我必须写: $pdo = new PDO($dsn, $user, $password); 但我不知道如何在这种情况下设置错误模式。 $pdo = new PDO(

Mysql PDO execute always return TRUE

Mysql PDO execute always return true, even if i do a wrong query: try { $sql = $this->db->prepare("INSERT INTO orders (total_price) VALUES ('not_int');"); $result = $sql->execute(); // WHY TRUE?? } catch (Exception $e) { die("Oh noes! There's an error in the query!"); } In table, type of column 'total_price' is INT. Exception also not catch (PDO::ATTR_ERRMODE is P

Mysql PDO执行始终返回TRUE

Mysql PDO执行始终返回true,即使我做了错误的查询: try { $sql = $this->db->prepare("INSERT INTO orders (total_price) VALUES ('not_int');"); $result = $sql->execute(); // WHY TRUE?? } catch (Exception $e) { die("Oh noes! There's an error in the query!"); } 在表中,'total_price'列的类型是INT。 异常也没有捕获(PDO :: ATTR_ERRMODE是PDO :: ERRMODE_EXCEPTION)。 在'

can't catch PDOException when my query has error, why?

I'm using CodeIgniter and I have the following code to fetch all records from database: try { $sth = $this->db->conn_id->prepare($query); $sth->bindParam(':something', $something, PDO::PARAM_INT); $sth->execute(); //... } catch (PDOException $e) { //error goes here } I made a mistake on $query which I put LIMIT right before OR

当我的查询有错误时无法捕获PDOException,为什么?

我使用的是CodeIgniter,我有以下代码从数据库中获取所有记录: try { $sth = $this->db->conn_id->prepare($query); $sth->bindParam(':something', $something, PDO::PARAM_INT); $sth->execute(); //... } catch (PDOException $e) { //error goes here } 我在$ ORDER BY之前放置了LIMIT的$查询时犯了一个错误。 即: SELECT `something` from some_tab

Query Failed: Drop down menu in php web page

Below is a form for adding a Book to a table called books. I currently have a drop down menu that allows a user to pick an existing module however when I submit the form the below error appears... "Query failed: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 " <form action="<?php echo $_SERVER['PHP_SE

查询失败:在php网页下拉菜单

下面是一个表格,用于将Book添加到名为books的表格中。 我目前有一个下拉菜单,允许用户选择一个现有的模块,但是当我提交表单时出现以下错误... “查询失败:SQLSTATE [21S01]:插入值列表与列表列表不匹配:1136列计数与第1行的值计数不匹配” <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> Book Code: <input type="text" name="fieldOne" /> Book Title: <

Syntax error or access violation using PDO

I'm migrating my local files online and having PHP issues with my db script. Works fine locally (running PHP 5.3.8) but gives me the following errors on my server (PHP 5.3.10) Without $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); Fatal error: Call to a member function setFetchMode() on a non-object in /.../...php on line 108 With $DBH->setAttribute( PDO::ATTR_E

使用PDO的语法错误或访问冲突

我在线迁移我的本地文件,并在我的db脚本中出现PHP问题。 在本地工作正常(运行PHP 5.3.8),但在我的服务器上出现以下错误(PHP 5.3.10) 没有$ DBH-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); 致命错误:在第108行的/.../...php中的非对象上调用成员函数setFetchMode() 使用$ DBH-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); SQLSTATE [42000]:语法错误或访

WordPress to changing simple quotes for double quotes

CONTEXT. I work with WordPress 4.7.5 and shortcodes. GOAL. Put a link that displays information hidden as an accordion. The simple way to do this is to insert a shortcode Accordion type: [su_accordion] [su_spoiler title="My link"] <h1>Lorem ipsum dolor sit amet</h1> [/su_spoiler] [/su_accordion] This works perfectly. But my goal is slightly more complex: the li

WordPress更改双引号的简单引号

上下文。 我使用WordPress 4.7.5和简码。 目标。 放置一个显示隐藏为手风琴信息的链接。 这样做的简单方法是插入简码手风琴类型: [su_accordion] [su_spoiler title="My link"] <h1>Lorem ipsum dolor sit amet</h1> [/su_spoiler] [/su_accordion] 这工作完美。 但是我的目标稍微复杂一些:链接(我的链接)必须嵌入HTML标记中,例如表格: [su_accordion] [su_spoiler title="

php new hosting with double quote error

I have just change my hosting, before all my PHP scripts worked fine but now i get many mysql error like this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near column = 'value' it seems that there is a double quote in some script there is a way to resolve without update all my PHP scripts? EDIT: example

PHP新的托管与双引号错误

在我所有的PHP脚本都正常工作之前,我只是更换了我的主机 但现在我得到了很多像这样的mysql错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near column = 'value' 在某些剧本中似乎有一个双引号 有没有更新所有我的PHP脚本解决方法? 编辑:PHP代码的例子 function test( $table,$column, $where ){ if( get_magic_quote

replace when not inside double quotes

Basically I want to replace certain words (eg the word "tree" with the word "pizza") in sentences. Restriction: When the word that should be replaced is between double quotes, the replace should not be performed. Example: The tree is green. -> REPLACE tree WITH pizza "The" tree is "green". -> REPLACE tree WITH pizza "The tree" is green. -> DONT REPLACE "The tree i

当不在双引号内时替换

基本上我想用句子替换某些单词(例如单词“树”与单词“pizza”)。 限制:当应该替换的单词在双引号之间时,不应执行替换。 例: The tree is green. -> REPLACE tree WITH pizza "The" tree is "green". -> REPLACE tree WITH pizza "The tree" is green. -> DONT REPLACE "The tree is" green. -> DONT REPLACE The ""tree is green. -> REPLACE tree WITH pizza 有没有可能用正则表达式来做到这一点? 我会