如何返回带有唯一键的数组?

时间:2019-12-09 08:08:45

标签: php arrays

我有这个JSON。

[{"transaction_code":"NKO27E93OU","transaction_timestamp":"20191124215327","transaction_date":"24-Nov-2019","transaction_time":"09:53 PM","transaction_amount":"1.00","transaction_account":"1235688jose","transactor_number":"254704476967","transactor_fname":"JOSPHAT","transactor_mname":"GITIRA","transactor_lname":""},{"transaction_code":"NL20DG5S4E","transaction_timestamp":"20191202124826","transaction_date":"02-Dec-2019","transaction_time":"12:48 PM","transaction_amount":"10.00","transaction_account":"s84d","transactor_number":"254704476967","transactor_fname":"JOSPHAT","transactor_mname":"GITIRA","transactor_lname":""}] 

我想返回带有特定交易代码的唯一数组。假设交易代码为NL20DG5S4E,则返回整个数组。目前,我只能使用此代码访问元素。

      $pc = json_decode($paymentdata, true);


    // foreach($pc as $p){
    //     echo $p['transaction_code'].'\n';
    // }

    $array = $pc[6]['transaction_code'];
    return $array;

5 个答案:

答案 0 :(得分:3)

使用简单的foreach循环遍历数据,并在找到事务值后返回数组。

<?php

$paymentdata = '[{"transaction_code":"NKO27E93OU","transaction_timestamp":"20191124215327","transaction_date":"24-Nov-2019","transaction_time":"09:53 PM","transaction_amount":"1.00","transaction_account":"1235688jose","transactor_number":"254704476967","transactor_fname":"JOSPHAT","transactor_mname":"GITIRA","transactor_lname":""},{"transaction_code":"NL20DG5S4E","transaction_timestamp":"20191202124826","transaction_date":"02-Dec-2019","transaction_time":"12:48 PM","transaction_amount":"10.00","transaction_account":"s84d","transactor_number":"254704476967","transactor_fname":"JOSPHAT","transactor_mname":"GITIRA","transactor_lname":""}]';


$pc = json_decode($paymentdata, true);

function returnAllInfo($pc,$transaction_code){
    foreach($pc as $data){
        if($data['transaction_code'] === $transaction_code) return $data;
    }
    return [];
}

print_r(returnAllInfo($pc,'NL20DG5S4E'));

演示: https://3v4l.org/iVcb4

答案 1 :(得分:3)

如果使用array_column,则可以通过键“ NL20DG5S4E”直接访问。

$pc = json_decode($paymentdata, true);

$arrTrCode = array_column($pc,null,'transaction_code')['NL20DG5S4E'];

/* or
$arrWithKeys = array_column($pc,null,'transaction_code');
$arrTrCode = $arrWithKeys['NL20DG5S4E'];
*/

echo '<pre>';
var_export($arrTrCode);

输出:

array (
  'transaction_code' => 'NL20DG5S4E',
  'transaction_timestamp' => '20191202124826',
  'transaction_date' => '02-Dec-2019',
  'transaction_time' => '12:48 PM',
  'transaction_amount' => '10.00',
  'transaction_account' => 's84d',
  'transactor_number' => '254704476967',
  'transactor_fname' => 'JOSPHAT',
  'transactor_mname' => 'GITIRA',
  'transactor_lname' => '',
) 

答案 2 :(得分:2)

您可以使用array_filter方法来检查特定密钥的值,例如:

$pc = json_decode($paymentdata, true);
$code = 'NL20DG5S4E';
$result = array_filter($pc, function($val) use($code){
   return $val['transaction_code'] == $code;
});

或者,如果您使用的是laravel >= 5.5,则可以使用collection

轻松获得它。
$pc = json_decode($paymentdata, true);

$collection = collect($pc);

$result = $collection->firstWhere('transaction_code', $code);

这是link

答案 3 :(得分:0)

下面的代码段可以帮助您找到所需的内容。

<?php 
$pc = json_decode($paymentdata, true);

$arr = [];
foreach($pc as $key => $val)
{
  $arr[$val]['transaction_code'] = $val;
}

return $arr;
?>

答案 4 :(得分:0)

您可以使用array_filter来解决这个问题。

$pc = json_decode($array, true);

$result = array_filter($pc, function ($transaction) {
    return $transaction['transaction_code'] === 'NKO27E93OU';
});

echo $result[0];