简体   繁体   中英

convert array in to associative array

i have following array. and i want to dynamically update this array.

Array
(
[0] => Array
    (
        [id] =>
        [cip] => 172.35.2.45
        [ctime] => 1498718978
        [uip] => 172.35.2.40
        [utime] => 1498712478
        [interface] => wan
        [type] => pass
        [disabled] => no
    )
[1] => Array
    (
        [id] =>
        [cip] => 172.35.2.24
        [ctime] => 1498778578
        [uip] => 172.35.2.41
        [utime] => 1498532478
        [interface] => wan
        [type] => pass
        [disabled] => no
    )
)

and i want it convert into.

Array
(
[0] => Array
    (
        [id] =>
        updated => Array
        (
            [time] => 1498712478
            [username] => admin@172.35.2.40
        )
        created => Array
        (
            [time] => 1498712478
            [username] => admin@172.35.2.45
        )
        [interface] => wan
        [type] => pass
        [disabled] => no
    )
[1] => Array
    (
        [id] =>
        [updated] => Array
        (
            [time] => 1498532478
            [username] => admin@172.35.2.41
        )
        created => Array
        (
            [time] => 1498778578
            [username] => admin@172.35.2.24
        )
        [interface] => wan
        [type] => pass
        [disabled] => no
    )
)

here we have two element in main array and it can be very, i tried it by using foreach loop and some other thing. but i am not near to the answer i want please help me to sort this

Figured I'd convert my comment into an answer cause it was long and unruly without proper indentation. $array1 is original array

$array2['id'] = $array1['id'];
$array2['updated'] = ['time'     => $array1['utime'],
                      'username' => 'admin@'.$array1['uip']];
$array2['created'] = ['time'     => $array1['ctime'],
                      'username' => 'admin@'.$array1['cip']];
$array2['interface'] = $array1['interface'];
$array2['type'] = $array1['type'];
$array2['disabled'] = $array1['disabled'];

This basically just goes line by line for your second array and sets the array elements properly according to your question.

I'm sure there's a way to automate what you're trying to do so you don't have to go line by line, but honestly, any answer that automates this step is overkill and would certainly take more time to process than this.

Short solution using array_combine and unset functions:

// $arr is your initial array
$arr['updated'] = array_combine(['time', 'username'], [$arr['utime'], 'admin@' . $arr['uip']]);
$arr['created'] = array_combine(['time', 'username'], [$arr['ctime'], 'admin@' . $arr['cip']]);
unset($arr['utime'], $arr['ctime'], $arr['uip'], $arr['cip']);

print_r($arr);

The output:

Array
(
    [id] => 
    [interface] => wan
    [type] => pass
    [disabled] => no
    [updated] => Array
        (
            [time] => 1498712478
            [username] => admin@172.35.2.40
        )

    [created] => Array
        (
            [time] => 1498718978
            [username] => admin@172.35.2.45
        )
)

I think the cleanest option would be like this:

$array2 = [
        'id => '',
        'updated' => [
            'time' => $array1['utime'],
            'username' => $array1['username']
        ],
        'created' => [
            'time' => $array1['ctime'],
            'username' => $array1['username']
        ],
        'interface' => $array1['wan'],
        'type' => $array1['pass'],
        'disabled' => $array1['no']
    ]
];

You didn't state PHP version so I used square brackets to start arrays, if you're still on less than 7 you'll need to swap all the square brackets for array();

I assumed that you would have more than one entry in your starting array and would want to build the resulting array out of the bigger initial array, so I put it through a loop.

$array1[] = array(
        'id' => '',
        'cip' => '172.35.2.45',
        'ctime' => 1498718978,
        'uip' => '172.35.2.40',
        'utime' => 1498712478,
        'interface' => 'wan',
        'type' => 'pass',
        'disabled' => 'no'
    );

$array2 = array();
$directkeys = array('id', 'interface', 'type', 'disabled');
foreach ($array1 as $mk => $subarray){
    foreach($subarray as $k => $v){
        if(in_array($k, $directkeys)) $array2[$mk][$k] = $v;
        if($k == 'uip') $array2[$mk]['id']['updated']['username'] = 'admin@'.$v;
        if($k == 'utime') $array2[$mk]['id']['updated']['time'] = $v;
        if($k == 'cip') $array2[$mk]['id']['created']['username'] = 'admin@'.$v;
        if($k == 'ctime') $array2[$mk]['id']['created']['time'] = $v;    
    }
}   

Output is:

[0] => Array
    (
        [id] => Array
            (
                [created] => Array
                    (
                        [username] => admin@172.35.2.45
                        [time] => 1498718978
                    )

                [updated] => Array
                    (
                        [username] => admin@172.35.2.40
                        [time] => 1498712478
                    )

            )

        [interface] => wan
        [type] => pass
        [disabled] => no
    )

it's been done by doing this.

foreach($oldarray as $key => $value){
    $newarray['created']['time'] = '"'.strtotime($value['ctime']).'"';
    $newarray['created']['username'] = 'admin@'.$value['cip'];
    $newarray['updated']['time'] = '"'.strtotime($value['utime']).'"';
    $newarray['updated']['username'] = 'admin@'.$value['uip'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM