I build a plugin, that is able to run through a CSV file and parse the variables into custom fields of a custom post.
In my custom post type, I build a meta box with repeatable custom fields. The format, that this meta box fetch the data from MySQL is this:
a:3:{i:0;a:2:{s:5:"title";s:7:"Title 1";s:5:"point";s:19:"55.635656,12.506767";}i:1;a:2:{s:5:"title";s:7:"Title 2";s:5:"point";s:19:"55.625668,12.502392";}i:2;a:2:{s:5:"title";s:7:"Title 3";s:5:"point";s:19:"55.618781,12.519143";}}
Following code runs through a x amount of coordinate variables of the CSV file:
// Variables used for getting coordinates
$a = 0;
$b = 0;
$c = 0;
//Variables used for coordinate titles
$ctitle = 0;
// Array containing all imported coordinate sets
$points = array();
//$coordinate;
$Xcoordinate;
$Ycoordinate;
$XYcoordinate;
while (!empty($post["geometry/coordinates/" . $a . "/" . $b . "/" . $c])) {
// Instantiating array that contain a single set of point data
$point = array();
$newctexttitle = 'Title ' . $ctitle;
array_push($point,"title");
array_push($point,$newctexttitle);
array_push($point,"point");
echo "</br>";
echo "Coordinate title is: " . $ctitle;
echo "</br>";
if ($post["geometry/coordinates/" . $a . "/" . $b . "/" . $c]){
echo "The imported X coordinate is: " . $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
echo "</br>";
$Xcoordinate = $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
// Increase c variable by one
$c++;
echo "C is now: " . $c;
echo "</br>";
}
if ($post["geometry/coordinates/" . $a . "/" . $b . "/" . $c]){
echo "The imported Y coordinate is: " . $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
echo "</br>";
$Ycoordinate = $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
// Decrease c variable by one
$c--;
echo "C is now: " . $c;
echo "</br>";
}
// Increase b variable by one
$b++;
echo "B is now: " . $b;
echo "</br>";
// Increase title variable by one
$ctitle++;
$XYcoordinate = $Xcoordinate . ", " . $Ycoordinate;
array_push($point,$XYcoordinate);
array_push($points,$point);
// Empty out array for the specific point
unset($point);
}
update_post_meta($post["id"], 'points', $points);
Problem is, that the post meta is stored in following format:
a:3:{i:0;a:4:{i:0;s:5:"title";i:1;s:7:"Title 0";i:2;s:5:"point";i:3;s:19:"8.228271, 56.094402";}i:1;a:4:{i:0;s:5:"title";i:1;s:7:"Title 1";i:2;s:5:"point";i:3;s:19:"8.216593, 56.082355";}i:2;a:4:{i:0;s:5:"title";i:1;s:7:"Title 2";i:2;s:5:"point";i:3;s:18:"8.219391, 56.08139";}}
Etc. notice the "i:0","i:1", "i:2" and so on, of every every variable inside the nested array.
What do I need to do different, in order store the data in the same format, as in the example above?
I build a plugin, that is able to run through a CSV file and parse the variables into custom fields of a custom post.
In my custom post type, I build a meta box with repeatable custom fields. The format, that this meta box fetch the data from MySQL is this:
a:3:{i:0;a:2:{s:5:"title";s:7:"Title 1";s:5:"point";s:19:"55.635656,12.506767";}i:1;a:2:{s:5:"title";s:7:"Title 2";s:5:"point";s:19:"55.625668,12.502392";}i:2;a:2:{s:5:"title";s:7:"Title 3";s:5:"point";s:19:"55.618781,12.519143";}}
Following code runs through a x amount of coordinate variables of the CSV file:
// Variables used for getting coordinates
$a = 0;
$b = 0;
$c = 0;
//Variables used for coordinate titles
$ctitle = 0;
// Array containing all imported coordinate sets
$points = array();
//$coordinate;
$Xcoordinate;
$Ycoordinate;
$XYcoordinate;
while (!empty($post["geometry/coordinates/" . $a . "/" . $b . "/" . $c])) {
// Instantiating array that contain a single set of point data
$point = array();
$newctexttitle = 'Title ' . $ctitle;
array_push($point,"title");
array_push($point,$newctexttitle);
array_push($point,"point");
echo "</br>";
echo "Coordinate title is: " . $ctitle;
echo "</br>";
if ($post["geometry/coordinates/" . $a . "/" . $b . "/" . $c]){
echo "The imported X coordinate is: " . $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
echo "</br>";
$Xcoordinate = $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
// Increase c variable by one
$c++;
echo "C is now: " . $c;
echo "</br>";
}
if ($post["geometry/coordinates/" . $a . "/" . $b . "/" . $c]){
echo "The imported Y coordinate is: " . $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
echo "</br>";
$Ycoordinate = $post["geometry/coordinates/" . $a . "/" . $b . "/" . $c];
// Decrease c variable by one
$c--;
echo "C is now: " . $c;
echo "</br>";
}
// Increase b variable by one
$b++;
echo "B is now: " . $b;
echo "</br>";
// Increase title variable by one
$ctitle++;
$XYcoordinate = $Xcoordinate . ", " . $Ycoordinate;
array_push($point,$XYcoordinate);
array_push($points,$point);
// Empty out array for the specific point
unset($point);
}
update_post_meta($post["id"], 'points', $points);
Problem is, that the post meta is stored in following format:
a:3:{i:0;a:4:{i:0;s:5:"title";i:1;s:7:"Title 0";i:2;s:5:"point";i:3;s:19:"8.228271, 56.094402";}i:1;a:4:{i:0;s:5:"title";i:1;s:7:"Title 1";i:2;s:5:"point";i:3;s:19:"8.216593, 56.082355";}i:2;a:4:{i:0;s:5:"title";i:1;s:7:"Title 2";i:2;s:5:"point";i:3;s:18:"8.219391, 56.08139";}}
Etc. notice the "i:0","i:1", "i:2" and so on, of every every variable inside the nested array.
What do I need to do different, in order store the data in the same format, as in the example above?
Share Improve this question asked Feb 13, 2019 at 22:05 DouglessDougless 751 silver badge8 bronze badges 3- This is how WordPress saves arrays in the database. How are you fetching the data? – czerspalace Commented Feb 14, 2019 at 0:00
- It should also be possible to store the array the same way, as the example above. The example i display, is also fetched from phpmyadmin. The example comes from a custom post, that I manually typed data intot he repeatable custom field. I simply just don't know how i save my automatically imported data in MySQL, the same way – Dougless Commented Feb 14, 2019 at 10:04
- How are you creating the repeatable custom fields? – czerspalace Commented Feb 14, 2019 at 20:41
1 Answer
Reset to default 0In order to figure the issue out, I ran unserialize on both data strings and saw that the top example saved as an associative array like:
[title] => Title 1
[point] => 55.635656,12.506767
and the second array was flat like:
[0] => title
[1] => Title 0
[2] => point
[3] => 8.228271, 56.094402
In order to have the imported example match the repeatable box, you need to replace
array_push($point,"title");
array_push($point,$newctexttitle);
array_push($point,"point");
with
$point['title'] = $newctexttitle;
and replace
array_push($point,$XYcoordinate);
with
$point['point'] = $XYcoordinate;