PHP - 从关联数组返回单行

时间:2023-02-02 21:37:31

Let say I have the datas in JSON,

假设我有JSON中的数据,

3 people have different datas such as New only / New and Old / Old and New.

3人拥有不同的数据,例如New only / New和Old / Old和New。

I tried using isset($_GET['NoID']); which is

我尝试使用isset($ _ GET ['NoID']);是的

http://localhost/test.php?NoID=31331 // Stan Lee

http://localhost/test.php?NoID = 31331 // Stan Lee

http://localhost/test.php?NoID=31332 // Mary Jane

http://localhost/test.php?NoID = 31332 // Mary Jane

http://localhost/test.php?NoID=31335 // John Doe

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.

我试着foreach()然后严格声明New和break,我也尝试了array_filter()和sort()。这些功能无法返回New。

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.

Mary Jane和John Doe都给出了正确的输出,但Stan Lee没有工作。

Anyone can help me out?

有人可以帮帮我吗?

Thank you!

7 个解决方案

#1


1  

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.

如果这是你需要的,请告诉我。随便问。

#2


1  

Remove the break; from the foreach() loop.

删除休息;来自foreach()循环。

break will interrupt the loop and no more array keys/values are treated anymore.

break将中断循环,不再处理数组键/值。

PHP break

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

可能你不想在每次满足条件时发送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;
    }

#3


1  

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

#4


0  

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

#5


0  

Use array_filter like this, demo

像这样使用array_filter,demo

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

#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);
    }
  }
}

#7


0  

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



    ?>

#1


1  

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.

如果这是你需要的,请告诉我。随便问。

#2


1  

Remove the break; from the foreach() loop.

删除休息;来自foreach()循环。

break will interrupt the loop and no more array keys/values are treated anymore.

break将中断循环,不再处理数组键/值。

PHP break

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

可能你不想在每次满足条件时发送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;
    }

#3


1  

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

#4


0  

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

#5


0  

Use array_filter like this, demo

像这样使用array_filter,demo

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

#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);
    }
  }
}

#7


0  

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



    ?>