0

I have four arrays that I get from the database unserialize inside a loop, and than get a single value from a loop inside this loop :) Code below:

<?php

// MONTH
$eduGraduationDateMonth = get_post_custom_values('rbeduendmonth');
// YEAR
$eduGraduationDateYear = get_post_custom_values('rbeduendyear');
// PLACE
$eduPlace = get_post_custom_values('rbeduplace');
// FACULTY
$eduFaculty = get_post_custom_values('rbedusubject');

foreach($eduGraduationDateMonth as $eduMonthValue){
  $eduMonthUnser = unserialize($eduMonthValue);
  foreach($eduMonthUnser as $eduMonthUnserValue){
    echo $eduMonthUnserValue;
  }
}

foreach($eduGraduationDateYear as $eduYearValue){
  $eduYearUnser = unserialize($eduYearValue);
  foreach($eduYearUnser as $eduYearUnserValue){
    echo $eduYearUnserValue;
  }
}

foreach($eduPlace as $eduPlaceValue){
  $eduPlaceUnser = unserialize($eduPlaceValue);
  foreach($eduPlaceUnser as $eduPlaceUnserValue){
    echo $eduPlaceUnserValue;
  }
}

foreach($eduFaculty as $eduFacultyValue){
  $eduFacultyUnser = unserialize($eduFacultyValue);
  foreach($eduFacultyUnser as $eduFacultyUnserValue){
    echo $eduFacultyUnserValue;
  }
}

?>

Of course when i echo it i get something like: mayseptember19901996College NameAnother CollegeITWriter

What I need is to loop it somehow so i can get the following:

May 1990

College Name

IT


September 1996

Another College

Writer

How could I do that?

Thank you!

6
  • 1
    If you will use key,value pair you can easily achieve what you want.that to in a single loop. Commented Aug 6, 2014 at 10:49
  • so should i do something like foreach($eduGraduationDateMonth as $key => $eduMonthValue). Or where exactly should i add a $key thing? Commented Aug 6, 2014 at 10:54
  • 1
    For all your four variables form array using key,value and make sure that you assign same key to all four variables i.e. 0,1,2.... for simplification then write one foreach in which try like this foreach($eduGraduationDateMonth as $key=>$eduMonthValue){ echo $eduMonthValue." ".$eduGraduationDateYear[$key]."<br>";echo $eduPlace[$key]." <br>";echo $eduFaculty[$key]."<br>;} Commented Aug 6, 2014 at 11:01
  • I got a little bit confused on that :) Could you show some simple example, please, so I could understand how form an array for these variables and assign key and value? Commented Aug 6, 2014 at 11:05
  • 1
    while using this get_post_custom_values() make sure that you are getting array with same set of keys for all four variables. Commented Aug 6, 2014 at 11:09

2 Answers 2

1

just change the order and ad some html elements ..everything is done .updated

   <?php

// MONTH
$eduGraduationDateMonth = get_post_custom_values('rbeduendmonth');
// YEAR
$eduGraduationDateYear = get_post_custom_values('rbeduendyear');
// PLACE
$eduPlace = get_post_custom_values('rbeduplace');
// FACULTY
$eduFaculty = get_post_custom_values('rbedusubject');

foreach($eduGraduationDateMonth as $eduMonthValue){
  $eduMonthUnser = unserialize($eduMonthValue);

}

foreach($eduGraduationDateYear as $eduYearValue){
  $eduYearUnser = unserialize($eduYearValue);

}

foreach($eduPlace as $eduPlaceValue){
  $eduPlaceUnser = unserialize($eduPlaceValue);

}

foreach($eduFaculty as $eduFacultyValue){
  $eduFacultyUnser = unserialize($eduFacultyValue);

}
foreach($eduMonthUnser as $key =>$value){
    echo $value.'<br>';
    echo $eduYearUnser[$key].'<br>'; 
    echo $eduPlaceUnser[$key].'<br>';
    echo $eduFacultyUnser[$key].'<br>';
  }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

nope, this will just add line breaks to it, and i need to be like information about 1 college, than information about second college and so on.
Ok, the edited one worked just perfect, thank you! :)
This will work but I don't prefer it. In the final loop, $key is based on $eduMonthUnser. What if that $key does not exist in year, place or faculty? If your form is properly coded then you should not have any problem. For robustness, better to check that $key exists.
1

I have not worked on wordpress but I took a quick look at the documentation: http://codex.wordpress.org/Function_Reference/get_post_custom

You can use get_post_custom() function and then loop over that. Within each iteration of the loop, access all the keys for that iteration.

Hope this helps.

3 Comments

get_post_custom() doesn't work with arrays as i remember, it returns Array instead. There is a property called get_post_custom_values() to get arrays from data base.
I don't understand. The documentation says it will return "as a multidimensional array containing all custom fields of the current post." Try it out. Will be interested to know if it really doesn't work.
Oh, I see what you mean, yeah my bad, get_post_meta() doesn't work with arrays. But get_post_custom() will return information from all the fields in the form, and this form has around 100 input fields, and user can dynamicaly add unlimited number of extra inputs, so it will be pretty hard to work with all that information if i use get_post_custom() :)

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.