Let say I have the datas in JSON,
3 people have different datas such as New
only / New and Old
/ Old and New
.
I tried using isset($_GET['NoID']);
which is
http://localhost/test.php?NoID=31331 // Stan Lee
http://localhost/test.php?NoID=31332 // Mary Jane
http://localhost/test.php?NoID=31335 // John Doe
And the result are:
// 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", } }, ] }
I want to return which is condition is New
only (Single row).
I tried to foreach()
then strict statement for New
and break
, also I tried array_filter()
and sort()
. Those function didn't work out to return New
only.
My code I tried so far:
foreach ($data['Table'] as $value) { if (is_array($value) && $value['Type'] == "New") { json($value); break; } elseif (is_string($value)) { json($value); break; } }
And the output is
Notice: Undefined index: Type "New"
Both Mary Jane
& John Doe
gimme the right output, but Stan Lee
didn't work.
Anyone can help me out?
Thank you!
Remove the break;
from the foreach()
loop.
break
will interrupt the loop and no more array keys/values are treated anymore.
edit
Probable you do not want to sent the json each time the condition is met.
So before the foreach() create variable $output
and collect matched values to it $output[] = $value;
. And finally send the whole output using 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;
}
Maybe this will help you:
$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));
hi bro i have checked you code and i just remove the break try this:
$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;
}
}
and here is the output:
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
)
)
Let me know if this is what you need. feel free to ask.
Solution:
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);
}
Call to function:
echo arraySearch($data['Table']);
Use array_filter
like this, demo
array_filter($data, function($v){
return count($v) == 1 && $v['Type'] == 'New';
})
Result: [{"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));
?>
<?php foreach ($data['Table'] as $value) { foreach ($value as $data) { if(in_array('New', $data)) { //here you can code echo "<pre>"; print_r($data); } } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.