php - How do I get key name on this array loop?
I have 2 keys:
- content
- media
I then run this:
$fields = get_post_meta($postId);
unset($fields['_edit_lock'], $fields['_edit_last'], $fields['_thumbnail_id']);
echo '<ul>';
foreach($fields as $i) {
foreach($i as $key => $value) {
echo '<li>'.$i.'->'.$value.'</li>';
}
}
That gives me:
- Array-> 327
- Array-> Custom test 1
- Array-> Custom test 2
What I'm looking for is:
- Media-> 327
- Content-> Custom test 1
- Content-> Custom test 2
Answer
Solution:
Like this:
foreach ((array) get_post_meta($postId) as $arrayKey => $arrayValue) {
if (!in_array($arrayKey, array("_edit_lock", "_edit_last", "_thumbnail_id"))) {
foreach ($arrayValue as $key => $value) {
echo '<li>' . $arrayKey . '->' . $value . '</li>';
}
}
}
If you are in a scenario like this:
You have a array like this:
$fields = array(
'media' => array(
0 => '327',
),
'content' => array(
0 => 'Custom test 1',
1 => 'Custom test 2',
),
'link' => array(
0 => 'Custom test 10',
1 => 'Custom test 20',
2 => 'Custom test 30',
3 => 'Custom test 40',
),
'embed' => array(
0 => 'Custom test 100',
1 => 'Custom test 200',
2 => 'Custom test 300',
),
'_edit_lock' => array(
0 => '1667908749:1',
),
);
You just want these keys:
media
content
LInk
Embed
I need to output their values in sequence as per this example:
media
content
link
embed
New rows....
media
content
link
embed
You want to get something like this:
media->327
content->Custom test 1
link->Custom test 10
embed->Custom test 100
media->327
content->Custom test 1
link->Custom test 10
embed->Custom test 200
media->327
content->Custom test 1
link->Custom test 10
embed->Custom test 300
media->327
content->Custom test 1
link->Custom test 20
embed->Custom test 100
media->327
content->Custom test 1
link->Custom test 20
embed->Custom test 200
media->327
content->Custom test 1
link->Custom test 20
embed->Custom test 300
media->327
content->Custom test 1
link->Custom test 30
embed->Custom test 100
media->327
content->Custom test 1
link->Custom test 30
embed->Custom test 200
media->327
content->Custom test 1
link->Custom test 30
embed->Custom test 300
media->327
content->Custom test 1
link->Custom test 40
embed->Custom test 100
media->327
content->Custom test 1
link->Custom test 40
embed->Custom test 200
media->327
content->Custom test 1
link->Custom test 40
embed->Custom test 300
media->327
content->Custom test 2
link->Custom test 10
embed->Custom test 100
media->327
content->Custom test 2
link->Custom test 10
embed->Custom test 200
media->327
content->Custom test 2
link->Custom test 10
embed->Custom test 300
media->327
content->Custom test 2
link->Custom test 20
embed->Custom test 100
media->327
content->Custom test 2
link->Custom test 20
embed->Custom test 200
media->327
content->Custom test 2
link->Custom test 20
embed->Custom test 300
media->327
content->Custom test 2
link->Custom test 30
embed->Custom test 100
media->327
content->Custom test 2
link->Custom test 30
embed->Custom test 200
media->327
content->Custom test 2
link->Custom test 30
embed->Custom test 300
media->327
content->Custom test 2
link->Custom test 40
embed->Custom test 100
media->327
content->Custom test 2
link->Custom test 40
embed->Custom test 200
media->327
content->Custom test 2
link->Custom test 40
embed->Custom test 300
So do this:
echo '<ul>';
for ($i = 0; $i < count($fields['media']); $i++) {
for ($j = 0; $j < count($fields['content']); $j++) {
for ($k = 0; $k < count($fields['link']); $k++) {
for ($z = 0; $z < count($fields['embed']); $z++) {
echo '<li>media->' . $fields['media'][$i] . '</li>';
echo '<li>content->' . $fields['content'][$j] . '</li>';
echo '<li>link->' . $fields['link'][$k] . '</li>';
echo '<li>embed->' . $fields['embed'][$z] . '</li>';
}
}
}
}
echo '</ul>';
Source