Question

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

Was it helpful?

Solution

after the edit i guess you want more than 2 ?

so ..

foreach ($new as $n){
echo $n['post'].',';
}

OTHER TIPS

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...

PHP >= 5.5.0

foreach(array_column($new, 'post') as $post) {
  echo $post;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top