Converting an array to a new array to meet template - php

I have an old file. I would like to integrate it into a new template I bought.
I have a list of links. I store them in a Session array.
The array has a title and a url for each
<?php session_start();
//in my old file i write this
foreach ($_SESSION[links] as $value){
?>
<a href="<?php print $value[linkurl]?>"><?php print $value[linktitle]?></a ><?php
}
// array style i need to match in the new file
// "name" => array(
// "title" => "Display Title",
// "url" => "http://yoururl.com"
// )
// what i tried
// foreach ($_SESSION[links] as $value){
// "$value[linktitle]" => array(
// "title" => "$value[linktitle]",
// "url" => "$value[linkurl]"
// )
// }
//obviously doesn't work
?>
Here is the full template. I'm trying to replace the Smart UI sub (carousel, tab...)
<?php
//CONFIGURATION for SmartAdmin UI
//ribbon breadcrumbs config
//array("Display Name" => "URL");
$breadcrumbs = array(
"Home" => APP_URL
);
/*navigation array config
ex:
"dashboard" => array(
"title" => "Display Title",
"url" => "http://yoururl.com",
"url_target" => "_self",
"icon" => "fa-home",
"label_htm" => "<span>Add your custom label/badge html here</span>",
"sub" => array() //contains array of sub items with the same format as the parent
)
*/
$page_nav = array(
"dashboard" => array(
"title" => "Dashboard",
"url" => APP_URL,
),
"smartui" => array(
"title" => "Smart UI",
"icon" => "fa-code",
"sub" => array(
"carousel" => array(
"title" => "Carousel",
"url" => APP_URL.'/smartui-carousel.php'
),
"tab" => array(
"title" => "Tab",
"url" => APP_URL.'/smartui-tab.php'
),
"accordion" => array(
"title" => "Accordion",
"url" => APP_URL.'/smartui-accordion.php'
),
"widget" => array(
'title' => "Widget",
'url' => APP_URL."/smartui-widget.php"
),
"datatable" => array(
"title" => "DataTable",
"url" => APP_URL."/smartui-datatable.php"
),
"button" => array(
"title" => "Button",
"url" => APP_URL."/smartui-button.php"
),
'smartform' => array(
'title' => 'Smart Form',
'url' => APP_URL.'/smartui-form.php'
)
)
)
);
//configuration variables
$page_title = "";
$page_css = array();
$no_main_header = false; //set true for lock.php and login.php
$page_body_prop = array(); //optional properties for <body>
$page_html_prop = array(); //optional properties for <html>
?>
var_dump:
array(6) {
[0]=> array(4) { ["linktitle"]=> string(13) "Claims Center" ["linkurl"]=> string(79) "Claims.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" }
[1]=> array(4) { ["linktitle"]=> string(16) "Expense Allocate" ["linkurl"]=> string(81) "Expense.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" }
[2]=> array(4) { ["linktitle"]=> string(13) "Freight Rater" ["linkurl"]=> string(105) "User.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" }
[3]=> array(4) { ["linktitle"]=> string(14) "Invoice Center" ["linkurl"]=> string(71) "Online.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" }
[4]=> array(4) { ["linktitle"]=> string(4) "KPIs" ["linkurl"]=> string(4) "KPIs" ["Sectionlinktitle"]=> string(10) "Dashboards" ["linkdes"]=> string(9) "Dashboard" }
[5]=> array(4) { ["linktitle"]=> string(16) "Multi-Mode Rater" ["linkurl"]=> string(0) "M" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(16) "Multi-Mode Rater" }
}

You want to add the navigation in $_SESSION['links'] to the new template!
Comment out the $page_nav variable (/* $page_nav = array( ... ); */) in the template. This will generate the code to insert in the template (cut and paste):
$a = $_SESSION['links'];
echo '<pre>$page_nav = array(' . "\n";
foreach( $a as $k=>$v ) {
echo "\t'" . strtolower(str_replace(' ','',$v['linktitle'])) . "' => array(\n"
. "\t\t'title' => '" . $v['linktitle'] . "',\n"
. "\t\t'url' => '" . $v['linkurl'] . "'\n"
. "\t),\n";
}
echo ');</pre>';
If you want to keep the code in the $_SESSION variable (really not a good idea) you put this piece of code in its place:
$page_nav = array();
foreach ( $_SESSION['links'] as $k=>$v ) {
$page_nav[strtolower(str_replace(' ','',$v['linktitle']))] = array(
'title' => $v['linktitle'],
'url' => $v['linkurl']
);
}
I think this should work! I have not considered any submenus, since none were in your var_dump!

You need to create a new array:
$page_nav = array();
foreach ($_SESSION[links] as $value) {
$page_nav[][$value['linktitle']] = array(
'title' => $value['linktitle'],
'url' => $value['linkurl']
)
}
Now you should have everything in the new array $var;

Related

Php make column wise array from an existing array

I wanted to make an array as column from another array which representing like below. the column should be individual based on each index like , custom_text, thumb and title. I did more favor below.
array(6) {
[0]=>
array(1) {
["custom_text"]=>
string(15) "custom text 1"
}
[1]=>
array(1) {
["custom_text"]=>
string(18) "custom text 2"
}
[2]=>
array(1) {
["thumb"]=>
string(59) "image 1"
}
[3]=>
array(1) {
["thumb"]=>
string(59) "image 2"
}
[4]=>
array(1) {
["title"]=>
string(51) "title 1"
}
[5]=>
array(1) {
["title"]=>
string(181) "title 2"
}
}
here below code is for the array output
$th = array();
$allrows = array();
foreach($aawp_table['rows'] as $table_row_id => $table_row ){
if ( ! $table_row['status'] )
continue;
$th[] = array('headings' => $table_row['label']);
foreach ($aawp_table['products'] as $table_product_id => $table_product){
$asin = $aawp_table['products'][$table_product_id]['asin'];
$data = $aawp_table['products'][$table_product_id]['rows'][$table_row_id];
$type = $aawp_table['rows'][$table_row_id]['type'];
if ( 'custom_text' === $type ) {
$allrows[] = array(
'custom_text' => 'custom text'
);
}
if ( 'thumb' === $type ) {
$allrows[] = array(
'thumb' => 'thumb'
);
}
if('title' === $type){
$allrows[] = array(
'title' => 'title'
);
}
}
}
echo '<pre>';
var_dump($allrows);
echo '</pre>';
I want the array out like this as individual column
array(2) {
[0]=>
string() "custom text 1"
[1]=>
string() "image 1"
[2]=>
string() "custom text 2"
}
Can you please help to do it?
Let's iterate over the array you have and get all values for resulting array. Something like that.
<?php
$array = [
[
'custom_text' => 'custom text 1',
],
[
'custom_text' => 'custom text 2',
],
[
'thumb' => 'image 1',
],
[
'thumb' => 'image 2',
],
[
'title' => 'title 1',
],
[
'title' => 'title 2',
],
];
$result = [];
foreach ($array as $outterArray) {
foreach ($outterArray as $innerArrayValue) {
$result[] = $innerArrayValue;
}
}
var_dump($result);
The code snippet would generate the following output.
array(6) {
[0]=>
string(13) "custom text 1"
[1]=>
string(13) "custom text 2"
[2]=>
string(7) "image 1"
[3]=>
string(7) "image 2"
[4]=>
string(7) "title 1"
[5]=>
string(7) "title 2"
}
Instead of doing this
if ( 'custom_text' === $type ) {
$allrows[] = array(
'custom_text' => 'custom text'
);
}
if ( 'thumb' === $type ) {
$allrows[] = array(
'thumb' => 'thumb'
);
}
if('title' === $type){
$allrows[] = array(
'title' => 'title'
);
}
You should do this
if($type === 'title' || $type === 'thumb' || $type === 'custom_text'){
array_push($allrows,$type);
}

Array for each array row PHP

I have an array with name $items like this:
array(2) {
[0]=> array(8) { ["item_regel"]=> int(0) ["item_id"]=> string(2) "82" ["item_naam"]=> string(21) "Bureauschermen staand" ["item_uitvoering"]=> string(12) "100 x 200 cm" ["item_afmeting"]=> NULL ["item_kleur"]=> string(11) "Transparant" ["item_aantal"]=> string(1) "2" ["item_opmerking"]=> string(18) "Ik wil mijn logo 2" }
[1]=> array(8) { ["item_regel"]=> int(1) ["item_id"]=> string(3) "226" ["item_naam"]=> string(33) "Instructieborden Dibond aluminium" ["item_uitvoering"]=> string(10) "50 x 50 cm" ["item_afmeting"]=> NULL ["item_kleur"]=> string(5) "Blauw" ["item_aantal"]=> string(1) "2" ["item_opmerking"]=> string(4) "test" }
}
I found this code to prefill a gravity form list field:
add_filter( 'gform_field_value_list_one', 'itsg_prefill_list_one' );
function itsg_prefill_list_one( $value ) {
$list_array = array(
//List row
array(
"Product" => "Product",
"Afmeting" => "Good",
"Uitvoering" => "Product",
"Kleur" => "Product",
"Aantal" => "Product",
"Opmerking" => "Product",
),
//List row
array(
"Product" => "Product",
"Afmeting" => "Good",
"Uitvoering" => "Product",
"Kleur" => "Product",
"Aantal" => "Product",
"Opmerking" => "Product",
),
);
return $list_array;
}
Now this function is filled with fixed values. I want to do a 'List row' foreach row inside the array above.
I tried this but it won't work:
add_filter( 'gform_field_value_list_one', 'itsg_prefill_list_one' );
function itsg_prefill_list_one( $value ) {
$items = $_SESSION["wishlist"];
$list_array = array(
foreach ($items as $keys => $values) {
array(
"Product" => $values["item_naam"],
"Afmeting" => $values["item_afmeting"],
"Uitvoering" => $values["item_uitvoering"],
"Kleur" => $values["item_kleur"],
"Aantal" => $values["item_aantal"],
"Opmerking" => $values["item_opmerking"],
),
}
);
return $list_array;
}
Inside the loop add items to the array from the looped array
add_filter( 'gform_field_value_list_one', 'itsg_prefill_list_one' );
function itsg_prefill_list_one( $value ) {
foreach ($_SESSION["wishlist"] as $keys => $values) {
$list_array[] = [
"Product" => $values["item_naam"],
"Afmeting" => $values["item_afmeting"],
"Uitvoering" => $values["item_uitvoering"],
"Kleur" => $values["item_kleur"],
"Aantal" => $values["item_aantal"],
"Opmerking" => $values["item_opmerking"],
];
}
return $list_array;
}

Sort Multidimensional Array By Searched Word

I need to sort a multidimensional array by a searched keyword.
My array is like below.
<?php
array(
array(
'name' => '11th-Physics',
'branch' => 'Plus One',
'college' => 'Plus One',
),
array(
'name' => 'JEE-IIT',
'branch' => 'Physics',
'college' => 'IIT College',
),
array(
'name' => 'Physics',
'branch' => 'Bsc Physics',
'college' => 'College of Chemistry',
),
array(
'name' => 'Chemical Engineering',
'branch' => 'Civil',
'college' => 'Physics Training Center',
),
array(
'name' => 'Physics Education',
'branch' => 'Mechanical',
'college' => 'TBR',
),
)
?>
I need to sort this array when search keyword is physics . And after Sorting i need the result like below.
NEEDED RESULT
<?php
array(
array(
'name' => 'Physics',
'branch' => 'Bsc Physics',
'college' => 'College of Chemistry',
),
array(
'name' => 'Physics Education',
'branch' => 'Mechanical',
'college' => 'TBR',
),
array(
'name' => '11th-Physics',
'branch' => 'Plus One',
'college' => 'Plus One',
),
array(
'name' => 'JEE-IIT',
'branch' => 'Physics',
'college' => 'IIT College',
),
array(
'name' => 'Chemical Engineering',
'branch' => 'Civil',
'college' => 'Physics Training Center',
),
)
?>
That is I need to sort the array first by the name which is exactly like the searched keyword. Then wildcard search in name. Then to the next key branch and same as above. Is there any php function to sort this array like my requirement. I have already checked asort, usort. But I didn't get result properly.
Just call this simple function I just created for your requirement, It works just fine :) change the priority order according to your need
function sortArray($array,$itemToSearch)
{
$sortedArray = array();
$priorityOrder = ['name','branch','college'];
foreach ($priorityOrder as $key)
{
foreach ($array as $i => $value)
{
if(strpos(strtolower($value[$key]), strtolower($itemToSearch)) === 0)
{
array_push($sortedArray, $value);
unset($array[$i]);
}
}
foreach ($array as $i => $value)
{
if(strpos(strtolower($value[$key]), strtolower($itemToSearch)) > 0)
{
array_push($sortedArray, $value);
unset($array[$i]);
}
}
}
return $sortedArray;
}
Here we go
So I started from the algorithm in this answer and modified it to fit your requirements. Since you have three different "priorities" to you sorting, we have to use some temporary variables to separate the elements we wish sorted.
// arrays used to separate each row based on the row where the word "Physics" is found
$searchName = array();
$searchBranch = array();
$searchCollege = array();
// arrays used later for array_multisort
$foundInName = array();
$foundInBranch = array();
$foundInCollege = array();
foreach ($var as $key => $row) {
if(strpos(strtolower($row['name']), 'physics') !== false) {
$searchName[$key] = $row['name'];
$foundInName[] = $row;
}
elseif(strpos(strtolower($row['branch']), 'physics') !== false) {
$searchBranch[$key] = $row['branch'];
$foundInBranch[] = $row;
}
elseif(strpos(strtolower($row['college']), 'physics') !== false) {
$searchCollege[$key] = $row['college'];
$foundInCollege[] = $row;
}
}
// Note: I use SORT_NATURAL here so that "11-XXXXX" comes after "2-XXXXX"
array_multisort($searchName, SORT_NATURAL, $foundInName); // sort the three arrays separately
array_multisort($searchBranch, SORT_NATURAL, $foundInBranch);
array_multisort($searchCollege, SORT_NATURAL, $foundInCollege);
$sortedArray = array_merge($foundInName, $foundInBranch, $foundInCollege);
Outputting $sortedArray using var_dump() gives something like:
array(5) {
[0]=> array(3) {
["name"]=> string(12) "11th-Physics"
["branch"]=> string(8) "Plus One"
["college"]=> string(8) "Plus One"
}
[1]=> array(3) {
["name"]=> string(7) "Physics"
["branch"]=> string(11) "Bsc Physics"
["college"]=> string(20) "College of Chemistry"
}
[2]=> array(3) {
["name"]=> string(17) "Physics Education"
["branch"]=> string(10) "Mechanical"
["college"]=> string(3) "TBR"
}
[3]=> array(3) {
["name"]=> string(7) "JEE-IIT"
["branch"]=> string(7) "Physics"
["college"]=> string(11) "IIT College"
}
[4]=> array(3) {
["name"]=> string(20) "Chemical Engineering"
["branch"]=> string(5) "Civil"
["college"]=> string(23) "Physics Training Center"
}
}
As you can see 11th-Physics comes out first. That is because the ASCII value of numbers is lower than that of letters. To fix this, modify the $search... arrays by prepending a high ASCII character before the string.
if(strpos(strtolower($row['name']), 'physics') !== false) {
// if the first character is a number, prepend an underscore
$searchName[$key] = is_numeric(substr($row['name'], 0, 1)) ? '_'.$row['name'] : $row['name'];
$foundInName[] = $row;
}
Which yields the following output:
array(5) {
[0]=> array(3) {
["name"]=> string(7) "Physics"
["branch"]=> string(11) "Bsc Physics"
["college"]=> string(20) "College of Chemistry"
}
[1]=> array(3) {
["name"]=> string(17) "Physics Education"
["branch"]=> string(10) "Mechanical"
["college"]=> string(3) "TBR"
}
[2]=> array(3) {
["name"]=> string(12) "11th-Physics"
["branch"]=> string(8) "Plus One"
["college"]=> string(8) "Plus One"
}
[3]=> array(3) {
["name"]=> string(7) "JEE-IIT"
["branch"]=> string(7) "Physics"
["college"]=> string(11) "IIT College"
}
[4]=> array(3) {
["name"]=> string(20) "Chemical Engineering"
["branch"]=> string(5) "Civil"
["college"]=> string(23) "Physics Training Center"
}
}
Try it here!

How to compare two multidimensional arrays by certain keys in each?

I have two multidimensional arrays of the same structure.
Like this:
array(2) {
[0] =>
array(9) {
'id' =>
string(5) "44994"
'ersatzteil_id' =>
string(3) "120"
'lang' =>
string(6) "name2_tag2"
'title' =>
string(12) "Seitentüren"
'alias' =>
string(12) "seitentueren"
'content' =>
string(1610) "LOREM ISPUM BLALABLBL"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "1"
'short_text' =>
NULL
}
[1] =>
array(9) {
'id' =>
string(5) "44996"
'ersatzteil_id' =>
string(3) "122"
'lang' =>
string(6) "name1_tag1"
'title' =>
string(7) "Spoiler"
'alias' =>
string(7) "spoiler"
'content' =>
string(1513) "SOME OTHER RANDOM TEXT"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "0"
'short_text' =>
NULL
}
}
What I need to do is I need to compare first array with the second one.
I have to compare them by keys ersatzteil_id and content , and I find that they have same content I need to store element from first array in another new array, that wasn't existing before.
For example I need something like this, but more efficient:
if(array1[20]['ersatzteil_id'] == array2[145]['ersatzteil_id']
&& array1[20]['content'] == array2[145]['content']){
array3 = array1[20];
}
Try this code:-
$result = [];
foreach($array1 as $arr1){
foreach($array2 as $arr2){
if(($arr1['id'] == $arr2['id']) && ($arr1['ersatzteil_id'] == $arr2['ersatzteil_id'])){
$result[] = $arr1;
}
}
}
echo '<pre>'; print_r($result);

Merging arrays with specific output format

I am currently learning how to manage and work with arrays. But I am a bit lost with combining two arrays. Both arrays have the same number of values in it. The array_merge( $thumbnails, $urls ); does what it says but it is not what I am looking for. How can i merge the array as shown below?
$thumbnails = array(
array( "thumbnail" => "https://example1.png" ) ,
array( "thumbnail" => "https://example2.png" ) ,
array( "thumbnail" => "https://example3.png" ) ,
);
$urls = array(
array( "url" => "http://www.example.com/1" ) ,
array( "url" => "http://www.example.com/2" ) ,
array( "url" => "http://www.example.com/3" ) ,
);
Current Result
[0]=>
array(1) {
["thumbnail"]=> "https://example1.png"
}
[1]=>
array(1) {
["thumbnail"]=> "https://example2.png"
}
[2]=>
array(1) {
["thumbnail"]=> "https://example3.png"
}
[3]=>
array(1) {
["url"]=> "http://www.example.com/1"
}
[4]=>
array(1) {
["url"]=> "http://www.example.com/2"
}
[5]=>
array(1) {
["url"]=> "http://www.example.com/3"
}
Desired Result
[0]=>
array(2) {
["thumbnail"]=> "https://example1.png"
["url"]=> "http://www.example.com/1"
}
[1]=>
array(2) {
["thumbnail"]=> "https://example2.png"
["url"]=> "http://www.example.com/2"
}
[2]=>
array(2) {
["thumbnail"]=> "https://example2.png"
["url"]=> "http://www.example.com/2"
}
[3]=>
array(2) {
["thumbnail"]=> "https://example3.png"
["url"]=> "http://www.example.com/3"
}
I would just loop over them:
foreach ($thumbnails as $k => $src) {
if (isset($urls[$k]) {
$urls[$k]['thumbnail'] = $src;
}
}
The function array_merge won't help you out here, because it's "Array in Array". But you can loop over them, so you have only one array-item. The following function returns an array just as you desire:
function array_merge_keys($a, $b) {
$retArr = array();
// Lets use min(countA, countB) so there will be no errors on
// unbalanced array item counts.
for($i = 0; $i < min(count($a), count($b)); $i++) {
$retArr[$i] = array_merge($a[$i], $b[$i]);
} // for end
return $retArr;
}
$a = array(array("url" => "1.jpg"), array("url" => "2.jpg"));
$b = array(array("thumb" => "1.thumb.jpg"), array("thumb" => "2.thumb.jpg"));
print_r(array_merge_keys($a, $b));
This gives you:
Array
(
[0] => Array
(
[url] => 1.jpg
[thumb] => 1.thumb.jpg
)
[1] => Array
(
[url] => 2.jpg
[thumb] => 2.thumb.jpg
)
)
$thumbnails = array(
array( "thumbnail" => "https://example1.png" ) ,
array( "thumbnail" => "https://example2.png" ) ,
array( "thumbnail" => "https://example3.png" ) ,
);
$urls = array(
array( "url" => "http://www.example.com/1" ) ,
array( "url" => "http://www.example.com/2" ) ,
array( "url" => "http://www.example.com/3" ) ,
);
$thumbnails = array_combine(array_map(function($key){return 'key'.$key;},array_keys($thumbnails)),$thumbnails);
$urls = array_combine(array_map(function($key){return 'key'.$key;},array_keys($urls)),$urls);
var_dump(array_values(array_merge_recursive($thumbnails,$urls)));
do you mean this ?

Categories