1

I am querying a service if a person have phone number(s) (also maybe not). I have a json string (as return value) like the following:

$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';

I convert this string to json_decode() function.

$jd = json_decode($json);

Then I want to get the phone numbers only into an array without keys.

if($jd->found) {
    $o2a = get_object_vars($json);

}
var_dump($o2a);

When I want to see what $o2a holds with var_dump() function, I get the following:

array (size=2)
    'data' => 
        array (size=2)
            0 => 
                object(stdClass)[2]
                public 'tel1' => string '1219' (length=4)
            1 => 
                object(stdClass)[3]
                public 'tel2' => string '2710' (length=4)
    'found' => boolean true

I want to get only the phone numbers into an array at the end like:

$phones = array('1219', '2710');

What makes me stop doing this is that I do not know how many phone numbers one can have. Json array could consist of more or less elements.

$possibleJson1 = '{"data":[],"found":false}'; //no phone number found
$possibleJson2 = '{"data":[{"tel1":"1102"},{"tel2":"3220"},{"tel3":"1112"},{"tel4":"3230"}],"found":true}'; //4 phone numbers found

It may vary 0-to-n, so if it was a constant number I could create that array within a loop.

2
  • 1
    If you pass true for the second parameter (json_decode($json, true);), it'll decode JavaScript objects to an associative array that you can iterate more easily. Commented Jun 23, 2015 at 12:20
  • 1
    You don't need to know the exact number, like in other languages... In PHP you can use a foreach-loop on the data-key given, that it only contains phone numbers. Commented Jun 23, 2015 at 12:20

5 Answers 5

2

Some functions without any code :)

$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';

$vals = array_values(array_reduce(json_decode($json, true)['data'], 'array_merge',[]));
var_dump($vals);
Sign up to request clarification or add additional context in comments.

1 Comment

Notice: Use of undefined constant array_merge - assumed 'array_merge'
2

Convert it into an array and then you should be able to iterate it easily

$jd = json_decode($json, true);
$phones = array();
if(isset($jd['data']) && $jd['found']) {
    foreach($jd['data'] as $key => $val) $phones[] = $val;
}

1 Comment

This also works well but I chose @splash58's answer because it has not loop
1
  • Instead of handling with an object, use the second parameter of the json_decode function so it would returned an array.
  • Check if the data and found keys exist.
  • Since you don't know what are the keys names, you can use array_values
  • Demo

.

$jd = json_decode($json, true);
if(isset($jd['data']) && isset($jd['found'])){
  $telArr = $jd['data'];

  $phones = array();
  foreach($telArr as $tel){
    $value = array_values($tel);
    $phones[] = $value[0];
  }

  var_dump($phones);
}

Output:

array(2) {
  [0]=>
  string(4) "1102"
  [1]=>
  string(4) "3220"
}

Comments

0

Well, I would try something like that:

$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';
$jd = json_decode($json);
$phones = [];

if ($jd->found && count($jd->data)) {
    foreach ($jd->data as $key -> $value) {
        $phones[] = $value;
    }
}

Comments

0

Try as using in_array and simple foreach loop

$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';
$arr = json_decode($json, true);
$result = array();
if (in_array(true, $arr)) {
    foreach ($arr['data'] as $key => $value) {
        foreach($value as $k => $v)
            $result[] = $v;
    }
}
print_r($result);

Fiddle

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.