0

I'm fetching a post meta from Wordpress CPT which's returning multiple strings form individually instead of array as shown below:

$data = $calendar->data();
foreach ($data as $event) { 
    $days = get_post_meta( $event->ID, 'sp_day', true );
    var_dump($days);
}

string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "2" string(1) "2" string(1) "2" string(1) "2" string(1) "2" string(1) "2" string(1) "3" string(1) "3" string(1) "3" string(1) "3" string(1) "3" string(1) "3" string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""

I tried to convert it into array like $days = array(get_post_meta( $event->ID, 'sp_day', true )); the dump values were:

array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }

Than I tried the array_merge() and array_unique()

None of the above steps have solved the issue. As you can see we have repeated values multiple times instead I want them unique to get each value only once such "1, 2, 3, 4, 5, etc."

Suggestions are highly appreciated

David Buik
  • 522
  • 1
  • 8
  • 31
  • Are you saying that you are calling these two lines in a loop, with varying values for $event->ID? Otherwise what you are saying does not make sense to begin with - one single function call can not return multiple strings at the same time. If this is in a loop, then you probably simply want to add new items to an _array_, so initialize $days as an empty array before the loop, and then do `$days[] = get_post_meta(…);` inside. – CBroe Oct 29 '20 at 15:19
  • https://www.php.net/manual/en/function.array-unique.php helps to get only unique values afterwards. – CBroe Oct 29 '20 at 15:20
  • Yes it's in a loop I've edited the question and showed the loop. I already tried your suggestion but it didn't work – David Buik Oct 29 '20 at 15:28
  • Show how you tried it then, please. – CBroe Oct 30 '20 at 07:19

1 Answers1

1

you can add an empty array before the for loop and inside the for loop you can check if the item exist in the array, if it doesn't exist you add the item to the array.

$data = $calendar->data();
$result = [];
foreach ($data as $event) { 
    $days = get_post_meta( $event->ID, 'sp_day', true );
    if(! in_array($days, $result)) {
        $result[] = $days;
    }

    
}
var_dump($result);

GABY KARAM
  • 359
  • 1
  • 8