1

I have this string:

[{"position":"1d","number":10,"nb_plot1":12,"max_actions":3}
{"position":"2d","number":7,"nb_plot1":15,"max_actions":30}
{"position":"3d","number":100,"nb_plot1":2,"max_actions":5}]

and i need to obtain two different string with different format like this:

for numbers:

[10,7,100]

for positions:

['1d','2d','3d']

and cut unnecesarry strings.

I'm a noob sorry.

2
  • 1
    php.net/manual/en/function.json-decode.php you may find this helpful Commented Apr 25, 2012 at 19:51
  • do you want to obtain two array not string Commented Apr 25, 2012 at 19:51

1 Answer 1

2

Decode the string in a array with json_decode.

Iterate over this array and build new arrays for numbers and position.

Encode the arrays.

In code:

$arr = json_decode($string);

$numbers = array();
$positions = array();

foreach($arr as $a) 
{
    $numbers[] = (int)$a->number;
    $positions[] = $a->position;
}

$number_string = json_encode($numbers);
$position_string = json_encode($positions);
Sign up to request clarification or add additional context in comments.

4 Comments

@Gore if I put var_dump(json_decode($string, true)); see the array, but with your code I have a blank page
Yes. I forgot that json_decode return a array of Objects. :/ @user1147636
@Gore . Ok now number print right, but position_string return Null
@Gore You lost "s" in $position ....but this code works correctly! Many thanks to all.please correct it for other users.

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.