Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组中组的HTML标记_Php_Html_Arrays - Fatal编程技术网

PHP数组中组的HTML标记

PHP数组中组的HTML标记,php,html,arrays,Php,Html,Arrays,借用别人的问题 我有一个数组 Array ( [0] => Array ( [id] => 95, [shipping_no] => 212755-1 ) [1] => Array ( [id] => 96, [shipping_no] => 212755-1 ) [2] => Array ( [id] => 97,

借用别人的问题

我有一个数组

Array
(
[0] => Array
    (
        [id] => 95,
        [shipping_no] => 212755-1
    )

[1] => Array
    (
        [id] => 96,
        [shipping_no] => 212755-1
    )

[2] => Array
    (
        [id] => 97,
        [shipping_no] => 212755-2
    )

)`
我需要输出,以便它出现

<div class="shipping-no">
 Shipping No: 212755-1
 ID: 95
 ID: 96
</div>
<div class="shipping-no">
 Shipping No: 212755-2
 ID: 97
</div>

装运编号:212755-1
身份证号码:95
身份证号码:96
装运编号:212755-2
身份证号码:97
使用
foreach
单步执行,排序很好。我遇到的“问题”是,每次迭代的顶部都会进行装运编号是否与上一次相同的测试,因此迭代的第一行是
if($last\u shipping\u no!=$this\u shipping\u no){echo'}

这对我来说似乎很笨拙,而且我必须在循环之前写第一个开始标记,在循环之后写最后一个结束标记


做这件事的优雅方式是什么?

如果你想采用mvc方法,我建议你首先对数据进行记录

if(count($array) == count(array_unique($array))){ 
  //shipping-no is unique 
}else{ 
  echo 'The following Shipping-no are not unique:'; 
  var_dump(array_diff($array,array_unique($array))); 

 // here you search about The key that are not unique with for or foreach 
}  
它可以是一个命令,将shipping_no作为id的键和值数组


建议您从自己的角度尽可能多地了解业务逻辑。因此,如果您折射输入的数据(数组),您将不必担心如何显示它,我的意思是至少不必使用混合了html的php。

正如@davicumu所建议的,我将根据唯一标识符重新索引数据。在这种情况下,它似乎是
装运号
。这可以通过一个简单的
foreach
实现,如下所示:

$indexed = array();
foreach ($shippings as $s) {
    # if the key does not exist create it
    if (!isset($indexed, $s["shipping_no"])) {
        $indexed[$s["shipping_no"]] = array();
        $indexed[$s["shipping_no"]]["ids"] = array();
    }

    # In any case append the ID
    $indexed[$s["shipping_no"]]["ids"][] = $s["id"];
}
结果如下:

array(2) {
  ["212755-1"]=>
  array(1) {
    ["ids"]=>
    array(2) {
      [0]=>
      int(95)
      [1]=>
      int(96)
    }
  }
  ["212755-2"]=>
  array(1) {
    ["ids"]=>
    array(1) {
      [0]=>
      int(97)
    }
  }
}
如您所见,装运订单由唯一标识符索引到数组中。每个都有一个数组/列表id。这允许将来扩展,即在不更改数据结构的情况下向
发货添加新属性

然后,打印代码变得微不足道:

foreach ($indexed as $k=>$v) {
    # Open the shipping-no div
    print("<div class='shipping-no'>\n");

    # Print shipping attributes
    print("Shifting No: $k\n");

    # Loop and print its IDs
    foreach ($v["ids"] as $id) {
        print("ID: $id\n");
    }

    # Close the shipping-no div
    print("</div>\n");
}
foreach($k=>v){
#打开shipping no div
打印(“\n”);
#打印装运属性
打印(“移动编号:$k\n”);
#循环并打印其ID
foreach($v[“ids”]作为$id){
打印(“ID:$ID\n”);
}
#关闭装运编号div
打印(“\n”);
}

希望它有助于

统一解决方案,以解决阵列中无序元素的更复杂情况:

$arr = array(
    array(
        'id' => 98,
        'shipping_no' => '212755-2'
    ),
    array(
        'id' => 95,
        'shipping_no' => '212755-1'
    ),
    array(
        'id' => 96,
        'shipping_no' => '212755-1'
    ),
    array(
        'id' => 97,
        'shipping_no' => '212755-2'
    ),
    array(
        'id' => 99,
        'shipping_no' => '212755-3'
    )
);

uasort($arr, function($a,$b){
    return ($a['shipping_no'] < $b['shipping_no'])? -1 : (($a['shipping_no'] > $b['shipping_no'])? 1 : 0);     
});

$result = [];
foreach ($arr as $value) {
    if (!isset($result[$value['shipping_no']])){
        $result[$value['shipping_no']] = [$value['id']];
    } else {
        $result[$value['shipping_no']][] = $value['id'];
    }
    sort($result[$value['shipping_no']], SORT_NUMERIC);
}

foreach ($result as $key => $value) {
    $ids = (count($value) > 1)? implode("<br>ID: ", $value) : $value[0];
    echo '<div class="shipping-no">'.
        "Shipping No: $key <br> ID: ". $ids;
}
$arr=array(
排列(
“id”=>98,
“装运编号”=>“212755-2”
),
排列(
'id'=>95,
“装运编号”=>“212755-1”
),
排列(
“id”=>96,
“装运编号”=>“212755-1”
),
排列(
'id'=>97,
“装运编号”=>“212755-2”
),
排列(
'id'=>99,
“装运编号”=>“212755-3”
)
);
uasort($arr,函数($a,$b){
退货($a['shipping\u no']<$b['shipping\u no'])?-1:($a['shipping\u no']>$b['shipping\u no'])?1:0);
});
$result=[];
foreach($arr作为$value){
如果(!isset($result[$value['shipping_no']])){
$result[$value['shipping_no']]=[$value['id']];
}否则{
$result[$value['shipping_no'][]=$value['id'];
}
排序($result[$value['shipping\u no']],sort\u NUMERIC);
}
foreach($结果为$key=>$value){
$ID=(计数($value)>1)?内爆(
ID:,$value):$value[0]; 回显“”。 “装运编号:$key
ID:”.$ID; }

Shipping No:212755-1
ID:95
ID:96 Shipping No:212755-2
ID:97
ID:98 Shipping No:212755-3
ID:99
。感觉这些数据来自数据库,可以通过发送\ u no进行分组。无论如何,我认为您应该在生成html之前在php代码中进行分组。在生成html之前进行分组听起来不错。我该怎么做?那么foreach是如何工作的呢?我需要嵌套循环吗?对不起!这一点直接超出了我的头脑(尽管我理解你的意思)-你能指出一个我可以从中了解这一点的资源吗?当然,克里斯,有很多,但你可以使用这个资源。任何mvc的解释都是可行的——我和你分享的解释中有php——这看起来就像是一条路要走(除了在SQL中做同样的事情)——我会学习的。目前它比我的工资等级略高,但这是我喜欢的方式。谢谢。如果你从SQL数据库加载这些数据,你也可以实现一个或使用某种ORM来为你做这项工作(取决于你正在使用ORM的表的数量,这可能是过分的-例如),这只是为了提醒走这条路的任何人(因为我花了一段时间来解决);在现实世界中,“id”显然是另一个数组中的一个值,因此您可以将数组加载到此代码
$indexed[$s[“shipping\u no”]][“ids”[]=$s
然后在打印中使用
$id['id']、$id['some\u other\u key']
等访问值