5

我想学习一种解压嵌套数组的聪明方法。例如,我有一个数组变量 $rma_data['status'] 如下所示;

 [status] => Array
    (
        [0] => Array
            (
                [created] => 1233062304107
                [statusId] => 5
                [statusName] => Open
            )

        [1] => Array
            (
                [created] => 1233061910603
                [statusId] => 2
                [statusName] => New
            )

        [2] => Array
            (
                [created] => 1233061910603
                [statusId] => 1
                [statusName] => Created
            )

    )

我想根据条件将 Created 时间戳和 statusId 存储到一个变量中,例如:如果我们发现存在“Open”状态,我们将使用 Open 而不是“New”和“Created”。如果只有 New 和 Created,我们将使用 New 代替。

我这样做的当前版本:

for($i=0; $i<count($rma_data['status']); $i++)
                {
                    switch($rma_data['status'][$i]['statusId'])
                    {
                        case 5: 

                                            case 2:

                                            case 3:
                }

有任何想法吗?

4

4 回答 4

2

对于中小型规模,您已经拥有的看起来不错。

我唯一的建议是使用额外的变量,例如计数并展开一些紧凑的代码以提高效率和可读性。

例如:

$total=count($rma_data['status']);
for($i=0; $i<$total; $i++){
    $x=$rma_data['status'][$i];
    if($x['statusName']=='Open'){ // Use your criteria
        $t=$x['created'];
        //...Do Work
    }
}
于 2013-02-19T11:48:55.837 回答
1

如果您真的依赖于 的这三个特定值statusName,那么一种更直接、更易读的方法是创建一个状态类型的索引数组,您可以更轻松地对其进行测试。

例如:

$rma_statuses = array();
foreach ((array)$rma_data['status'] as $status) :
    $rma_statuses[$status['statusName']] = array(
         'created'=>$status['created'],
         'id'=>$status['statusId']
    );
endforeach;

$rma_stauts = $rma_statuses['open'] ?: ($rma_statuses['new'] ?: $rma_statuses['created']);

 // Do something with $rma_stauts['created'] and $rma_stauts['id']
于 2013-02-19T12:00:08.587 回答
0
if(is_array($rma_data['status'])){
 //assuming there are only three values inside it
 //case1
 $open = ( $rma_data['status'][0]['statusName'] == 'Open' || 
           $rma_data['status'][1]['statusName'] == 'Open' || 
           $rma_data['status'][2]['statusName'] == 'Open');
 //case2
 $new = (!$open && 
         ($rma_data['status'][0]['statusName'] == 'New' || 
          $rma_data['status'][1]['statusName'] == 'New' || 
          $rma_data['status'][2]['statusName'] == 'New' ));
 if($open){
  echo 'open';
 }elseif($new){
  echo 'New';
 }else{
  echo 'None';
 }

}

第二:

foreach($rma_data['status'] as $key => $val){
 $statusName = $val['statusName'];
 $newarray[$statusName] = $val;
}
echo '<pre>';
print_r($newarray);

if(array_key_exists('Open', $newarray)){
 $created = $newarray['Open']['created'];
 $statusId = $newarray['Open']['statusId'];
 echo 'Open';

}
elseif(array_key_exists('New', $newarray)){
 $created = $newarray['New']['created'];
 $statusId = $newarray['New']['statusId'];
  echo 'New';
}else{
 echo "None";
}
于 2013-02-19T12:12:31.563 回答
0

我不太了解必要条件,但这可能会有所帮助:

$searched_status_id = null;
$searched_timestamp = null;
foreach ($rma_data['status'] as $id => $status) {
    if ((!$searched_timestamp && !$searchуd_status_id) ||
        ($status['statusName'] == 'New' || $status['statusName'] == 'Open')) {
        $searched_timestamp = $status['created'];
        $searched_status_id = $status['statusId'];
    }
    if ($status['statusName'] == 'Open') {
        break;
    }
}
于 2013-02-19T11:46:17.553 回答