PHP解包数组

I would like to learn a smart way to unpack nested array. For instance, i have an array variable $rma_data['status'] which looks like below;

 [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
            )

    )

I would like to store the Created timestamps and statusId into an variables based on the condition like: if we find out there is "Open" status exist, we will use Open instead of "New" and "Created" . If there is only New and Created, we will use New instead .

Current version of my way to do that:

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

                                            case 2:

                                            case 3:
                }

Any ideas?

For small to medium scale, what you already have looks good.

My only suggestions would be to use additional variables, for example the count and to unroll some of the compact code to be more efficient and readable.

For example:

$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
    }
}

I do not quite understand a necessary condition but it can be like this will help:

$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;
    }
}

If you're really depended on these three specific values of statusName, a more straightforward and readable way to go about it would be to create an indexed array of status types which you can test more easily.

For example:

$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']
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';
 }

}

Second:

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";
}