Create array from PHP form post - php

I'm trying to create a multidimensional array from a form post. This is the dump from that post:
array(8) {
["check"]=>
int(1)
["option_page"]=>
string(19) "content_boxes_group"
["action"]=>
string(6) "update"
["_wpnonce"]=>
string(10) "0adb157142"
["_wp_http_referer"]=>
string(39) "/wp-admin/themes.php?page=home-settings"
["title"]=>
array(3) {
[1]=>
string(9) "Downloads"
[2]=>
string(7) "Columns"
[3]=>
string(4) "Apps"
}
["id"]=>
array(3) {
[1]=>
string(21) "k2-settings-downloads"
[2]=>
string(19) "k2-settings-columns"
[3]=>
string(16) "k2-settings-apps"
}
["order"]=>
array(3) {
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(1) "3"
}
}
I'm trying to make it look like this:
array(
array('title' => 'Downloads', 'id' => 'k2-settings-downloads', 'order' => '1'),
array('title' => 'Columns', 'id' => 'k2-settings-columns', 'order' => '2'),
array('title' => 'Apps', 'id' => 'k2-settings-apps', 'order' => '3')
);
How can I do this?

something like this?
$post = $_POST['your_array'];
$output = array();
$titles = $post['title'];
$ids = $post['id'];
$orders = $post['order'];
foreach($titles as $id => $title){
$output[] = array("title"=>$title,"id"=>$ids[$id],'order'=>$orders[$id]);
}

Related

How to create multidimensional array unlimited depth array with parent and child came from single table

Im trying to create multi-dimesional array with unlimited depth.
I select the data from the database and check it if the field 'isArray' is true, meaning this column is a parent then I tried to make a loop to make it look for its child 'parent_id' => $row->id.
I'm expecting output like this.
array(
[0]=> array(
'id' => '29',
'section' => '',
'sorting' =>'',
'title' => 'POWERS OF THE BANKO SENTRAL',
'pdf_file' => '',
'content' => '',
'parent_id' => '0',
'isArray' => array(
[0] => array(
'id' => '30',
'section' => '001',
'sorting' => '',
'title' => 'Examination by the Bangko Sentral',
'pdf_file' => 'NRBSL CHRISTMAS PARTY RAFFLE WINNERS.pdf',
'parent_id' => '29',
'isArray' => 0,
),
[1] => array(
'id' => '31',
'section' => '002',
'sorting' => '',
'title' => 'Supervisory Enforcement',
'pdf_file' => 'APL-Form1.pdf'
'parent_id' => '29',
'isArray' => 0
)
),
),
[1]=> array(
[0] => array(
'id' => '32',
'section' => '',
'sorting' => '',
'title' => 'A. Risk Management',
'pdf_file' => '',
'content' => '',
'parent_id' => '0',
'isArray' => array(
[0] => array(
'id' => '33',
'section' => '911',
'sorting' => '',
'title' => 'RISK MANAGEMENT',
'pdf_file' => '',
'content' => '',
'parent_id' => '32',
'isArray' => array(
[0] => array(
'id' => '34',
'section' => 'ASDF',
'sorting' => '',
'title' => 'ASDFSDF',
'pdf_file' => '',
'content' => '',
'parent_id' => '33',
'isArray' = array()
)
)
)
)
)
)
)
And the data I get from the database is this
I came up with this code:
public function findParentsParent($result) {
global $subs;
foreach ($result as $row) {
$isArray = filter_var($row->isArray, FILTER_VALIDATE_BOOLEAN);
if ($isArray) {
$subs[][$row->parent_id] = $row;
$where = ['parent_id' => $row->id];
$result = $this->my_model->select_where('tbl_policies', $where, 'sorting, section');
if(!empty($result))
$this->findParentsParent($result);
//return array_reverse($subs);
} else {
$subs[] = $row;
}
}
return array_reverse($subs);
}
But I ended up with this array:
array(6) {
[0]=>
object(stdClass)#44 (11) {
["id"]=>
string(2) "30"
["section"]=>
string(3) "001"
["sorting"]=>
string(0) ""
["title"]=>
string(33) "Examination by the Bangko Sentral"
["pdf_file"]=>
string(41) "NRBSL CHRISTMAS PARTY RAFFLE WINNERS1.pdf"
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "29"
["isArray"]=>
string(1) "0"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 11:46:06"
["updated_at"]=>
string(19) "2022-03-03 11:46:06"
}
[1]=>
object(stdClass)#43 (11) {
["id"]=>
string(2) "31"
["section"]=>
string(3) "002"
["sorting"]=>
NULL
["title"]=>
string(30) "Supervisory Enforcement Policy"
["pdf_file"]=>
string(13) "APL-Form1.pdf"
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "29"
["isArray"]=>
string(1) "0"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 13:19:27"
["updated_at"]=>
string(19) "2022-03-03 13:19:27"
}
[2]=>
array(1) {
[0]=>
object(stdClass)#40 (11) {
["id"]=>
string(2) "29"
["section"]=>
string(0) ""
["sorting"]=>
string(0) ""
["title"]=>
string(28) "POWERS OF THE BANGKO SENTRAL"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(1) "0"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 11:45:25"
["updated_at"]=>
string(19) "2022-03-03 11:45:25"
}
}
[3]=>
array(1) {
[33]=>
object(stdClass)#42 (11) {
["id"]=>
string(2) "34"
["section"]=>
string(4) "ASDF"
["sorting"]=>
NULL
["title"]=>
string(7) "ASDFSDF"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "33"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:29:37"
["updated_at"]=>
string(19) "2022-03-04 09:29:37"
}
}
[4]=>
array(1) {
[32]=>
object(stdClass)#41 (11) {
["id"]=>
string(2) "33"
["section"]=>
string(3) "911"
["sorting"]=>
NULL
["title"]=>
string(15) "RISK MANAGEMENT"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "32"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:29:18"
["updated_at"]=>
string(19) "2022-03-04 09:29:18"
}
}
[5]=>
array(1) {
[0]=>
object(stdClass)#39 (11) {
["id"]=>
string(2) "32"
["section"]=>
NULL
["sorting"]=>
NULL
["title"]=>
string(18) "A. Risk Management"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(1) "0"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:28:41"
["updated_at"]=>
string(19) "2022-03-04 09:28:41"
}
}
}
Credits to Recursive function to generate multidimensional array from database result
I solved my problem with this code
public function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->buildTree($elements, $element['id']);
$isArray = filter_var($element['isArray'], FILTER_VALIDATE_BOOLEAN);
if ($isArray) {
$element['isArray'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}

How to get specific values out of all indexes in an array

I have an array which I want to iterate over to push the items into a select box, but I can't figure out how to do it.
The array I get from the function:
array(2) {
["de"]=> array(10) {
["id"]=> int(10)
["order"]=> int(1)
["slug"]=> string(2) "de"
["locale"]=> string(5) "de-DE"
["name"]=> string(7) "Deutsch"
["url"]=> string(34) "http://localhost/werk/Mol/de/haus/"
["flag"]=> string(66) "http://localhost/werk/Mol/wp-content/plugins/polylang/flags/de.png"
["current_lang"]=> bool(false)
["no_translation"]=> bool(false)
["classes"]=> array(4) {
[0]=> string(9) "lang-item"
[1]=> string(12) "lang-item-10"
[2]=> string(12) "lang-item-de"
[3]=> string(15) "lang-item-first"
}
}
["nl"]=> array(10) {
["id"]=> int(3)
["order"]=> int(2)
["slug"]=> string(2) "nl"
["locale"]=> string(5) "nl-NL"
["name"]=> string(10) "Nederlands"
["url"]=> string(26) "http://localhost/werk/Mol/"
["flag"]=> string(66) "http://localhost/werk/Mol/wp-content/plugins/polylang/flags/nl.png"
["current_lang"]=> bool(true)
["no_translation"]=> bool(false)
["classes"]=> array(4) {
[0]=> string(9) "lang-item"
[1]=> string(11) "lang-item-3"
[2]=> string(12) "lang-item-nl"
[3]=> string(12) "current-lang"
}
}
}
I tried a foreach but I did only get the indexes of the array
<?php
$translations = pll_the_languages(array('raw' => 1));
$lang_codes = array();
foreach ($translations as $key => $value) {
array_push($lang_codes, $key);
}
?>
I need the language slug, URL, and flag from all indexes in this array (de & nl), what should I do?
A simple iteration over the outer array and then pick the values you want from the sub array.
<?php
$translations = pll_the_languages(array('raw' => 1));
$lang_codes = array();
foreach ($translations as $lang => $info) {
$lang_codes[$lang] = [ 'slug' => $info['slug'],
'url' => $info['url'],
'flag' => $info['flag']
];
}
?>
You can approach this as
$res = [];
foreach($translations as $key => $value){
$res[$key] = [
'slug' => $value['slug'],
'url' => $value['url'],
'flag' => $value['flag']
];
}
Live Demo

Wordpress custom tax query with multiple conditions not working

I have this weird functionality that is not working in wordpress.
I'm using woocomerce and I have to get some products based on some filters using the following dump code:
["tax_query"]=>
array(2) {
[0]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(4) {
[0]=>
string(3) "running"
[1]=>
string(9) "hiking"
}
["operator"]=>
string(3) "AND"
}
[1]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(2) {
[0]=>
string(5) "nike"
[1]=>
string(9) "adidas"
}
["operator"]=>
string(2) "OR"
}
}
For $args['tax_query'][0] I need to make AND logic. This means that the results must decrease if I select more checkboxes.
For $args['tax_query'][1] I need to make an OR logic.
The explanation for the above code is: Give me all the products that are for running but also for hiking and is produced by nike or adidas.
The following code returns 0 results. If I remove the ["operator"]=> 'AND' from $args['tax_query'][0] it will return all the results like it just pass the filter.
The final code is:
array(6) {
["posts_per_page"]=>
string(2) "12"
["post_status"]=>
string(7) "publish"
["post_type"]=>
string(7) "product"
["offset"]=>
string(1) "0"
["tax_query"]=>
array(2) {
[0]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(3) {
[0]=>
string(3) "running"
[1]=>
string(9) "hiking"
}
["operator"]=>
string(3) "AND"
}
[1]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(2) {
[0]=>
string(11) "nike"
[1]=>
string(34) "adidas"
}
["operator"]=>
string(2) "OR"
}
}
["meta_query"]=>
NULL
}
Any explanation or a fix for this?
Thank you.
You would need to use OR relation first, then AND
Modify the arguments as you need, this solution is just an idea.
Tested with POSTS
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => array( 'Apple', 'Design' ),
'field' => 'name',
'operator' => 'AND',
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'terms' => array('Photography', ' Android'),
'field' => 'name',
),
),
),
);
// The Query
$query = new WP_Query( $args );
foreach ($query->posts as $key => $post) {
echo '<pre>';print_r($post->post_title . ' - '. get_the_category_list(','));echo '</pre>';
}

Create a checkbox for tags in Wordpress

Im editing a plugin because I want to create a checkbox for the tags the plugin has. In this moment Ive got in a variable, this array:
array(9) { [129]=> object(EM_Tag)#84 (15) { ["id"]=> string(3) "129" ["term_id"]=> string(3) "129" ["name"]=> string(35) "Accessible for non-English speakers" ["slug"]=> string(11) "non-english" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(3) "129" ["taxonomy"]=> string(10) "event-tags" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "0" ["fields"]=> array(0) { } ["required_fields"]=> array(0) { } ["feedback_message"]=> string(0) "" ["errors"]=> array(0) { } ["mime_types"]=> array(3) { [1]=> string(3) "gif" [2]=> string(3) "jpg" [3]=> string(3) "png" } } }
There are more tags but I just put one. I would like to generate a checkbox for each tag.
One solution is to iterate over the array that you provided and access the fields that way. I made a shortened array with proper indentation based on your example provided. It seems to be the same but let me know otherwise.
$array = array(
129 => array(
'id' => '129',
'name' => 'Accessible for non-English Speakers'
),
130 => array(
'id' => '130',
'name' => 'A second piece of information'
),
131 => array(
'id' => '131',
'name' => 'A third piece of information'
)
);
// Iterate over the array
foreach ($array as $c) {
// Access the required data
$id = $c['id'];
$name = $c['name'];
// Generate your checkbox
print "<input type='checkbox' name='$name' id='$id'>";
}

How to merge PHP multidimensional array keeping a unique key and separating with comma the other value

I have a multidimensional array and I want to make it matching the "unique key" value but merge the other key that has the same "unique key" value, it could be speared by comma, since my final output will be to use json_encode.
So for instance, if I have:
[0]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "123"
}
[1]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "124"
}
[2]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "126"
}
[3]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "129"
}
[4]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "130"
}
[5]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "102"
}
[6]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "193"
}
[7]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "156"
}
[8]=>
array(2) {
["label"]=>
string(2) "BG"
["value"]=>
string(8) "246"
}
[9]=>
array(2) {
["label"]=>
string(1) "C"
["value"]=>
string(8) "234"
}
[10]=>
array(2) {
["label"]=>
string(1) "C"
["value"]=>
string(8) "235"
}
[11]=>
array(2) {
["label"]=>
string(2) "CA"
["value"]=>
string(8) "345"
}
[12]=>
array(2) {
["label"]=>
string(2) "CA"
["value"]=>
string(8) "564"
}
And I want an output like:
[0]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string "123,124,126,129,130,102,193,156“
}
[1]=>
array(2) {
["label"]=>
string(2) "BG"
["value"]=>
string "246"
}
[2]=>
array(2) {
["label"]=>
string(1) "C"
["value"]=>
string(8) "234,235,”
}
[3]=>
array(2) {
["label"]=>
string(2) "CA"
["value"]=>
string(8) "345,564,”
}
I am not sure how to do it, I've looked into array_merge_recursive and other similar solutions, but I did not got it, maybe I need to use implode.
You can use something like this:
$result = array();
foreach ($array as $arr) {
if (!isset($result[$arr['label']])) {
$result[$arr['label']] = $arr;
continue;
}
$result[$arr['label']]['value'] .= ',' . $arr['value'];
}
// if you really need numeric indexes use:
$result = array_values($result);
You can try
$array = array(
array(
'label' => "AB",
'value' => 123
),
array(
'label' => "AB",
'value' => 124
),
array(
'label' => "AB",
'value' => 125
),
array(
'label' => "C",
'value' => 126
),
array(
'label' => "C",
'value' => 127
),
array(
'label' => "C",
'value' => 127
),
);
$result = array();
foreach ($array as $el) {
$result[$el['label']] = array(
'label' => $el['label'],
'value' => implode( ',',
array_unique(
array_merge(
array_filter(
explode(',', $result[$el['label']]['value'])
),
array($el['value'])
)
)
)
);
}
echo "<pre>"; var_dump(array_values($result));

Categories