skip to Main Content

I’m Getting some arrays from some wordpress custom fields:

$content = array(get_post_meta($postId, 'content'));
$media = array(get_post_meta($postId, 'media'));
$yt = array(get_post_meta($postId, 'youtube'));

I then need to have it printing in sequence, like:

media

content

LInk

Embed

And repeat the sequence for each value

media

content

LInk

Embed

For the sequence I’d use this:

echo '<ul>';
for ($i = 0; $i < count($all_array['media']); $i++) {
    for ($j = 0; $j < count($all_array['content']); $j++) {
        for ($k = 0; $k < count($all_array['youtube']); $k++) {
            echo '<li>media->' . $all_array['media'][$i] . '</li>';
            echo '<li>content->' . $all_array['content'][$j] . '</li>';
            echo '<li>link->' . $all_array['link'][$k] . '</li>';
        }
    }
}
echo '</ul>';

But I’m doing something wrong with the merging of the 3 fields as if I do a var_dump before to run the for bit, like

echo '<pre>' . var_export($all_array, true) . '</pre>';

Then this is what I get and I cannot iterate as I wish:

array (
  0 => 
  array (
    0 => 
    array (
      0 => '
brother

',
      1 => '
Lorem

',
      2 => '
End it

',
    ),
    1 => 
    array (
      0 => '337',
      1 => '339',
    ),
    2 => 
    array (
      0 => 'https://www.youtube.com/watch?v=94q6fzbJUfg',
    ),
  ),
)

Literally the layout in html that I’m looking for is:

  1. image
  2. content
  3. link
  4. image
  5. content
  6. link

UPDATE

This how I am merging the arrays:

foreach ( $content as $idx => $val ) {
  $all_array[] = [ $val, $media[$idx], $yt[$idx] ];
}

This is the associative array how it looks like:

Content:

    array (
      0 => 
      array (
        0 => '
    brother
    
    ',
        1 => '
    Lorem
    
    ',
        2 => '
    End it
    
    ',
      ),
    )

Media

    array (
      0 => 
      array (
        0 => '337',
        1 => '339',
      ),
    )

Youtube

    array (
      0 => 
      array (
        0 => 'https://www.youtube.com/watch?v=94q6fzbJUfg',
      ),
    )

2

Answers


  1. Chosen as BEST ANSWER

    The way I've resolved it is first I calculate the tot. items within each array, then I get the array with max items and loop and add the items in sequence:

    //GET CUSTOM FIELDS
        $content = get_post_meta($post_to_edit->ID, 'content', false);
        $media = get_post_meta($post_to_edit->ID, 'media', false);
        $yt = get_post_meta($post_to_edit->ID, 'youtube', false);
        $max = max(count($content), count($media), count($yt));
        $combined = [];
    //
    // CREATE CUSTOM FIELDS UNIQUE ARRAY
    for($i = 0; $i <= $max; $i++) {
        if(isset($media[$i])) {
            $combined[] = ["type" => "media", "value" => $media[$i]];
        }
        if(isset($content[$i])) {
            $combined[] = ["type" => "content", "value" => $content[$i]];
        }
        if(isset($yt[$i])) {
            $combined[] = ["type" => "youtube", "value" => $yt[$i]];
        } 
    }
    

    Finally I can loop:

    foreach ($combined as $key => $val) {
       if($val['type'] === "media") {
         ...
       }
       if($val['type'] === "content") {
    ...
    

  2. You don’t need to merge the arrays together. It will work fine in separate arrays. However, your for loops don’t have the right logic. Try:

    for ($i = 0; $i < count($media); $i++) {
        for ($j = 0; $j < count($media[$i]); $j++) {
            echo '<li>media->' . $media[$i][$j] . '</li>';
        }
        for ($j = 0; $j < count($content[$i]); $j++) {
            echo '<li>content->' . $content[$i][$j] . '</li>';
        }
        for ($j = 0; $j < count($youtube[$i]); $j++) {
            echo '<li>link->' . $youtube[$i][$j] . '</li>';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search