John Davidson

data gets overwritten when using array_combine in php after creating another array

0 comments
Message:


Trying to figure out how I can update the new array with a value.


$data = array('[ETRA-VRTR-MIB::vRtrIfName.1.1]' => 'STRING: "intf1"', '[ETRA-VRTR-MIB::vRtrIfName.1.2]' => 'STRING: "intf2"');

echo '<pre>';
print_r($data);

foreach($data as $key => $val) {
$newval = explode(':',trim($val, 'STRING: '));
$newkey = explode(' ',trim($key, '[ETRA-VRTR-MIB::vRtrIfName.]'));

$Array = array_combine($newkey, $newval);

echo '<pre>';
print_r($Array);

}

$data = $Array;
echo '<pre>';
print_r($data);

The first print output before the for loop


Array
(
[[ETRA-VRTR-MIB::vRtrIfName.1.1]] => STRING: "intf1"
[[ETRA-VRTR-MIB::vRtrIfName.1.2]] => STRING: "intf2"
)

The Second print output while in the for loop


    Array
(
[1.1] => "intf1"
)
Array
(
[1.2] => "intf2"
)

The third print output of $data


   Array
(
[1.2] => "intf2"
)

As you can see, it gets overwritten so only the second array is displayed. Trying to figure out how I can iterate through $Array and assign it appropriate key. Final $data should be as below.


Array
(
[1.1] => "intf1"
)
Array
(
[1.2] => "intf2"
)

Thank you so much for your help.



My Answer:
'GigabitEthernet1/0/1', '[ETRA-VRTR-MIB::vRtrIfName.1.2]' = 'GigabitEthernet1/0/2');

$newData = array_combine(array_keys($data), array_fill(0, count($data), 'NewValue'));

print_r($newData);

Output:
Array
(
[ETRA-VRTR-MIB::vRtrIfName.1.1] => NewValue
[ETRA-VRTR-MIB::vRtrIfName.1.2] => NewValue
)

Rate this post

4 of 5 based on 1145 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us