[英]split array values into different divs
I have a php array generated by Advanced custom field. 我有一个由高级自定义字段生成的php数组。 here is my php code: 这是我的PHP代码:
<?php
$rows = get_field('nous_suivre',11 ); // get all the rows
print_r($rows)
?>
when I print_r my array, here is what I get: 当我使用数组print_r时,得到的是:
Array
(
[0] => Array
(
[nom] => Facebook
[lien] => http://www.facebook.com/ID
)
[1] => Array
(
[nom] => Twitter
[lien] => http://www.twitter.com/ID
)
[2] => Array
(
[nom] => Instagram
[lien] => http://www.instagram.com/ID
)
)
what I'm trying to do is to split my array content into separate divs, without incrementation. 我想做的是将数组内容拆分为单独的div,而无需增加。
here is what I'm trying to get: 这是我想要得到的:
<div id="1"><a href="http://www.facebook.com/ID">Facebook<a></div> qsdjqslkdqjkg qsdhjqsd <div id="2"><a href="http://www.instagram.com/ID">Instagram<a></div>
div id="8"><a href="http://www.twitter.com/ID">Twitter<a></div>
in fact I need to echo wherever I want values from rows. 实际上,无论何时需要行值,我都需要回显。 when reading on the net, I tried for example to get "Facebook", this: 在网上阅读时,我尝试例如获取“ Facebook”,例如:
<?php echo $row[0]->[nom]; ?> // get value "nom" from first row of "$row" table;
but it's not working. 但它不起作用。
Try this: 尝试这个:
$data = array(
array(
'nom'=>'Facebook',
'lien'=>'http://www.facebook.com/ID'
),
array(
'nom'=>'Twitter',
'lien'=>'http://www.twitter.com/ID'
),
array(
'nom'=>'Instagram',
'lien'=>'http://www.instagram.com/ID'
),
);
foreach($data as $key => $value) {
echo '<div id="'.($key+1).'"><a href="'.$value['lien'].'">'.$value['nom'].'</a></div>';
}
Result: 结果:
Facebook
Twitter
Instagram
HTML Result : HTML结果:
<div id="1"><a href="http://www.facebook.com/ID">Facebook</a></div>
<div id="2"><a href="http://www.twitter.com/ID">Twitter</a></div>
<div id="3"><a href="http://www.instagram.com/ID">Instagram</a></div>
EDIT (after the comments): 编辑 (在评论后):
$new_data = array();
foreach($data as $key => $value) {
$new_data[$value['nom']] = $value;
}
echo $new_data['Facebook']['nom'].' => '.$new_data['Facebook']['lien'].'<br />';
var_dump($new_data);
Result: 结果:
Facebook => http://www.facebook.com/ID
array (size=3)
'Facebook' =>
array (size=2)
'nom' => string 'Facebook' (length=8)
'lien' => string 'http://www.facebook.com/ID' (length=26)
'Twitter' =>
array (size=2)
'nom' => string 'Twitter' (length=7)
'lien' => string 'http://www.twitter.com/ID' (length=25)
'Instagram' =>
array (size=2)
'nom' => string 'Instagram' (length=9)
'lien' => string 'http://www.instagram.com/ID' (length=27)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.