1
var $json = [{name:'abaneel',age:23},
    {name: 'john', age: 32},
    {name: 'Dev' , age:22}
];

How to create this type of string from this json object in php as like this string:

$simple string ="abaneel,23,john,32,Dev,22";

2 Answers 2

4

here what you need to do:

$json = '[
  {"name": "abaneel", "age" : 23},
  {"name": "john", "age" : 32},
  {"name": "Dev", "age" : 22}
]';
$implode = array();
$multiple = json_decode($json, true);
foreach($multiple as $single)
    $implode[] = implode(', ', $single);

echo implode(', ', $implode);   //this will output abaneel, 23, john, 32, Dev, 22
/*
    You can see that it will be hard to distinguish that where line(one array is ending);
*/
echo implode(' | ', $implode);  //will output abaneel, 23 | john, 32 | Dev, 22

hope this will help

Sign up to request clarification or add additional context in comments.

2 Comments

i am newby in stack overflow i cant understand what do u mean by mark as answer .... but its work nice . thank you so much
I mean you should accept this as answer, if you hover the answer, under the vote button there will be a tick mark, check it.
0
<?php
$json ='[
  {"name": "abaneel", "age" : 23},
  {"name": "john", "age" : 32},
  {"name": "Dev", "age" : 22}
]';

$data=json_decode($json,true);
$your_string="";

foreach($data as $key=>$v){ 
    $your_string.=$data[$key]['name'].",".$data[$key]['age'].",";
}
$your_string= trim($your_string, ",");
echo $your_string;

 ?>

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.