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

3
  • why you dont want to get value by key Commented Feb 10, 2014 at 10:36
  • 2
    without foreach, without for, without php ? lol. Just use foreach with the original array (not json) Commented Feb 10, 2014 at 10:37
  • I dont want key to get printed in output..only value needs to be printed.But i have no problem in using key to obtain value.. Commented Feb 10, 2014 at 10:56

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

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.