PHP - Return 关联数组中的单行

PHP - Return single row from associated arrays

假设我有 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",
              }
            },
          ]
     }

我想要 return 条件是 New(单行)。

我试过 foreach() 然后 Newbreak 的严格声明,我也试过 array_filter()sort()。这些功能仅适用于 return 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 JaneJohn Doe 都给出了正确的输出,但是 Stan Lee 没有用。

谁能帮帮我?

谢谢!

foreach() 循环中删除 break;

break 将中断循环,不再处理数组 keys/values。

PHP 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;
    }

解决方案:

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']);

这样使用array_filterdemo

array_filter($data, function($v){
    return count($v) == 1 && $v['Type'] == 'New'; 
})

结果:[{"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));



    ?>

也许这会对您有所帮助:

     $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));

<?php 
foreach ($data['Table'] as $value) {
   foreach ($value as $data) {
    if(in_array('New', $data)) {
       //here you can code
       echo "<pre>";
       print_r($data);
    }
  }
}

嗨,兄弟,我已经检查了你的代码,我只是删除了中断试试这个:

 $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
     )

  )

让我知道这是否是您需要的。欢迎提问。