Jump to content

[Solved] Converting multiple coma or pipe delimited field data to multi-dimensional array


Recommended Posts

Hi all. This is where my lack of actual PHP knowledge is getting to me. I've inherited a never-completed WordPress website that I'm trying to redo in PW. There are products, categories and product options all supplied as separate CSV files. (These will be edited and uploaded every day, so I will need to eventually import the data with a chron script). Importing  and displaying the category and product data isn't a problem, but the product options files contains numerous fields with comma- and pipe-delimited strings which need to be converted into arrays.

So, when viewing the product-page (template) for 'Chair 123' (product code 'c123'), I'd normally get all the options like '$options = $pages->find('template=options-page, pcode=c123');

But since there are fields in the options-page that look like "505|501|512" (for color codes), I'll need to get these into an array. Thats' very simplified; the actual options page has about 45 fields, used to present various options in 3 worldwide territories with multiple parts/color combinations for each conditional territory!

I should also mention that I have the old WordPress site and can see the custom functions the previous guy had set up and (for me) it's very complicated. Most of it is standard PHP, so I was hoping to use much of it 'as is' as I can.

For instance, there's a function on the products page that returns an associative array that looks like this (in the WordPress php):

$bt_options = bt_get_product_options($entry["code"]);

foreach($bt_options['spec_docs'] as $key => $bt_spec_doc) {
    $bt_show_download = "";
    if($bt_options['spec_docs_territory'][$key] == "E" && $bt_this_territory == "EU") { $bt_show_download = "Y"; }
    elseif($bt_options['spec_docs_territory'][$key] == "U" && $bt_this_territory == "US") { $bt_show_download = "Y"; }

And the bt_get_product_options function starts with this (again from WordPress)

function bt_get_product_options($id) {
	global $wpdb, $bt_colours, $mysql_table;
	$entriesList = $wpdb->get_results("SELECT * FROM ".$mysql_table['options']." WHERE web_product_code = '".$id."' ORDER BY id ASC LIMIT 1");
	$entriesList = json_decode(json_encode($entriesList), true); /// CONVERT TO STANDARD ARRAY

	if(count($entriesList) > 0){
		$output = array();
		$output['jquery'] = "";
		foreach($entriesList as $entry) {
			elseif($key == "comb_territory" || $key == "addnl_subtabs_territory" || $key == "spec_docs_territory"){
				$ter_array = explode("|", $tt);
				foreach ($ter_array as $k => $tr) {
					$output[$key][] = $tr;
		... loads more like this ...
	return $output;

TL;DR: Can I use the above in my PW code, provided I swap the WP MySQL bit with $entriesList = $(wire)pages->find("template=options-page, product_code=$id, limit=1) ? (I tried this on a limited set of fields but didn't return anything). Or do I need to do a MySQL request too?

And from what I can tell, thet json_decode/encode line should covert the page object to an associative array, correct? Should that work in this case?

Thanks for reading this far.

Link to comment
Share on other sites

Ok, a little more research turned up a topic about merging different arrays and that led me to explode my page array then use the above-mentioned  json_decode/encode thing to convert to a standard array. I don't quite understand how that works, but it does, so I'm a happy camper.

$entriesList = wire('pages')->find("template=options-page, title=$id, limit=1")->explode(['title', 'sku_code', ... ]);
$entriesList = json_decode(json_encode($entriesList), true); /// CONVERT TO STANDARD ARRAY


Link to comment
Share on other sites

  • strandoo changed the title to [Solved] Converting multiple coma or pipe delimited field data to multi-dimensional array

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...