基于键将多维数组拆分为两个数组

时间:2015-12-12 10:12:04

标签: php arrays json codeigniter multidimensional-array

我是PHP World的新手,并且在过去三天里遇到了这个问题。

Array
(
    [0] => stdClass Object
        (
            [sim_id] => 15
            [Tarrif_code] => D34
            [rental] => USD 345
            [service_provider] => Vodafone
            [incoming_calls] => 3.2 /sec
            [local_outgoing] => 3.2 /sec
            [international_call] => 3.2 /sec
            [company_name] => Airtel
            [local_text] => 
            [international_text] => 
            [data] => 2G
            [sim_type_id] => 1
            [offer_short_descrption] => 
            [validity] => 
            [dialing_instruction] => 
            [recharge_instruction] => 
            [usage_warning] => 
            [country_id] => 1
            [type] => Prepaid
        )
)

,结果应为

Array
(
    [0] => stdClass Object
        (
            [sim_id] => 15
            [Tarrif_code] => D34
            [company_name] => Airtel            
            [rental] => USD 345
            [service_provider] => Vodafone
            [country_id] => 1
            [type] => Prepaid


   [1] => stdClass Object
        (
            [international_call] => 3.2 /sec
            [incoming_calls] => 3.2 /sec
            [local_outgoing] => 3.2 /sec)
            [local_text] => 
            [international_text] => 
            [data] => 2G
            [sim_type_id] => 1
            [offer_short_descrption] => 
            [validity] => 
            [dialing_instruction] => 
            [recharge_instruction] => 
            [usage_warning] => 
        )
)

1 个答案:

答案 0 :(得分:0)

关于提供的数据:

$arr[0] = new stdClass;

$arr[0]->id = 1;
$arr[0]->name = 2;
$arr[0]->state = 3;
$arr[0]->land = 4;

$arr2[0] = new stdClass;

foreach($arr[0] as $k => $v)
{
    if (!in_array($k, array('id', 'land')))// array of keys for first object
    {
        $arr2[0]->{$k} = $v;
        unset($arr[0]->{$k});
    }
}

var_dump($arr[0]);
echo "<br>";
var_dump($arr2[0]);
exit;