Reputation: 53
Can someone help me how to push values into multidimensional array?
here is my code:
`
$connection = ssh2_connect('10.10.10.10', 22);
ssh2_auth_password($connection, 'user', 'pass');
$stream = ssh2_exec($connection, 'show l2circuit connections brief | display json');
stream_set_blocking($stream, true);
$stream_out=ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$units = json_decode(stream_get_contents($stream_out), true);
$ps_data=array();
for ($i=0; $i <= count($units["l2circuit-connection-information"][0]["l2circuit-neighbor"][0]["connection"])+1; $i++) {
$dev_ip=$units["l2circuit-connection-information"][0]["l2circuit-neighbor"][$i]["neighbor-address"][0]["data"];
#$ps_data[$i]=$dev_ip;
for ($d=0; $d < count($units["l2circuit-connection-information"][0]["l2circuit-neighbor"][$i]["connection"]); $d++) {
$ps_int = explode(".", $units["l2circuit-connection-information"][0]["l2circuit-neighbor"][$i]["connection"][$d]["connection-id"][0]["data"]);
$ps_data[$dev_ip] = array($d => $ps_int[0]);
}
}
echo "<pre>";
print_r($ps_data)
`
here is output: `
Array
(
[10.255.255.140] => Array
(
[4] => ps203
)
[10.255.255.171] => Array
(
[6] => ps44
)
[10.255.255.172] => Array
(
[6] => ps205
)
[10.255.255.173] => Array
(
[7] => ps25
)
[10.255.255.174] => Array
(
[8] => ps51
)
[10.255.255.178] => Array
(
[4] => ps37
)
[10.255.255.193] => Array
(
[8] => ps33
)
)
`
In this case each $dev_ip have several interfaces and I want to push them to array but it added only last interface with corresponding index
Upvotes: 0
Views: 38
Reputation: 57121
Your big problem is
$ps_data[$dev_ip] = array($d => $ps_int[0]);
This overwrites any previous contents of the array with a new array with 1 element.
Instead, you should add the new item to the array...
$ps_data[$dev_ip][$d] = $ps_int[0];
Upvotes: 1