[英]How to loop through an array in php with formatted html design?
I want to loop through an array with formatted HTML that set up before.我想循环使用之前设置的格式化 HTML 的数组。 For ex:
例如:
$display_format = "%address%<br>%zipcode% %city%<br>€ %price% / %m2% m2"
And I have an array like this:我有一个这样的数组:
$array_display = array (array ( 'address' => 'Prins Hendriklaan 15','zipcode' => '1075', 'city' => 'Amsterdam', 'price' => 6750000, 'm2' => 428 ), array ( 'address' => 'Huizingalaan', 'zipcode' =>'187 189-191', 'city'=> Amsterdam, 'price' => 3000000, 'm2' => 1.155) )
so I want to loop through $array_display and display each row into the formated design from $display_format.所以我想循环遍历 $array_display 并将每一行显示到 $display_format 的格式化设计中。 And most importantly, when I update the order or add more field or format of $display_format then it would be changed accordingly from the loop of array, no hardcode when looping through array for fixed field order( this is the most difficult part).
最重要的是,当我更新顺序或添加更多 $display_format 的字段或格式时,它将从数组循环中相应地更改,在循环数组以获取固定字段顺序时没有硬代码(这是最困难的部分)。
Can anybody help me to do this?任何人都可以帮我做到这一点吗?
Thanks a lot.非常感谢。
After correcting the small mistook about the unquote value - Amsterdam
'-) you can use preg_replace
with a pattern based upon the array keys found in the source data ($array_display)在纠正了关于 unquote 值的小错误 -
Amsterdam
'-) 之后,您可以使用preg_replace
和基于源数据中找到的数组键的模式 ($array_display)
<?php
$array_display = array (
array (
'address' => 'Prins Hendriklaan 15',
'zipcode' => '1075',
'city' => 'Amsterdam',
'price' => 6750000,
'm2' => 428
),
array (
'address' => 'Huizingalaan',
'zipcode' => '187 189-191',
'city' => 'Amsterdam',
'price' => 3000000,
'm2' => 1.155
)
);
$display_format = "%address%<br>%zipcode% %city%<br>€ %price% / %m2% m2";
foreach( $array_display as $i=>$arr ){
$keys=array_keys( $arr );
$tmp=$display_format;
foreach( $keys as $key ){
$pttn=sprintf('@%%%s%%@',$key);
$tmp=preg_replace($pttn,$arr[$key],$tmp);
}
echo $tmp;
}
?>
Which yields:产生:
Prins Hendriklaan 15
1075 Amsterdam
€ 6750000 / 428 m2Huizingalaan
187 189-191 Amsterdam
€ 3000000 / 1.155 m2
There's a typo in your array data 'city' => Amsterdam
, fixed that to 'city' => 'Amsterdam'
.您的数组数据
'city' => Amsterdam
中有错字,已将其修复为'city' => 'Amsterdam'
。
Loop through the array, $placeholders = array_map(function($value) { return "%$value%"; }, array_keys($display));
遍历数组,
$placeholders = array_map(function($value) { return "%$value%"; }, array_keys($display));
will create a list of values to replace from your array keys ( %address%
, %city%
...), then replace them with the actual values from the arrays.将从您的数组键(
%address%
、 %city%
...)中创建要替换的值列表,然后用 arrays 中的实际值替换它们。
This code does not check for existence of the "placeholders" in your $display_format
此代码不检查
$display_format
中是否存在“占位符”
$display_format = "%address%<br>%zipcode% %city%<br>€ %price% / %m2% m2";
$array_display = array (array ( 'address' => 'Prins Hendriklaan 15','zipcode' => '1075', 'city' => 'Amsterdam', 'price' => 6750000, 'm2' => 428 ), array ( 'address' => 'Huizingalaan', 'zipcode' =>'187 189-191', 'city'=> 'Amsterdam', 'price' => 3000000, 'm2' => 1.155) );
foreach ($array_display as $display) {
$placeholders = array_map(function($value) { return "%$value%"; }, array_keys($display));
echo str_replace($placeholders, $display, $display_format);
}
<?php
$array_display = array( array ( 'address' => 'Prins Hendriklaan 15','zipcode' => '1075', 'city' => 'Amsterdam', 'price' => 6750000, 'm2' => 428 ), array ( 'address' => 'Huizingalaan', 'zipcode' =>'187 189-191', 'city'=> 'Amsterdam', 'price' => 3000000, 'm2' => 1.155) );
foreach($array_display as $displayItem) {
$display_format = "{$displayItem['address']}<br>{$displayItem['zipcode']} {$displayItem['city']}<br>€ {$displayItem['price']} / {$displayItem['m2']} m2";
}
Edit: made a solution that didn't work编辑:提出了一个不起作用的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.