PHP Unpack Array

I would like to learn a smart way to unzip a nested array. For example, I have an array variable $ rma_data ['status'] that looks like this;

 [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 save the created timestamps and statusId in variables based on conditions such as: if we find that the status is "Open", we will use Open instead of "New" and "Created". If there is only New and Created, we will use New instead.

The current version of my way to do this is:

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

                                            case 2:

                                            case 3:
                }

Any ideas?

+5
source share
4 answers

For small and medium-sized, you already look good.

, , , , .

:

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

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']
+1

I do not quite understand the necessary condition, but it could be like this:

$searched_status_id = null;
$searched_timestamp = null;
foreach ($rma_data['status'] as $id => $status) {
    if ((!$searched_timestamp && !$searchd_status_id) ||
        ($status['statusName'] == 'New' || $status['statusName'] == 'Open')) {
        $searched_timestamp = $status['created'];
        $searched_status_id = $status['statusId'];
    }
    if ($status['statusName'] == 'Open') {
        break;
    }
}
0
source
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';
 }

}

Secondly:

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

All Articles