0

I need to get equal cloums for different arrays for list view. For example, I have posts in a list view, some of them have 5 values but some 3 or 4.

I want to replace all non-existing $keys=>$val[0] to "-".

And get table like this.

ID ; TITLE2 ; TITLE3 ; TITLE4 ; TITLE5

ROW1 ; ROW2 ; ROW3 ; ROW4 ; ROW5

ROW1 ; ROW2 ; ROW3 ; ROW4 ; ---------

ROW1 ; ROW2 ; ROW3 ; --------- ; ---------

ROW1 ; ROW2 ; ROW3 ; ROW4 ; ROW5

And after loop table is broken. Here is a code for getting a values from post_meta table.

foreach($meta_values as $key=>$val)
{

echo  '<td>'.$val[0].'</td>';

}
2
  • If I think what you're thinking then just use an inline conditional? - ($val[$x] == null ? '----' : $val[$x]) Commented Sep 13, 2018 at 13:07
  • It is absolutely unclear what you are trying to do. Post error which you have, full code what you use and make a more clear title. Commented Sep 13, 2018 at 13:19

1 Answer 1

2

First of all you need to know max elements in row, for this you can count elements in header. Then you can interate through row and check for non-existing keys (empty($val[$i]) and change them to " ---------"

//If first element is header 
$length = count($meta_values[0]);

foreach($meta_values as $val) {
    foreach ($i=0; $i < $length; $i++) {
        if (empty($val[$i])) {
           $val[$i] = " ---------";
        }
    } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Also $meta_value has lots of values what I don't need, I have used this condition to filter them in a loop if (substr($key, 0, 1) != '_' && $key !== 'um_content_restriction') but how can I do it know? Count() function will give me number of all keys in array included keys what I filtered.
@Hazzy then first you have to filter array and then do what I answered above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.