I have a tree array from cakephp 2.0 tree behavior noe i need to separate it in levels so i can build a selector per each level. Here is an example of the array:
Array
(
[25] => Global
[26] => _Region1
[29] => __Pais1
[41] => ___Distrito1
[42] => ___Distrito2
[30] => __Pais2
[43] => ___Distrito1
[44] => ___Distrito2
[31] => __Pais3
[45] => ___Distrito1
[32] => __Pais4
[46] => ___Distrito1
[27] => _Region2
[33] => __Pais1
[47] => ___Distrito1
[34] => __Pais2
[48] => ___Distrito1
[35] => __Pais3
[36] => __Pais4
[28] => _Region3
[37] => __Pais1
[38] => __Pais2
[39] => __Pais3
[40] => __Pais4
)
Now what i need is to create one select for global, another select for region1 another for pais1 and another for disctrito1
The problem is that i can use ids to generate it since this will be changing by the user.
What will be the best way to manipulate this array so it builds a select to each level of the array.
Thanks in advance.
You can get the tree as a nested array via find('threaded'). This will give you each item in the tree, with a 'children' key containing all child nodes as an array;
Retrieving Your Data - find('threaded')
However, as indicated, because the content will be modified by the user, the depth of the tree may change. To accommodate for those changes, you'll need to use a 'recursive function' to loop through the tree, regardless of its depth
Mockup Code:
echo $this->MyTreeHelper->buildTreeDropDowns($treeData);
class MyTreeHelper extends AppHelper {
public function buildTreeDropDowns($treeData)
{
$output = '';
foreach($treeData as $item) {
$output .= createAdropDown($item);
if (!empty($item['children'])) {
$output .= $this->buildTreeDropDowns($item['children']);
}
}
return $output;
}
}
Related
EDIT: After reading the comments and looking at this again - I realise I was going about this the wrong way.
I was able to achieve my aim by using the first foreach loop on it's own.
Apologies for the long post.
I am parsing an array where the returned data is not consistent with the keys.
From this array, I need to determine what is contained within: Title:, Album:, Artist:
I can't simply filter or search for those strings, as they appear multiple times but followed by different text (see array below).
Below is a small sample of the array ($playlistid).
Array
(
[0] => MPD 0.23.5
[1] => file: Blondie/Parallel_Lines/12_Just_Go_Away.flac
[2] => Last-Modified: 2016-06-05T17:15:07Z
[3] => Format: 44100:16:2
[4] => Album: Parallel Lines
[5] => AlbumArtist: Blondie
[6] => Artist: Blondie
[7] => Composer: Debbie Harry
[8] => Date: 1978
[9] => Genre: Rock
[10] => Title: Just Go Away
[11] => Track: 12
[12] => Time: 214
[13] => duration: 213.800
[14] => Pos: 0
[15] => Id: 745
[16] => file: Blondie/Parallel_Lines/13_Once_I_Had_a_Love-aka_The_Disco_Song-1978_version-.flac
[17] => Last-Modified: 2016-06-05T17:15:01Z
[18] => Format: 44100:16:2
[19] => Album: Parallel Lines
[20] => AlbumArtist: Blondie
[21] => Artist: Blondie
[22] => Composer: Chris Stein; Debbie Harry
[23] => Date: 1978
[24] => Genre: Rock
[25] => Title: Once I Had a Love (aka The Disco Song) (1978 version)
[26] => Track: 13
[27] => Time: 198
[28] => duration: 198.000
[29] => Pos: 1
[30] => Id: 746
[31] => file: Blondie/Parallel_Lines/14_Bang_a_Gong-Get_It_On-live-.flac
[32] => Last-Modified: 2016-06-05T17:15:03Z
[33] => Format: 44100:16:2
[34] => Album: Parallel Lines
[35] => AlbumArtist: Blondie
[36] => Artist: Blondie
[37] => Composer: Marc Bolan
[38] => Date: 1978
[39] => Genre: Rock
[40] => Title: Bang a Gong (Get It On) (live)
[41] => Track: 14
[42] => Time: 330
[43] => duration: 330.293
[44] => Pos: 2
[45] => Id: 747
[46] =>
)
I have a foreach loop that searches the above array ($playlistid) for the desired field, and generates three arrays with the desired keys.
foreach($playlistid as $key => $value) {
if (preg_match('/\bTitle:\s/', $value)) {
$song_title = $playlistid[$key];
}
if (preg_match('/\bArtist:\s/', $value)) {
$song_artist = $playlistid[$key];
}
if (preg_match('/\bAlbum:\s/', $value)) {
$album_title = $playlistid[$key]
}
}
The result I'm looking for, is the output to look like this:
Artist, Album, Title
Blondie Parallel Lines Just Go Away
Blondie Parallel Lines Once I Had a Love (aka The Disco Song) (1978 version)
Blondie Parallel Lines Bang a Gong (Get It On) (live)
It's worth bearing in mind that with a various album for example, the Artist and Album tags will also change with each new track.
I hope all of this makes sense!
I have created a multi-dimensional array using fgetcsv from a CSV file.
Using both DOMDocument and SimpleXML I am trying to create a XML file of the CSV document.
The array and XML variables are being passed to a function within the same class file. The XML document is being created without any issues, but no value is passing from the array into the XML. It does work it I use a static value opposed to passing a value from the array, also if I print_r the array the structure and values are all correct.
I have tried 'htmlspecialcharacters' and 'encode_UTF8' before passing the value into the XML.
An example of the code is below, product is the multi-dimensional array.
public function array_to_xml($product, &$xml)
{
foreach($product as $row)
{
$element = $xml->createElement("Product");
$xml->appendChild($element);
$element = $xml->createElement("ID", ($row[38]));
$xml->appendChild($element);
}
}
The problem is obviously with the array but I can't find the answer. Any help would be gratefully appreciated.
The output currently looks like (with not value in the ID element). Once it is working Product will have about 20 child elements.
<?xml version="1.0"?>
<ProductList/>
<Product>
<ID/>
</Product>
</ProductList>
Example of $row when printed to screen:
Array ( [0] => [1] => [2] => 6/10/2016 [3] => [4] => [5] => 7.35 [6] => N [7] => N [8] => N [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 80 [15] => 0 [16] => 80 [17] => 0 [18] => 80 [19] => N [20] => N [21] => N [22] => N [23] => 236.50 [24] => 0.00 [25] => 4.86 [26] => AFG Home Loans - Alpha [27] => 100% Offset Lo Doc Fixed [28] => 100% Offset Lo Doc 4 Year Fixed Owner Occupied [29] => 250.00 [30] => [31] => 7.35 [32] => 0.00 [33] => 4.9 [34] => N [35] => 325.00 [36] => 48 [37] => 4.52 [38] => 1-1MX78TF [39] => N [40] => [41] => [42] => N [43] => N [44] => [45] => Y [46] => 0.00 [47] => 10,000.00 [48] => 2,000,000.00 [49] => Y [50] => 30 [51] => [52] => [53] => Y [54] => 0.00 )
A couple things stand out. First, you have a syntax error on this line:
$element = $xml->createElement("ID", ($row[38])); (note the errant parentheses around $row[38]. The createElement method takes a String for its second parameter.
Second, you're not adding the ID to the product, but to the root XML. Fixing that, your code should look closer to this.
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$id = $xml->createElement("ID", $row[38]);
$product->appendChild($id);
$xml->appendChild($product);
}
}
If you need it as an attribute as #Barmar commented, you'd use the DOMElement->setAttribute() method, and it would look like:
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$product->setAttribute('ID', $row[38]);
$xml->appendChild($product);
}
}
I want to split array into two on the basis of value.
For example
Array(
[25] => Korea Strawberry (Pkt) 250g
[26] => *8887187338308
[27] => PROM Mix 2#38.9G
[28] => 19.90*2 38.90 Z
[29] => 0.45*2
[30] =>
[31] =>
[32] => Total with GST # 6%
[33] => Rounding
[34] => Total
[35] => Tender
[36] => Master 0003 App: 807946
[37] => Change
[38] => GST Analysis Goods Tax Amount
[39] => S = 6%
[40] => Item 3
[41] => Qty 4
[42] => Total Saving 9.44
[43] => 56.94
)
I want it to be in two parts. One all the values before Total with GST # 6% second all remaining values.
How can I do that. I tried array_diff() and other methods but didn't work as what I want.
You could get the key number for Total with GST # 6% using array_search then split the array using 2 loops.
$array = Array(
[25] => Korea Strawberry (Pkt) 250g
[26] => *8887187338308
[27] => PROM Mix 2#38.9G
[28] => 19.90*2 38.90 Z
[29] => 0.45*2
[30] =>
[31] =>
[32] => Total with GST # 6%
[33] => Rounding
[34] => Total
[35] => Tender
[36] => Master 0003 App: 807946
[37] => Change
[38] => GST Analysis Goods Tax Amount
[39] => S = 6%
[40] => Item 3
[41] => Qty 4
[42] => Total Saving 9.44
[43] => 56.94
);
$value = 'Total with GST # 6%'; //This can be dynamically filled
$split = array_search($value, $array)
for($i=0; $i<$split; $i++) {
$newArray1[] = $array[$i];
}
for($i=$split; $i<count($array); $i++) {
$newArray2[] = $array[$i];
}
unset($array); //Now it's split we can unset the $array variable
You are looking for array_slice.
Just be careful about using it on associative arrays, or arrays with arbitrary numerical keys.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been coding and improving the code jszobody provided me that can found on this LINK which It makes me to be my refference but it results me to an error Fatal error: Using $this when not in object context
PHP:
<?php
function getAllPossiblePermutations($mdArray, $firstTime=true, $tempArray=array())
{
// initialize results array
if ($firstTime)
{
$this->permutationsResultsArray = array();
}
// find first sub array and iterate through it
$thisArray = array_shift($mdArray);
foreach ($thisArray as $key => $elem)
{
// if this number has already been used skip this possible permutation
if (in_array($elem, $tempArray))
{
continue;
}
$tempArray[] = $elem;
if (count($mdArray) == 0)
{
$this->permutationsResultsArray[] = $tempArray;
}
else
{
$this->getAllPossiblePermutations($mdArray, false, $tempArray);
}
array_pop($tempArray);
}
}
$traits = array
(
array('Happy', 'Sad', 'Angry', 'Hopeful'),
array('Outgoing', 'Introverted'),
array('Tall', 'Short', 'Medium'),
array('Handsome', 'Plain', 'Ugly')
);
print_r(getAllPossiblePermutations($traits));
?>
EXPECTED OUTPUT:
Array ( [0] => HappyOutgoingTallHandsome 1 => HappyOutgoingTallPlain [2] => HappyOutgoingTallUgly [3] => HappyOutgoingShortHandsome [4] => HappyOutgoingShortPlain [5] => HappyOutgoingShortUgly [6] => HappyOutgoingMediumHandsome [7] => HappyOutgoingMediumPlain [8] => HappyOutgoingMediumUgly [9] => HappyIntrovertedTallHandsome [10] => HappyIntrovertedTallPlain [11] => HappyIntrovertedTallUgly [12] => HappyIntrovertedShortHandsome [13] => HappyIntrovertedShortPlain [14] => HappyIntrovertedShortUgly [15] => HappyIntrovertedMediumHandsome [16] => HappyIntrovertedMediumPlain [17] => HappyIntrovertedMediumUgly [18] => SadOutgoingTallHandsome [19] => SadOutgoingTallPlain [20] => SadOutgoingTallUgly [21] => SadOutgoingShortHandsome [22] => SadOutgoingShortPlain [23] => SadOutgoingShortUgly [24] => SadOutgoingMediumHandsome [25] => SadOutgoingMediumPlain [26] => SadOutgoingMediumUgly [27] => SadIntrovertedTallHandsome [28] => SadIntrovertedTallPlain [29] => SadIntrovertedTallUgly [30] => SadIntrovertedShortHandsome [31] => SadIntrovertedShortPlain [32] => SadIntrovertedShortUgly [33] => SadIntrovertedMediumHandsome [34] => SadIntrovertedMediumPlain [35] => SadIntrovertedMediumUgly [36] => AngryOutgoingTallHandsome [37] => AngryOutgoingTallPlain [38] => AngryOutgoingTallUgly [39] => AngryOutgoingShortHandsome [40] => AngryOutgoingShortPlain [41] => AngryOutgoingShortUgly [42] => AngryOutgoingMediumHandsome [43] => AngryOutgoingMediumPlain [44] => AngryOutgoingMediumUgly [45] => AngryIntrovertedTallHandsome [46] => AngryIntrovertedTallPlain [47] => AngryIntrovertedTallUgly [48] => AngryIntrovertedShortHandsome [49] => AngryIntrovertedShortPlain [50] => AngryIntrovertedShortUgly [51] => AngryIntrovertedMediumHandsome [52] => AngryIntrovertedMediumPlain [53] => AngryIntrovertedMediumUgly [54] => HopefulOutgoingTallHandsome [55] => HopefulOutgoingTallPlain [56] => HopefulOutgoingTallUgly [57] => HopefulOutgoingShortHandsome [58] => HopefulOutgoingShortPlain [59] => HopefulOutgoingShortUgly [60] => HopefulOutgoingMediumHandsome [61] => HopefulOutgoingMediumPlain [62] => HopefulOutgoingMediumUgly [63] => HopefulIntrovertedTallHandsome [64] => HopefulIntrovertedTallPlain [65] => HopefulIntrovertedTallUgly [66] => HopefulIntrovertedShortHandsome [67] => HopefulIntrovertedShortPlain [68] => HopefulIntrovertedShortUgly [69] => HopefulIntrovertedMediumHandsome [70] => HopefulIntrovertedMediumPlain [71] => HopefulIntrovertedMediumUgly [72] => )
where did I go wrong?
You have a lot of referemces to class variables, like this:
$this->permutationsResultsArray = array();
And PHP complains since this function is not a method in a class. It will work if you just remove this-> so you get:
$permutationsResultsArray = array();
In addition when you are all done you never really return the result.. Like this:
return $permutationsResultsArray;
There is a problem with it though. You are recusing and you don't create that array except in the first round but your code uses it as if it was defined.
BTW: Your function could be much easier with 3 foreach loops:
function getCombinations($traits)
{
$combinations = array('');
foreach( $traits as $trait_level ) {
$new_combinations = array();
foreach ( $combinations as $comb ) {
foreach ( $trait_level as $trait ){
$new_combinations[] = "$comb $trait";
}
}
$combinations = $new_combinations;
}
return $combinations;
}
A function doesn't have a self referential $this. Actually, removing all your $this-> references and make $tempArray a pass by reference instead of by value, your code should work...
function getAllPossiblePermutations($mdArray, $firstTime=true, &$tempArray=array())
I'm using imagettftext to create images of certain text characters. To do this, I need to convert an array of hexidecimal character codes to their HTML equivalents and I can't seem to find any functionality built-in to PHP to do this. What am I missing? (through searching, I came across this: PHP function imagettftext() and unicode, but it none of the answers seem to do what I need - some characters convert but most don't).
Here's the resulting HTML representation (in the browser)
[characters] => Array
(
[33] => A
[34] => B
[35] => C
[36] => D
[37] => E
[38] => F
[39] => G
[40] => H
[41] => I
[42] => J
[43] => K
[44] => L
)
Which comes from this array (not capable of rendering in imagettftext):
[characters] => Array
(
[33] => A
[34] => B
[35] => C
[36] => D
[37] => E
[38] => F
[39] => G
[40] => H
[41] => I
[42] => J
[43] => K
[44] => L
)
Based on a sample from the PHP manual, you could do this with a regex:
$newText = preg_replace('/&#x([a-f0-9]+)/mei', 'chr(0x\\1)', $oldText);
I'm not sure a raw html_entity_decode() would work in your case, as your array elements are missing the trailing ; -- a necessary part of these entities.
EDIT, July 2015:
In response to Ben's comment noting the /e modifier being deprecated, here's how to write this using preg_replace_callback() and an anonymous function:
$newText = preg_replace_callback(
'/&#x([a-f0-9]+)/mi',
function ($m) {
return chr(hexdec($m[1]));
},
$oldText
);
Well, you obviously haven't search hard enough. html_entity_decode().