1

I have json and I want to remove 1st attribute from it but don't know how. Here is my json:

["car",["Nissan","Chevrolet","Ford"]]

I want to show it like this:

["Nissan","Chevrolet","Ford"]

my Php

$url = 'http://example.com/json?=car';
$content = file_get_contents($url);
echo $content;
1
  • not really an "attribute"... Commented Oct 19, 2014 at 6:00

3 Answers 3

1

You need to convert it into an array first, point it to that particular index you want then encode again. (Assuming that json string on your question is really the exact json string you have).

$contents = '["car",["Nissan","Chevrolet","Ford"]]';
$data = json_decode($contents, true);
$contents = json_encode($data[1]);
echo $contents; // ["Nissan","Chevrolet","Ford"]
Sign up to request clarification or add additional context in comments.

4 Comments

I am getting this error Warning: reset() expects parameter 1 to be array, null given
@osisboris try my revision, are you sure thats your only json? or a part of it?
answer null. it is same as I showed above just value changes
Hi it is working fine. It was the typo $content , $contents . Thanks buddy.
1

Looks like you want to just extract array.

json_decode($content[1])

1 Comment

If you need still JSON string, not an array — you need @Ghost answer
0
$json_array = json_decode(file_get_contents($url)); //converts json data to array

echo $json_array[1]; //displays ["Nissan","Chevrolet","Ford"]

//contents of json_array
 /* Array
  (
    [0] => car
    [1] => Array
      (
        [0] => Nissan
        [1] => Chevrolet
        [2] => Ford
    )

  ) */

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.