-3

How do I get the value of an associative array using its key. For example, I have:

$new = array(

array(
"post" => "anything",
"newt" => "Nothing"
),
array(
"post" => "something",
"newt" => "Filthy"
),
    array(
"post" => "one value",
"newt" => "normal"
),
    array(
"post" => "two value",
"newt" => "happy"
),
    array(
"post" => "Three",
"newt" => "more"
)
);

Now I want to get the value of post. That means I want to get echoed anything, something. How do I accomplish this? I tried a way but that's not working. Example;

print_r($new['post']);

I also tried echo $new['post']; but doesn't work. Please help

9
  • echo $new[0]['post']; (returns:anything) and echo $new[1]['post']; (returns:something) Commented Apr 14, 2014 at 22:21
  • What if I want both the values? Commented Apr 14, 2014 at 22:21
  • that is both the values (anything, something) Commented Apr 14, 2014 at 22:21
  • 3
    This question appears to be off-topic because it shows no prior research nor minimal understanding of the problem being solved Commented Apr 14, 2014 at 22:22
  • @Dagon I mean by one line of code either print or echo instead of two echoes Commented Apr 14, 2014 at 22:23

3 Answers 3

1

after the edit i guess you want more than 2 ?

so ..

foreach ($new as $n){
echo $n['post'].',';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that's what I wanted. Thank you so much :)
every one edits my answers ;( (usually trivially)
1

You can do like this

foreach($new as $key => $value){
  echo $value['post']."<br/>"; 
}

Incase you don't need the key:

foreach($new as $value){
  echo $value['post']."<br/>"; 
}

Live Demo...

Comments

1

PHP >= 5.5.0

foreach(array_column($new, 'post') as $post) {
  echo $post;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.