1

how can i create a empty array that i can push users information such as:

$users->set('bob', array('email' => '[email protected]', 'password' => '123456'));
$users->set('joe', array('email' => '[email protected]', 'password' => 'test'));

and retrieve/read the keys i want to read like:

$user = $users->get('bob');
echo 'Bob, your email is ' . $user['email'];

and also be able to retrieve all key names

$keys = $users->getKeys(); 

i have searched google but nothing worked right. Please show me how so i can learn more..

PS: It would also be nice to learn how to delete keys from the array like:

// Delete a key
$users->delete('joe');
5
  • Why don't you just do $users['bob'] = array(...)? Commented Sep 11, 2015 at 10:20
  • i need a system, but what does $users contain? what did you define it as Commented Sep 11, 2015 at 10:22
  • To do it with your syntax, you need to define a class. The class contains an array property, and the get and set methods access the array using normal PHP syntax. Commented Sep 11, 2015 at 10:22
  • could you show mw without a class ( get, set) Commented Sep 11, 2015 at 10:23
  • You can't do it without a class. The -> syntax only works with classes. Commented Sep 11, 2015 at 10:23

2 Answers 2

7

Just use ordinary array syntax:

$users['bob'] = array('email' => '[email protected]', 'password' => '123456');

$user = $users['bob'];

$keys = array_keys($users);

unset($users['joe']);
Sign up to request clarification or add additional context in comments.

3 Comments

Pefect, you sir are mr. Perfect! thanks it what i need
But how do i store the whole array holder: $users ? when i do: $users['bob'] = array('email' => '[email protected]', 'password' => '123456'); $users['nana'] = array('email' => '[email protected]', 'password' => '764634664'); $data= $users; $user = $data['nana']; echo 'Bob, your email is ' . $data['password']; it does not work
I've Never Seen Mr.Barmar Giving Wrong Answer. Lots Of Knowledge. God Gifted Brain. +1 For You Sir.
-1

Learn array_keys() function on http://php.net/manual/en/function.array-keys.php

$keys = array_keys($users);

2 Comments

How does this help with ->get and ->set?
$arr['xxx'] = 'yyy' - set value, $arr['xxx'] - get value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.