读取子数组中包含字符串和对象的php数组

时间:2012-07-02 02:28:39

标签: php

我有以下输出唱var_dump ..如何读取每个数组的'transferfrom'值? '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)

  }
}

2 个答案:

答案 0 :(得分:2)

不确定您需要什么,但是这将从每个数组条目中选择'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
}

答案 1 :(得分:0)

foreach($arrays as $arr){
  $transferfrom = $arr['transferfrom'];
  //here you do whatever you want with $arr
  //...
}