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.
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 have an array named $info. I want to replace string "YES" found wherever within the array with space or empty string.How can it be done?
Below is the code:
<?php
$conn = mysql_connect("localhost" , "root" , "") or die("Can not connect." . mysql_error());
$db = mysql_select_db("leothian" , $conn) or die("Can not connect." . mysql_error());
$selTable = "select * from dump_hotelbasicinfo ORDER BY id ASC LIMIT 5";
$resultTable = mysql_query($selTable,$conn);
while($rowTable = mysql_fetch_array($resultTable))
{
$selQuery = "SELECT * FROM dump_hotelamenities WHERE HotelCode='$rowTable[HotelCode]' LIMIT 5";
$resultQuery = mysql_query($selQuery);
while($row=mysql_fetch_array($resultQuery))
{
echo "<br> Hotel Code : " .$row['HotelCode'];
$info = array();
$info = (explode(';',$row['PAmenities']));
echo array_search("YES",$info);
echo "<br> Hotel Features : " ;
print_r( $info);
echo "<hr>";
}
}
?>
OUTPUT :
Hotel Features : Array ( [0] => Small pets allowed under 5 kg [1] => YES Small pets allowed under 5 kg [2] => Large pets allowed over 5 kg [3] => YES Large pets allowed over 5 kg [4] => Wheelchair-accessible [5] => YES Wheelchair-accessible [6] => Car park [7] => YES Car park [8] => Garage [9] => YES Garage [10] => Mobile phone coverage [11] => Wired Internet [12] => Wi-fi [13] => Transfer service [14] => Secure parking [15] => Room service [16] => Laundry service [17] => Hotel safe [18] => Cloakroom [19] => Lift access [20] => Newspaper stand [21] => Supermarket [22] => Bicycle storage [23] => Sun terrace [24] => Gym [25] => Newspapers [26] => Restaurant [27] => Non-smoking area [28] => Photocopier [29] => Sun loungers [30] => Children& [31] => apos [32] => s play area [33] => TV lounge [34] => Sauna [35] => Massage [36] => Spa treatments [37] => Year of construction - 2008 [38] => Number of floors main building - 10 [39] => Apartments - 25 [40] => Studios - 1 [41] => Connecting rooms [42] => YES Connecting rooms [43] => Apartment complex [44] => Nearest Bus / Metro Stop - 25000 m [45] => Ski slopes - 2000 m )
A for each loop would work
foreach($info as $k=>$v)
$info[$k] = str_replace('YES','',$v);
<?php
$array = array('this','is','a','test','yes it is','YES it is');
$array = array_map(function($item){
$item = str_replace('YES','', $item);
return $item;
}, $array);
var_dump($array);
function customSearch($keyword, $arrayToSearch){
foreach($arrayToSearch as $key => $arrayItem){
if( stristr( $arrayItem, $keyword ) ){
$arrayToSearch[key] = "yes";
return $key;
}
}
}
I haven't actually tried this but please Refer to this question here if my code is a bit off:
Search for PHP array element containing string
I have three arrays. One that contains the given title information from the user stored in $attributenames. The second has the data from the user stored in $anesdata. (So the first two are related) And the third is a dictionary so that I can get out the information I want using the index which is stored in $medicalInstanceDictionary.
This is the first one:
Array
(
[0] => PATIENT MRN
[1] => PATIENT LAST NAME
[2] => PATIENT FIRST NAME
[3] => PATIENT MIDDLE NAME
[4] => PATIENT SSN
[5] => PATIENT SEX
[6] => INVOICE
[7] => TRANSACTION
[8] => DATE OF PROCEDURE
[9] => POSTING DATE
[10] => LOCATION
[11] => CHARGE AMT
[12] => PROVIDER
[13] => FSC1
[14] => FSC2
[15] => PATIENT DOB
[16] => ATTENDING SURGEON
[17] => ATTENDING SURGEON
[18] => CPT CODES
[19] => ASA CLASSFICATION
[20] => ANESTHESIA CRNA
[21] => ANESTHESIA RESIDENT
[22] => SURGERY START TIME
[23] => SURGERY END TIME
[24] => SVC UNIT
[25] => FACILITY NAME
[26] => BILLING NUMBER
[27] => BILLING AREA NAME
[28] => PROCEDURE MODIFIER1
[29] => PROCEDURE MODIFIER2
[30] => PROCEDURE MODIFIER3
[31] => PRIMARY DX
[32] => SECONDARY DX
)
The second array is a two dimensional array, but each line is equivalent to one patient. So that first patient looks like this(put in x's instead of actual patient data):
[0] => Array
(
[0] => xxxx
[1] => xxxx
[2] => xxxx
[3] => xxxx
[4] => xxxxx
[5] => xxxx
[6] => xxxx
[7] => xxx
[8] => xxxxx
[9] => xxxx
[10] => xxxx
[11] => xxxxx
[12] => xxxx
[13] => xxxxx
[14] => xxxx
[15] => xxxx
[16] => xxxxxxx
[17] => xxxxx
[18] => xxxxx
[19] => xxxx
[20] =>
[21] => xxxxx
[22] => xxxxx
[23] => xxxxx
[24] => xxxxx
[25] => xxxx
[26] => xxxxx
[27] => xxxx
[28] => xxxxxxxx
[29] => xxxx
[30] =>
[31] => xxxxxxx
[32] => xxxxxxx
)
Then the dictionary looks like this:
$medicalInstanceDictionary = array(
'CPT CODES' => "CPT_Code",
'ASA CLASSFICATION' => "MG_ASA_Class",
'FACILITY NAME' => "Facility_Name",
'BILLING NUMBER' => "Billing_Number",
'BILLING AREA NAME' => "Billing_Area_Name",
'PROCEDURE MODIFIER1' => "Procedure_Modifier1",
'PROCEDURE MODIFIER2' => "Procedure_Modifier2",
'PRIMARY DX' => "Primary_Dx",
'SECONDARY DX' => "Secondary_Dx",
'INVOICE' => "FIN"
);
I am doing a nested foreach loop to get each row.
foreach ($dataarray as $dataindex => $datavalue)
{
$out = "";
foreach ($dictionary as $index => $value)
{
//find PATIENT MRN in $attributearray and get it's index
$attributeindex = array_search($index, $attributearray);
if ($attributeindex===FALSE) {
echo "Error : ".$index." not found <br />";
} else {
echo "<br>The attribute is: ".$value." The index is: ".$attributeindex."<br>";
}
(more code....)
}
(more code....)
}
That echo statement looks like this:
The attribute is: CPT_Code The index is: 18
The attribute is: MG_ASA_Class The index is: 19
The attribute is: Facility_Name The index is: 25
The attribute is: Billing_Number The index is: 26
The attribute is: Billing_Area_Name The index is: 27
The attribute is: Procedure_Modifier1 The index is: 28
The attribute is: Procedure_Modifier2 The index is: 29
The attribute is: Primary_Dx The index is: 31
Error : SECONDARY DX not found
The attribute is: FIN The index is: 6
I have no idea why it is skipping over Secondary_Dx. I have checked for spelling errors. I don't think it is my method of doing it because it only does not work for Secondary_Dx. The only thing I can think of is that it does something funky since it is the last element of the array. Has anyone seen this before?
Edit:
Added element(tried both methods, and both resulted in the same looking array using print_r:
//array_push($attributenames, "THE END");
$attributenames[] ="THE END";
echo "<pre>";
print_r($attributenames);
echo "</pre>";
output from that along with the error handling statement from above:
Array
(
[0] => PATIENT MRN
[1] => PATIENT LAST NAME
[2] => PATIENT FIRST NAME
[3] => PATIENT MIDDLE NAME
[4] => PATIENT SSN
[5] => PATIENT SEX
[6] => INVOICE
[7] => TRANSACTION
[8] => DATE OF PROCEDURE
[9] => POSTING DATE
[10] => LOCATION
[11] => CHARGE AMT
[12] => PROVIDER
[13] => FSC1
[14] => FSC2
[15] => PATIENT DOB
[16] => ATTENDING SURGEON
[17] => ATTENDING SURGEON
[18] => CPT CODES
[19] => ASA CLASSFICATION
[20] => ANESTHESIA CRNA
[21] => ANESTHESIA RESIDENT
[22] => SURGERY START TIME
[23] => SURGERY END TIME
[24] => SVC UNIT
[25] => FACILITY NAME
[26] => BILLING NUMBER
[27] => BILLING AREA NAME
[28] => PROCEDURE MODIFIER1
[29] => PROCEDURE MODIFIER2
[30] => PROCEDURE MODIFIER3
[31] => PRIMARY DX
[32] => SECONDARY DX
[33] => THE END
)
This is dictionary array Array
(
[CPT CODES] => CPT_Code
[ASA CLASSFICATION] => MG_ASA_Class
[FACILITY NAME] => Facility_Name
[BILLING NUMBER] => Billing_Number
[BILLING AREA NAME] => Billing_Area_Name
[PROCEDURE MODIFIER1] => Procedure_Modifier1
[PROCEDURE MODIFIER2] => Procedure_Modifier2
[PRIMARY DX] => Primary_Dx
[SECONDARY DX] => Secondary_Dx
[INVOICE] => FIN
)
The attribute is: CPT_Code The index is: 18
The attribute is: MG_ASA_Class The index is: 19
The attribute is: Facility_Name The index is: 25
The attribute is: Billing_Number The index is: 26
The attribute is: Billing_Area_Name The index is: 27
The attribute is: Procedure_Modifier1 The index is: 28
The attribute is: Procedure_Modifier2 The index is: 29
The attribute is: Primary_Dx The index is: 31
Error : SECONDARY DX not found
Array ( [0] => PATIENT MRN [1] => PATIENT LAST NAME [2] => PATIENT FIRST NAME [3] => PATIENT MIDDLE NAME [4] => PATIENT SSN [5] => PATIENT SEX [6] => INVOICE [7] => TRANSACTION [8] => DATE OF PROCEDURE [9] => POSTING DATE [10] => LOCATION [11] => CHARGE AMT [12] => PROVIDER [13] => FSC1 [14] => FSC2 [15] => PATIENT DOB [16] => ATTENDING SURGEON [17] => ATTENDING SURGEON [18] => CPT CODES [19] => ASA CLASSFICATION [20] => ANESTHESIA CRNA [21] => ANESTHESIA RESIDENT [22] => SURGERY START TIME [23] => SURGERY END TIME [24] => SVC UNIT [25] => FACILITY NAME [26] => BILLING NUMBER [27] => BILLING AREA NAME [28] => PROCEDURE MODIFIER1 [29] => PROCEDURE MODIFIER2 [30] => PROCEDURE MODIFIER3 [31] => PRIMARY DX [32] => SECONDARY DX [33] => THE END )
Array ( [CPT CODES] => CPT_Code [ASA CLASSFICATION] => MG_ASA_Class [FACILITY NAME] => Facility_Name [BILLING NUMBER] => Billing_Number [BILLING AREA NAME] => Billing_Area_Name [PROCEDURE MODIFIER1] => Procedure_Modifier1 [PROCEDURE MODIFIER2] => Procedure_Modifier2 [PRIMARY DX] => Primary_Dx [SECONDARY DX] => Secondary_Dx [INVOICE] => FIN )
The attribute is: FIN The index is: 6
You should test for a valid $attributeindex !
$attributeindex = array_search($index, $attributearray);
if ($attributeindex===FALSE) {
echo "Error : ".$index." not found <br />";
} else {
echo "<br>The attribute is: ".$value." The index is: ".$attributeindex."<br>";
}
If you get a not found Error you can be shure $index is not found in $attributearray .
Update :
This is very strange !
From your output we can clearly see.
$index == SECONDARY DX
and
$attributearray has a key [32]
[32] => SECONDARY DX
Only to test :
can you add to $attributearray at the end
[33] => 'END'
and see what happens.
Update 2 :
As i can see in the new output you got with
echo "<pre>";
print_r($attributenames);
echo "</pre>";
There is a empty line between [32] and [33].
There must be a invisible sign at the end of [32] => SECONDARY DX
I suspect a new line character.
Array
(
[0] => PATIENT MRN
[1] => PATIENT LAST NAME
....
[30] => PROCEDURE MODIFIER3
[31] => PRIMARY DX
[32] => SECONDARY DX
[33] => THE END
)
Try to remove that character and it should work .
TIP:
If you ever re experiencing similar behavior, you should check with :
for example:
echo bin2hex($attributenames[32]);
Output in windows look at the end :
5345434f4e444152592044580d0a
Where 0d is CR = Carriage return and 0a is LF = Line feed .
ASCII-Tabelle
Try using trim() on the value you are testing for. If there is white space you can't see at the end it won't match.
$attributeindex = trim( array_search($index, $attributearray) );
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;
}
}