sujai
sujai

Reputation: 13

printing json array data without looping using key in php

$c_array on printing gives below data
Array ( [0] => Array ( [Category_Name] => sample quiz question 1 [Score] => 50 ) [1] =>         Array ( [Category_Name] => sample quiz question 1 [Score] => 100 ) )

/json encoding array/

$jse= json_encode($c_array);

 echo $jse;

/*On echoing $jse i get this below json data */

[{"Category_Name":"sample quiz question 1","Score":"50"},{"Category_Name":"sample quiz question 2","Score":"100"}]

What i need is just below output

sample quiz question 1
sample quiz question 2

without key "Category_Name" while echoing and I wanted it to be done without using foreach loop or print_r (just only using echo)

how can i do this ?Any help is greatly appreciated .

Upvotes: 0

Views: 408

Answers (1)

fdehanne
fdehanne

Reputation: 1718

You can use array_map() to get only names :

$array = array(
  array(
    'Category_Name' => 'sample quiz question 1',
    'score' => 50
  ),
  array(
    'Category_Name' => 'sample quiz question 2',
    'score' => 100
  )
  // ...
);

function getName($array) {
  return $array['Category_Name'];
}

$result = array_map("getName", $array);

If you want to print values only, you can use array_walk() :

function printName($array) {
  echo $array['Category_Name']."\n";
}

$result = array_walk($array, "printName");

Upvotes: 1

Related Questions