0

Title sounds a bit convoluted but the title itself is pretty self-descriptive I think.

Assume that I have an associative array like this:

$data['blog_info'] = array(
   "title" => "Adventure",
   "author" => "Yo"
);

Now, I would like add to the current key 'blog_info" another set of key => value array. so the result should be :

$data['blog_info'] = array(
   "title" => "Adventure",
   "author" => "Yo",
   "ISBN" => "23423498"
);

so for example I would like to add "ISBN" => "23423498" inside this 'blog_info' key. How am I able to achieve this? (but by going like $data['blog_info'].push("ISBN" => "23423498") etc?)

3
  • You want something like $data['blog_info']['ISBN']? Commented Sep 11, 2014 at 8:13
  • 1
    What would the result look like? Also, the {..} are incorrect. Commented Sep 11, 2014 at 8:13
  • In PHP array_push or the [] operator would allow you to push an item to an array but since you have an associative array (Strings as keys) you should use $data['blog_info']['ISBN'] = '23423498'; to achieve your desired result. See my answer below. Commented Sep 11, 2014 at 8:27

2 Answers 2

1

The below will achieve it ($data['blog_info'] is just an array).

$data['blog_info']['ISBN'] = '23423498';
Sign up to request clarification or add additional context in comments.

Comments

1

In PHP you don't need the curly braces { and } in this context.

$data['blog_info'] = array(
   "title" => "Adventure",
   "author" => "Yo"
);

Try something like

$data['blog_info']['ISBN'] = '23423498';

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.