2

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.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
Mike T
  • 47
  • 3

1 Answers1

0

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

lazyCoder
  • 2,544
  • 3
  • 22
  • 41
  • Thanks but when I do that, the final $data output would become like this which is not what I want. Array ( [[ETRA-VRTR-MIB::vRtrIfName.1.1]] => STRING: "intf1" [[ETRA-VRTR-MIB::vRtrIfName.1.2]] => STRING: "intf2" ) – Mike T Nov 05 '20 at 05:16
  • try this working code here https://paiza.io/projects/WXZPyYsiYH9ZtLXa3axGKw?language=php or copy my answer code – lazyCoder Nov 05 '20 at 05:19
  • Would like the final output, $data, to be stored as this.. Array ( [1.1] => "intf1" ) Array ( [1.2] => "intf2" ) – Mike T Nov 05 '20 at 05:19
  • Not sure why but getting PHP Warning: array_merge(): Expected parameter 1 to be an array, null given and it points to this code: $Array = array_merge($Array, array_combine($newkey, $newval)); Do you know why? It's defined as an array. – Mike T Nov 05 '20 at 05:38
  • show me your code, i guess you did not declare $Array = array() just above the foreeach – lazyCoder Nov 05 '20 at 06:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224136/discussion-between-mike-t-and-vishal-solanki). – Mike T Nov 05 '20 at 07:05