找到你要的答案

Q:PHP how to extract info from json string wtihout knowing the keys

Q:PHP如何提取信息从JSON字符串如果知道密钥

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.

我查询一个服务,如果一个人有电话号码(s)(也可能不是)。我有一个JSON字符串(如返回值)如下:

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

我将这个字符串转换为json_decode()功能。

$jd = json_decode($json);

然后我只想把电话号码变成一个没有按键的数组。

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

}
var_dump($o2a);

当我想看什么var_dump()功能持有美元的O2A,我得到以下:

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

我想得到的电话号码只有一个数组在年底喜欢:

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

是什么让我停止这样做是,我不知道有多少电话号码可以有。JSON数组可以包含更多或更少的元素。

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

它可能会有所不同0-to-n,所以如果这是一个恒定的数字我可以创建在一个环形阵列。

answer1: 回答1:

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);

一些没有任何代码的函数:

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

$vals = array_values(array_reduce(json_decode($json, true)['data'], 'array_merge',[]));
var_dump($vals);
answer2: 回答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;
}

将它转换成一个数组,那么你应该能够很容易的进行

$jd = json_decode($json, true);
$phones = array();
if(isset($jd['data']) && $jd['found']) {
    foreach($jd['data'] as $key => $val) $phones[] = $val;
}
answer3: 回答3:
  • 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"
}
  • 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);
}

输出:

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

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;
    }
}

嗯,我会尝试这样的东西:

$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;
    }
}
answer5: 回答5:

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

尝试使用in_array简单foreach循环

$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);

小提琴

php  json