PHP解包数组

时间:2013-02-19 11:39:42

标签: php arrays

我想学习一种解包嵌套数组的智能方法。例如,我有一个数组变量$ 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 timestamps和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 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分: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']

答案 2 :(得分: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;
    }
}

答案 3 :(得分: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";
}