将数组值拆分为不同的div [英] split array values into different divs

查看:50
本文介绍了将数组值拆分为不同的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由高级自定义字段生成的php数组.这是我的PHP代码:

I have a php array generated by Advanced custom field. here is my php code:

<?php
$rows = get_field('nous_suivre',11 ); // get all the rows
print_r($rows)
?>

当我print_r我的数组时,这就是我得到的:

when I print_r my array, here is what I get:

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
        )

)

我想做的是将我的数组内容拆分为多个单独的div,而无需增加.

what I'm trying to do is to split my array content into separate divs, without incrementation.

这就是我想要得到的:

<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>

事实上,我需要在需要行值的地方回显.在网上阅读时,我尝试例如获取"Facebook",

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:

<?php echo $row[0]->[nom]; ?> // get value "nom" from first row of "$row" table; 

但是它不起作用.

推荐答案

尝试一下:

$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>';
}

结果:

Facebook
Twitter
Instagram

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>



编辑(在评论后):

$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);

结果:

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)

这篇关于将数组值拆分为不同的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆