2012-07-02 28 views
1

我有以下輸出唱var_dump ..我如何讀取每個數組的'transferfrom'的值? 'ST00576'和'OT01606'是動態值。它可以在子序列上改變。閱讀PHP數組中有字符串和子數組中的對象

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) 

    } 
} 
+0

也許我不理解你的問題,但你不只是需要一個foreach循環嗎? – phpmeh

回答

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 
} 
0
foreach($arrays as $arr){ 
    $transferfrom = $arr['transferfrom']; 
    //here you do whatever you want with $arr 
    //... 
}