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.
This is because you are not merging previous array in the loop that's why it is overwriting try array_merge()
like below:
<?php
$data = array('[ETRA-VRTR-MIB::vRtrIfName.1.1]' => 'STRING: "intf1"', '[ETRA-VRTR-MIB::vRtrIfName.1.2]' => 'STRING: "intf2"');
echo '<pre>';
print_r($data);
$Array = array();
foreach($data as $key => $val) {
$newval = explode(':',trim($val, 'STRING: '));
$newkey = explode(' ',trim($key, '[ETRA-VRTR-MIB::vRtrIfName.]'));
$Array = array_merge($Array, array_combine($newkey, $newval));
echo '<pre>';
print_r($Array);
}
$data = $Array;
echo '<pre>';
print_r($data);
check the output here https://paiza.io/projects/WXZPyYsiYH9ZtLXa3axGKw?language=php