问题
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)
?>
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
)
)
what I'm trying to do is to split my array content into separate divs, without incrementation.
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:
<?php echo $row[0]->[nom]; ?> // get value "nom" from first row of "$row" table;
but it's not working.
回答1:
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 :
<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)
来源:https://stackoverflow.com/questions/33595025/split-array-values-into-different-divs