0

I have arrays $devices,$port

print_r($devices);
Array(
[0] => cisco1
[1] => cisco2
 )

print_r($port);
Array
(
[0] => Port1/1/1
[1] => Port1/1/10
[2] => Port1/1/11
) 

I want to create an array $devlist which would be something like this:

Array(

[cisco1] =>Port1/1/1
           Port1/1/10
           Port1/1/11

[cisco2] =>Port2/1/1
           Port2/1/10
           Port2/1/11

)

My point is there is an array of devices($devices) and arrays of ports that are there in each of the device. The $port array gets created newly for each device in the $device array.

What i have tried so far:

foreach ($devices as $value)
{
$port=();
//iam polling the respective device and getting a list of ports available for that device in array **$port**

array_push($devices[$value], $port);
}

This method generates an error "array_push() expects parameter 1 to be array, null given"

Kindly excuse me if this seems an easy question becoz iam new to php and scripting as well:(

4 Answers 4

1

Try this:

    $devlist = array();
    foreach ($devices as $value)
    {
            $port=();
            //iam polling the respective device and getting a list of ports available for that device in array **$port**

            $devlist[$value] = $port;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Do you want something like this? If so, i don't understand why, when you could just use the values from $ports for each $device?

$devices = array
(
    'cisco1', 'cisco2'
);

$ports = array 
(
    'Port1/1/1',
    'Port1/1/10',
    'Port1/1/11'
);

$dev_list = array();

foreach ($devices as $device) 
{
    $dev_list[$device] = array();

    foreach ($ports as $port) 
    {
        array_push($dev_list[$device], $port);
    }
}

echo '<pre>';
print_r($dev_list);
echo '</pre>';

Array
(
    [cisco1] => Array
        (
            [0] => Port1/1/1
            [1] => Port1/1/10
            [2] => Port1/1/11
        )

    [cisco2] => Array
        (
            [0] => Port1/1/1
            [1] => Port1/1/10
            [2] => Port1/1/11
        )

)

5 Comments

yup i should have done like this: $dev_list[$device] = $port; ur detailed post made it a lot clearer :) as i said iam a bit of a noob still.....thanks a lot
No problem, but why do you want that? You're gonne dublicate all the values in the new array, instead of just using the $ports-array?
$ports will not be same for each device in $devices....the above one that i had given was just for an example.....so $ports will be different for each $device in $devices...so no duplication right?
Right, but then you could just setup your $devices-array with cisco1, cisco2 etc.. as keys with empty arrays as values from start, and then add the ports as values, instead of putting them to $ports-array first? :)
i would first have to do some manupulation on the ports array like array_unique....so i would hav to get the list of ports in an array first manipulate them and assign them on the fly:)
0

You may use a foreach or a for loop but the main thing is that you have to use variable variables.

Solution One:

$devlist = array();
foreach($devices as $key => $device){
$devlist[$device] = ${"port".($key+1)};
}

Solution Two:

$devlist = array();
$size = sizeof($devices);
for($i = 0; $i < $size; $i++){
$devlist[$devices[$i]] = ${"port".($i+1)};
}

1 Comment

$devlist[$devices[$i]] = ${"port".($i+1)}; This line would assign $port1,$port2......to $devlist[$devices] right?? i had edited my question and removed $port1,$port2 etc.......there is jus 1 array $port which gets overwritten foreach ($devices as $value)...
0

If I understand your question, you want something like this:

$arr = array
(
    'cisco1' => array 
    (
        'Port1/1/1', 
        'Port1/1/10',
        'Port1/1/11'
    ),
    'cisco2' => array
    (
        'Port1/1/1', 
        'Port1/1/10',
        'Port1/1/11'
    )
);

foreach($arr as $key => $value) // use the foreach-loop like this to get both keys and values
{
    echo "$key: <br />-" . implode('<br />-', $value) . '<br /><br />';
}

prints:

cisco1: 
-Port1/1/1
-Port1/1/10
-Port1/1/11

cisco2: 
-Port1/1/1
-Port1/1/10
-Port1/1/11

2 Comments

No,iam afraid u misunderstood ,my question was how do i form that $arr in the first place? i have $cisco1 and $cisco2 ...how do i make an array that has cisco1,cisco2 etc as its key and $port array as its value?
Added a new answer, hope it helps. :) But if that's what you're looking for, i don't understand?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.