0

I'm trying to add more arrays to an array.

$data[$username] = array('bytes' => $bytes, 'ip' => array( $ip => array('bytes' => $bytes)));

How do you add one more array to ip array?

I tried this but wont work.

 $data[$username]['ip'] = array($ip => array('bytes' => $bytes));
1
  • $data[$username]['ip'][] = array($ip => array('bytes' => $bytes)); Commented Nov 23, 2013 at 3:44

2 Answers 2

1

So you have your data array...

$data[$username] = array(
    'bytes' => $bytes, // bytes?
    'ip' => array(
        $ip => array(
            'bytes' => $bytes // bytes again?
        }
    )
);

To add more key/value pairs to the ip array, you need to do the following, assuming $ip is not already a key in the array. If it is, it will overwrite the value currently at that key.

$data[$username]['ip'][$ip] = array(
    'bytes' => $bytes
);
Sign up to request clarification or add additional context in comments.

Comments

1
$data[$username]['ip'][$ip] = array('bytes' => $bytes);

This will add another array('bytes' => $bytes) to the $data[$username]['ip'] array.

Comments

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.