将php数组的输出排序为元素
我已经被这个问题困扰了好几天了,但是什么都不能用。我在网上发现了许多关于使用各种方法的相互矛盾的帖子,这些方法在php7中要慢得多,而且有更新的、更好的方法可以使用。我的目标服务器是PHP7,这样就可以了 我有以下问题:将php数组的输出排序为元素,php,arrays,Php,Arrays,我已经被这个问题困扰了好几天了,但是什么都不能用。我在网上发现了许多关于使用各种方法的相互矛盾的帖子,这些方法在php7中要慢得多,而且有更新的、更好的方法可以使用。我的目标服务器是PHP7,这样就可以了 我有以下问题: object(SingleClass)#463 (8) { ["id"]=> string(1) "1" [created_date]=> string(19) "2020-06-25 17:50:0
object(SingleClass)#463 (8) {
["id"]=>
string(1) "1"
[created_date]=>
string(19) "2020-06-25 17:50:00"
[cricket]=>
string(27) "{"data":{"example":"data"}}"
[rugby]=>
string(27) "{"data":{"example":"data"}}"
[football]=>
string(27) "{"data":{"example":"data"}}"
[tennis]=>
string(27) "{"data":{"example":"data"}}"
[swimming]=>
string(27) "{"data":{"example":"data"}}"
[order]=>
string(60) "{"cricket":1,"football":2,"rugby":3,"swimming":4,"tennis":5}"
}
在我看来,我可以访问这个对象。我需要根据[order]
数组值对上述各项的输出进行排序,然后打印对象。因此,在本例中,输出可以忽略一些类似于id和created_date的内容,但应该类似于:
echo `cricket` data
echo `football` data
echo `rugby` data
echo `swimming` data
echo `tennis` data
上面的输出是$this->singleitem
的打印,\r
,因此我可以像这样访问每个对象:
<?php $orders = json_decode($this->singleitem->order);?>
<?php foreach ($orders as $itemOrder) {
print_r($itemOrder);
} ?>
如果[order]为空,或者缺少一个值,它应该跳过相应的缺少项并将其追加到末尾,这在php中可能吗
谢谢
顺序
,并按数值排序(以防它们并不总是按顺序排列)order
数组的键,并从singleitem
$order=json_decode($this->singleitem->order,true);//“true”生成关联数组
asort($order);//按值排序,维护键
foreach(数组_键($order)作为$prop){
$val=json_decode($this->singleitem->$prop);
echo$val->data->example,PHP_EOL;
}
演示~这可能无法完全按照预期工作,因为我使用的数据结构可能略有不同,但它应该会让您对如何操作有一个很好的了解
<?php
$singleitem = '
{
"id": 1,
"created_date": "2020-06-25 17:50:00",
"cricket": {
"data": {
"example": "data"
}
},
"rugby": {
"data": {
"example": "data"
}
},
"football": {
"data": {
"example": "data"
}
},
"tennis": {
"data": {
"example": "data"
}
},
"swimming": {
"data": {
"example": "data"
}
},
"order": {
"cricket": 1,
"rugby": 3,
"swimming": 4,
"tennis": 5
}
}
';
$json = json_decode($singleitem, true); // true turns the data into an assoc array
$sport_order = array_keys($json['order']);
// list all sport data in order
foreach ($sport_order as $sport) {
echo $sport . json_encode($json[$sport]['data']) . "<br>";
}
// list all sport data, that is not in sort list, last
// eg in this example football should be listed last
foreach ($json as $key => $val) {
// idealy we should have a list of valid values, rather then excluding id, order and created_date,
// unless we are sure there will never be any further meta data added
// ignore id
if ($key === 'id') {
continue;
}
// ignore order
if ($key === 'order') {
continue;
}
// ignore created_date
if ($key === 'created_date') {
continue;
}
if (in_array($key, $sport_order)) {
continue;
}
echo $key . json_encode($val) . "<br>";
}
这在php中是可能的。您展示的数据结构不是json,也不是php,它是什么?[]是指那种格式的数组吗?我复制/粘贴正确内容的能力太差了。我用对象的var_转储更新了OP中的代码。谢谢你能回显(string)$this->singleitem,然后在注释中复制/粘贴原始json,这样我就不必创建相同的数据结构了。我试过了,它打印出来:SingleClass
,所以我做了每一个,比如$this->singleitem->order,它在字符串(60)减去引号后给了我与上面完全相同的字符串,例如{“cricket”:1,“足球”:2,“橄榄球”:3,“游泳”:4,“网球”:5}
这项工作做得很好。你是个英雄!!很好的方法,谢谢。这让我可以轻松决定要显示哪些对象以及可以忽略哪些对象。再次感谢。
<?php
$singleitem = '
{
"id": 1,
"created_date": "2020-06-25 17:50:00",
"cricket": {
"data": {
"example": "data"
}
},
"rugby": {
"data": {
"example": "data"
}
},
"football": {
"data": {
"example": "data"
}
},
"tennis": {
"data": {
"example": "data"
}
},
"swimming": {
"data": {
"example": "data"
}
},
"order": {
"cricket": 1,
"rugby": 3,
"swimming": 4,
"tennis": 5
}
}
';
$json = json_decode($singleitem, true); // true turns the data into an assoc array
$sport_order = array_keys($json['order']);
// list all sport data in order
foreach ($sport_order as $sport) {
echo $sport . json_encode($json[$sport]['data']) . "<br>";
}
// list all sport data, that is not in sort list, last
// eg in this example football should be listed last
foreach ($json as $key => $val) {
// idealy we should have a list of valid values, rather then excluding id, order and created_date,
// unless we are sure there will never be any further meta data added
// ignore id
if ($key === 'id') {
continue;
}
// ignore order
if ($key === 'order') {
continue;
}
// ignore created_date
if ($key === 'created_date') {
continue;
}
if (in_array($key, $sport_order)) {
continue;
}
echo $key . json_encode($val) . "<br>";
}