如何验证是否所有嵌套的项目包括多维PHP数组相同的密钥 [英] How to verify if all nested items contain the same key in a multidimensional PHP array
问题描述
我有此数组在PHP中:
[
{
idespecialidad:001,
especialidad:ALBAÑIL,
cantidad:3,
codpais:PE,
DISTANCIA:11.9
},
{
idespecialidad:006,
especialidad:厨师,
cantidad:1,
codpais:PE,
DISTANCIA:5.3
},
{
idespecialidad:003,
especialidad:ELECTRICISTA,
cantidad:2,
codpais:PE,
DISTANCIA:7.7
},
{
idespecialidad:009,
especialidad:PROGRAMADOR,
cantidad:1,
codpais:PE
}
]
我需要验证是否每个对象具有相同的键(例如,在这种情况下,最后一个不具有 DISTANCIA
),然后返回的真或假
编辑:我想这一点:
如果(array_key_exists('DISTANCIA',$阵列)){
回声json_en code($数组);
}其他{
回声空JSON;
}
不过,这是行不通的。
在你希望的东西用数据来做,但两个选择,我相信将工作最适合你的 array_map
和的 array_filter
。
首先,设置基本的JSON例子作为一个字符串:
//设置JSON字符串的这个例子。
$ json_string =<<< EOT
[
{
idespecialidad:001,
especialidad:ALBAÑIL,
cantidad:3,
codpais:PE,
DISTANCIA:11.9
},
{
idespecialidad:006,
especialidad:厨师,
cantidad:1,
codpais:PE,
DISTANCIA:5.3
},
{
idespecialidad:003,
especialidad:ELECTRICISTA,
cantidad:2,
codpais:PE,
DISTANCIA:7.7
},
{
idespecialidad:009,
especialidad:PROGRAMADOR,
cantidad:1,
codpais:PE
}
]
EOT;
现在使用的例子 array_map
:
//德code JSON字符串作为任何数组。
$ json_string_de codeD = json_de code($ json_string,真正的);//使用array_map返回数组告诉你什么项目具有DISTANCIA。
$ contains_distancia = array_map(函数($值){
返回array_key_exists('DISTANCIA',$值);
} $ json_string_de codeD);//转储阵列查看其内容。
呼应'< pre>';
的print_r($ contains_distancia);
呼应'< / pre>';
将输出一个名为新数组 $ contains_distancia
基于该主阵列中的项目包含返回true或false DISTANCIA
:
阵列
(
[0] => 1
[1] => 1
[2] => 1
[3] =>
)
但我只是在另一个答案看到你问:
但是,如果我想返回一个
格式JSON发生了什么
刚刚与
具有德DISTANCIA关键对象?
块引用>非常容易,只要使用
array_filter
这样的例子://过滤与'array_filter'的阵列。
$ contains_distancia = array_filter($ json_string_de codeD,函数($值){
返回array_key_exists('DISTANCIA',$值);
});//转储阵列查看其内容。
呼应'< pre>';
的print_r($ contains_distancia);
呼应'< / pre>';返回的数组
$ contains_distancia
将只是与项目DISTANCIA
:阵列
(
[0] =>排列
(
[idespecialidad] => 001
[especialidad] => ALBAÃ'IL
[cantidad] => 3
[codpais] => PE
[DISTANCIA] => 11.9
) [1] =>排列
(
[idespecialidad] => 006
[especialidad] =>厨师
[cantidad] => 1
[codpais] => PE
[DISTANCIA] => 5.3
) [2] =>排列
(
[idespecialidad] => 003
[especialidad] => ELECTRICISTA
[cantidad] => 2
[codpais] => PE
[DISTANCIA] => 7.7
))然后,只需使用
json_en code
这样的$ contains_distancia
://将数组JSON。
$ contains_distancia_json = json_en code($ contains_distancia);//转储JSON来查看内容。
呼应'< pre>';
的print_r($ contains_distancia_json);
呼应'< / pre>';和输出将会是:
<$p$p><$c$c>[{\"idespecialidad\":\"001\",\"especialidad\":\"ALBA\ÑIL\",\"cantidad\":\"3\",\"codpais\":\"PE\",\"distancia\":\"11.9\"},{\"idespecialidad\":\"006\",\"especialidad\":\"CHEF\",\"cantidad\":\"1\",\"codpais\":\"PE\",\"distancia\":\"5.3\"},{\"idespecialidad\":\"003\",\"especialidad\":\"ELECTRICISTA\",\"cantidad\":\"2\",\"codpais\":\"PE\",\"distancia\":\"7.7\"}]
I have this array in php:
[ { "idespecialidad": "001", "especialidad": "ALBAÑIL", "cantidad": "3", "codpais": "PE", "distancia": "11.9" }, { "idespecialidad": "006", "especialidad": "CHEF", "cantidad": "1", "codpais": "PE", "distancia": "5.3" }, { "idespecialidad": "003", "especialidad": "ELECTRICISTA", "cantidad": "2", "codpais": "PE", "distancia": "7.7" }, { "idespecialidad": "009", "especialidad": "PROGRAMADOR", "cantidad": "1", "codpais": "PE" } ]
I need to verify if each object has the same keys (for e.g. in this case the last one doesn't have
distancia
), then return true or false.Edit: I tried this:
if (array_key_exists('distancia', $array)) { echo json_encode($array); } else { echo "a null json"; }
But it doesn't work.
解决方案Not 100% clear on what you wish to do with the data, but the two options I believe will work best for you are
array_map
andarray_filter
.First, set the basic JSON example as a string:
// Set the JSON string for this example. $json_string = <<<EOT [ { "idespecialidad": "001", "especialidad": "ALBAÑIL", "cantidad": "3", "codpais": "PE", "distancia": "11.9" }, { "idespecialidad": "006", "especialidad": "CHEF", "cantidad": "1", "codpais": "PE", "distancia": "5.3" }, { "idespecialidad": "003", "especialidad": "ELECTRICISTA", "cantidad": "2", "codpais": "PE", "distancia": "7.7" }, { "idespecialidad": "009", "especialidad": "PROGRAMADOR", "cantidad": "1", "codpais": "PE" } ] EOT;
Now an example that uses
array_map
:// Decode the JSON string as any array. $json_string_decoded = json_decode($json_string, true); // Use array_map to return an array telling you what items have 'distancia'. $contains_distancia = array_map(function($value) { return array_key_exists('distancia', $value); }, $json_string_decoded); // Dump the array to view the contents. echo '<pre>'; print_r($contains_distancia); echo '</pre>';
The output would be a new array called
$contains_distancia
which returns true or false based on which items in the main array containdistancia
:Array ( [0] => 1 [1] => 1 [2] => 1 [3] => )
But I just saw in another answer you ask:
But, what happend if i want to return a
format json
just with the objects which have de distancia key?Easy, just use
array_filter
like this example:// Filter the array with 'array_filter'. $contains_distancia = array_filter($json_string_decoded, function($value) { return array_key_exists('distancia', $value); }); // Dump the array to view the contents. echo '<pre>'; print_r($contains_distancia); echo '</pre>';
The returned array
$contains_distancia
would be just the items withdistancia
:Array ( [0] => Array ( [idespecialidad] => 001 [especialidad] => ALBAÑIL [cantidad] => 3 [codpais] => PE [distancia] => 11.9 ) [1] => Array ( [idespecialidad] => 006 [especialidad] => CHEF [cantidad] => 1 [codpais] => PE [distancia] => 5.3 ) [2] => Array ( [idespecialidad] => 003 [especialidad] => ELECTRICISTA [cantidad] => 2 [codpais] => PE [distancia] => 7.7 ) )
Then just use
json_encode
like this on$contains_distancia
:// Convert the array to JSON. $contains_distancia_json = json_encode($contains_distancia); // Dump the json to view the contents. echo '<pre>'; print_r($contains_distancia_json); echo '</pre>';
And the output would be:
[{"idespecialidad":"001","especialidad":"ALBA\u00d1IL","cantidad":"3","codpais":"PE","distancia":"11.9"},{"idespecialidad":"006","especialidad":"CHEF","cantidad":"1","codpais":"PE","distancia":"5.3"},{"idespecialidad":"003","especialidad":"ELECTRICISTA","cantidad":"2","codpais":"PE","distancia":"7.7"}]
这篇关于如何验证是否所有嵌套的项目包括多维PHP数组相同的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!