如何使用PHP无键获取内部数组值

如何使用PHP无键获取内部数组值,php,Php,在我的php文件中,我得到的字符串看起来像[[“category_id”、“=”、“3”]、[“price”、“您可以使用内爆和一些循环 $filter = isset($_POST['filter']) ? $_POST['filter'] : ''; $arr = json_decode($filter,true); foreach($arr as $k => $v) { $query['q'][] = [ 'column' => 'product.' . $v[0]

在我的php文件中,我得到的字符串看起来像[[“category_id”、“=”、“3”]、[“price”、“您可以使用内爆和一些循环

$filter = isset($_POST['filter']) ? $_POST['filter'] : '';
$arr = json_decode($filter,true);

foreach($arr as $k => $v) {
  $query['q'][] = [
   'column' => 'product.' . $v[0],
   'equality' => $v[1],
   'value' => $v[2]
  ];
}

$filter = array();

foreach($query as $q) {
  foreach($q as $k) {

  $filter[]= sprintf("%s %s %s", $k['column'],$k['equality'],$k['value']);
    
  }
}

$query = implode(' AND ',$filter);

echo $query;
// product.category_id = 3 AND product.price < 40
$filter=isset($\u POST['filter'])?$\u POST['filter']:'';
$arr=json_decode($filter,true);
foreach($arr为$k=>$v){
$query['q'][]=[
'列'=>'产品'.$v[0],
“相等”=>$v[1],
'值'=>v$2]
];
}
$filter=array();
foreach($q查询){
foreach($q为$k){
$filter[]=sprintf(“%s%s%s”、$k['column']、$k['equality']、$k['value']);
}
}
$query=内爆('AND',$filter);
echo$query;
//product.category_id=3,product.price<40
警告:您对SQL注入非常开放,应该使用参数化的预处理语句,而不是手动生成查询。它们由PDO或MySQLi提供。永远不要信任任何类型的输入!即使您的查询仅由受信任的用户执行,您仍有损坏数据的风险。仅转义是不够的! -达曼


看起来您正在为SQL查询构造一个
WHERE
子句。下面是关于如何构建字符串的提示,但您应该使用预处理语句,而不是将用户输入直接注入到查询中!此外,您实际上不需要嵌套foreach,如下所示。有关解释,请参阅注释

要使用准备好的语句(请使用!),您可以将
ed条件中的变量替换为
“product.?”;
。然后使用。还可以使用其他类型的参数绑定

<?php
    
// Your input array.
$filters = [["category_id","=","3"],["price","<","40"]];

// Your question is unclear on this, but if your $_POST['filter'] is not actually an array, but a JSON string, then you may try this:
$filters = json_decode($_POST['filter'], TRUE); // TRUE to decode to associative array.

// $where holds each WHERE condition, so you can perform a clean string glue with implode().
// In your current string concatenation, additional conditions are required, or the query
// will fail due to an errant trailing AND if none are following.
$where = [];

// $filterText will hold this part of your WHERE clause. Consider renaming it to something
// more descriptive, like $whereSql.
$filterText = '';
foreach ($filters as [$key, $comparisonOperator, $value])
        $where[] = "product.$key $comparisonOperator $value";

// Glue each condition with as many ANDs are necessary. 
$filterText .= implode(' AND ', $where);

echo $filterText;
// Outputs: product.category_id = 3 AND product.price < 40

你能打印r($\u POST['filter'])并将输出添加到你的问题中吗?你从当前代码中得到了什么?有错误吗?你提到你从android中得到的是一个字符串,你是否尝试过验证
$filter
仍然不是一个字符串?还要注意你的引号:
'
不是
'
不是
字符串是JSON格式的,所以您只需要使用
JSON\u decode($filter,true)
将其转换为数组。另外,看起来您正在那里构建一个SQL查询,您应该使用准备好的语句来避免SQL注入,而不是将变量从不可信的源连接到其中:如果我手动分配$arr,则代码可以工作,但如果我通过post获得值,则代码无法工作并打印任何内容。我对PHP不太了解h、 所以,谢谢你的警告,我会研究它。谢谢你的回答。结果与Jerson答案相同。如果我手动分配$filters,代码会工作,但如果我用post代码获取值,则不会工作并打印任何内容。我不太懂PHP,我会研究准备好的语句。显示
var\u dump($\u post['filter'])
也许我可以更新我的答案,让它对你更有用。它不打印任何东西。但只打印$\u POST['filter']打印[[“category\u id”、“=”、“3”]、[“price”,“试试我的更新,看看是否有效。而
var\u dump()
应该总是会产生一些输出,您刚才的评论告诉我您可能正在处理JSON字符串。数据是用android的字符串发送的,但是您的更新工作正常。非常感谢
<?php
    
// Your input array.
$filters = [["category_id","=","3"],["price","<","40"]];

// Your question is unclear on this, but if your $_POST['filter'] is not actually an array, but a JSON string, then you may try this:
$filters = json_decode($_POST['filter'], TRUE); // TRUE to decode to associative array.

// $where holds each WHERE condition, so you can perform a clean string glue with implode().
// In your current string concatenation, additional conditions are required, or the query
// will fail due to an errant trailing AND if none are following.
$where = [];

// $filterText will hold this part of your WHERE clause. Consider renaming it to something
// more descriptive, like $whereSql.
$filterText = '';
foreach ($filters as [$key, $comparisonOperator, $value])
        $where[] = "product.$key $comparisonOperator $value";

// Glue each condition with as many ANDs are necessary. 
$filterText .= implode(' AND ', $where);

echo $filterText;
// Outputs: product.category_id = 3 AND product.price < 40