读取子数组中具有字符串和对象的 PHP 数组


read php array having string and object in sub array

我有以下输出唱var_dump..如何读取每个数组的"传输源"值?"ST00576"和"OT01606"是动态值。它可以在子序列数组上更改。

string(19) "TB3360    7D  B  70"
array(2) {
  ["ST00576"]=>
  object(stdClass)#1 (13) {
    ["transferfrom"]=>
    int(102)
    ["transferto"]=>
    int(66)
    ["BR_ID"]=>
    int(102)
  }
  ["OT01606"]=>
  object(stdClass)#2 (13) {
    ["transferfrom"]=>
    int(102)
    ["transferto"]=>
    int(66)
    ["BR_ID"]=>
    int(66)
  }
}
string(19) "TB3360    BL  A  75"
array(2) {
  ["ST00576"]=>
  object(stdClass)#3 (13) {
    ["transferfrom"]=>
    int(102)
    ["transferto"]=>
    int(66)
    ["BR_ID"]=>
    int(102)
  }
  ["OT01606"]=>
  object(stdClass)#4 (13) {
    ["transferfrom"]=>
    int(102)
    ["transferto"]=>
    int(66)
    ["BR_ID"]=>
    int(66)
  }
}

不确定你到底需要什么,但这将从每个数组条目中挑选'transferfrom'项,并返回一个具有相同键但字符串作为值的数组。

$arr = array_map(function($item) {
    return $item->transferfrom;
}, $arr);

或:

function pick_transferfrom($item)
{
    return $item->transferfrom;
}
$arr = array_map('pick_transferfrom', $arr);

结果(缩短):

['OT01606' => 102, 'ST00576' => 102];

或者你可以直接迭代:

foreach ($arr as $key => $item) {
    $transferfrom = $item->transferfrom;
    // do whatever you like with $transferfrom and $key
}
foreach($arrays as $arr){
  $transferfrom = $arr['transferfrom'];
  //here you do whatever you want with $arr
  //...
}