假设我有JSON中的数据,
3个人拥有不同的数据,例如New
/ New and Old
/ Old and New
。
我尝试使用isset($_GET['NoID']);
http://localhost/test.php?NoID=31331 // Stan Lee
http://localhost/test.php?NoID=31332 // Mary Jane
http://localhost/test.php?NoID=31335 // John Doe
结果是:
// Stan Lee
{
- Table: [
- {
Name: "Stan Lee",
NoID: "31331",
Type: "New",
- @attributes: {
id: "Table1",
rowOrder: "0",
}
},
]
}
// Mary Jane
{
- Table: [
- {
Name: "Mary Jane",
NoID: "31332",
Type: "New",
- @attributes: {
id: "Table1",
rowOrder: "0",
}
},
- {
Name: "Mary Jane",
NoID: "31333",
Type: "Old",
- @attributes: {
id: "Table2",
rowOrder: "1",
}
},
]
}
// John Doe
{
- Table: [
- {
Name: "John Doe",
NoID: "31334",
Type: "Old",
- @attributes: {
id: "Table1",
rowOrder: "0",
}
},
- {
Name: "John Doe",
NoID: "31335",
Type: "New",
- @attributes: {
id: "Table2",
rowOrder: "1",
}
},
]
}
我想返回条件仅为New
(单行)。
我尝试foreach()
然后严格声明New
和break
,同时我尝试了array_filter()
和sort()
。这些功能无法仅返回New
。
到目前为止,我尝试了我的代码:
foreach ($data['Table'] as $value) {
if (is_array($value) && $value['Type'] == "New") {
json($value);
break;
} elseif (is_string($value)) {
json($value);
break;
}
}
输出
Notice: Undefined index: Type
"New"
Mary Jane
& John Doe
给出正确的输出,但Stan Lee
无效。
任何人都可以帮助我吗?
谢谢!
答案 0 :(得分:1)
从break;
循环中删除foreach()
。
break
将中断循环,不再处理数组键/值。
修改强>
可能您不希望每次满足条件时都发送json
因此,在foreach()创建变量$output
并向其收集匹配值$output[] = $value;
之前。最后使用json($output);
<?php
$data = [
"Table" => [
"Type" => "New"
],
[
1 => ["Type" => "New"],
2 => ["Type" => "Old"],
],
[
1 => ["Type" => "Old"],
2 => ["Type" => "New"],
]
];
foreach ($data['Table'] as $key => $value) {
If($key == 'Type' and $value == 'New'){
$output[] = $value;
}
答案 1 :(得分:1)
也许这会对你有所帮助:
$newArray = array();
foreach($data as $key => $d){
if(sizeof($d) > 1){
foreach($d as $key1 => $d1){
if(strtolower($d1['Type']) == 'new'){
$newArray[$key1]['Type'] = $d1['Type'];
}
}
}else{
if(strtolower($d['Type']) == 'new'){
$newArray[$key]['Type'] = $d['Type'];
}
}
}
print_r(json_encode($newArray));
答案 2 :(得分:1)
嗨,兄弟,我检查了你的代码,我只是删除了中断试试这个:
$data = ["Table" => [[
"Name" => "Stan Lee",
"NoID" => "31331",
"Type" => "New",
"attributes" =>[
"id"=>"Table1",
"rowOrder"=> "0"
]
],
[
"Name" => "Mary Jane",
"NoID" => "31332",
"Type" => "New",
"attributes" =>[
"id"=>"Table1",
"rowOrder" => "0"
]
],
[
"Name" => "Mary Jane",
"NoID" => "31333",
"Type" => "Old",
"attributes" =>[
"id"=>"Table2",
"rowOrder" =>"1"
]
],
[
"Name" => "John Doe",
"NoID" => "31334",
"Type" => "Old",
"attributes" =>[
"id"=>"Table1",
"rowOrder"=>"0"
]
],
[
"Name" => "John Doe",
"NoID" => "31335",
"Type" => "New",
"attributes" =>[
"id"=>"Table2",
"rowOrder" => "1"
]
],
]
];
foreach ($data['Table'] as $value) {
if (is_array($value) && $value['Type'] == "New") {
echo '<pre>';
print_r($value);
echo '</pre>';
} elseif (is_string($value)) {
echo '<pre>';
print_r($value);
echo '</pre>';
break;
}
}
这是输出:
Array
(
[Name] => Stan Lee
[NoID] => 31331
[Type] => New
[attributes] => Array
(
[id] => Table1
[rowOrder] => 0
)
)
Array
(
[Name] => Mary Jane
[NoID] => 31332
[Type] => New
[attributes] => Array
(
[id] => Table1
[rowOrder] => 0
)
)
Array
(
[Name] => John Doe
[NoID] => 31335
[Type] => New
[attributes] => Array
(
[id] => Table2
[rowOrder] => 1
)
)
如果您需要,请告诉我。随便问。
答案 3 :(得分:0)
解决方案:
function arraySearch($array,$keyword="type",$value="New",$results=[]) {
foreach ( $array as $key => $val ) {
if(is_array($val)){
if(array_key_exists($keyword,$val)){
if ($val[$keyword] === $value ) {
$results[] = $key;
}
}else{
arraySearch($array,$keyword,$value,$results);
}
}
}
return json($results);
}
致电功能:
echo arraySearch($data['Table']);
答案 4 :(得分:0)
使用array_filter
这样的demo
array_filter($data, function($v){
return count($v) == 1 && $v['Type'] == 'New';
})
答案 5 :(得分:0)
结果:[{"0":"New","1":"New","4":"New"}]
<?php
$data = ["Table" => [[
"Name" => "Stan Lee",
"NoID" => "31331",
"Type" => "New",
"attributes" =>[
"id"=>"Table1",
"rowOrder"=> "0"
]
],
[
"Name" => "Mary Jane",
"NoID" => "31332",
"Type" => "New",
"attributes" =>[
"id"=>"Table1",
"rowOrder" => "0"
]
],
[
"Name" => "Mary Jane",
"NoID" => "31333",
"Type" => "Old",
"attributes" =>[
"id"=>"Table2",
"rowOrder" =>"1"
]
],
[
"Name" => "John Doe",
"NoID" => "31334",
"Type" => "Old",
"attributes" =>[
"id"=>"Table1",
"rowOrder"=>"0"
]
],
[
"Name" => "John Doe",
"NoID" => "31335",
"Type" => "New",
"attributes" =>[
"id"=>"Table2",
"rowOrder" => "1"
]
],
]
];
$build = [];
$output = [];
$i= 0;
foreach ($data as $value) {
foreach ( $value as $key => $val) {
if(!is_array($val) AND $val== "New"){
$build[$i][0] = $val;
$output = $build;
}else if(is_array($val) AND $val["Type"] === "New"){
$build[$i][$key] = "New";
$output = $build;
}
}
$i++;
}
print_r(json_encode($output));
?>
答案 6 :(得分:0)
<?php
foreach ($data['Table'] as $value) {
foreach ($value as $data) {
if(in_array('New', $data)) {
//here you can code
echo "<pre>";
print_r($data);
}
}
}