how can I print multiple lines into one csv cell from php? - php

how can I print multiple lines into one csv cell from php?
Array
(
[0] => Array
(
[0] => Payal(102)
[1] => 01:14:22
[2] => 00:58:14
[3] => 02:12:36
[4] => 4
[5] => 4
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] => Payal
[13] =>
[14] =>
[15] =>
)
[1] => Array
(
[0] => VMail(902)
[1] => 00:02:41
[2] => 00:00:00
[3] => 00:02:41
[4] => 1
[5] => 1
[6] => 7
[7] => 0
[8] => 6
[9] => 0
[10] => 7
[11] => 0
[12] =>
[13] =>
[14] =>
[15] =>
)
[2] => Array
(
[0] => NadiaAbdoolla(113)
[1] => 01:44:47
[2] => 03:56:28
[3] => 05:41:15
[4] => 8
[5] => 11
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] => Nadia
[13] => 2019-12-31 08:32:25
[14] => 2019-12-31 15:57:49
[15] => Array
(
[0] => Array
(
[In] => 2019-12-31 08:32:25
[Out] => 2019-12-31 12:35:50
[Total] => 4:3:25
)
[1] => Array
(
[In] => 2019-12-31 12:40:44
[Out] => 2019-12-31 14:13:41
[Total] => 1:32:57
)
[2] => Array
(
[In] => 2019-12-31 14:14:01
[Out] => 2019-12-31 15:57:49
[Total] => 1:43:48
)
)
)
[3] => Array
(
[0] => Huma(202)
[1] => 00:43:10
[2] => 02:25:32
[3] => 03:08:42
[4] => 4
[5] => 8
[6] => 2
[7] => 0
[8] => 2
[9] => 0
[10] => 2
[11] => 0
[12] => Huma
[13] => 2019-12-31 13:11:55
[14] => 2019-12-31 20:55:50
[15] => Array
(
[0] => Array
(
[In] => 2019-12-31 13:11:55
[Out] => 2019-12-31 20:55:50
[Total] => 7:43:55
)
)
)
[4] => Array
(
[0] => Grace(203)
[1] => 01:01:57
[2] => 00:11:04
[3] => 01:13:01
[4] => 4
[5] => 20
[6] => 3
[7] => 0
[8] => 3
[9] => 3
[10] => 2
[11] => 2
[12] => Payal Panda
[13] => 2019-12-31 13:20:56
[14] => 2019-12-31 20:56:45
[15] => Array
(
[0] => Array
(
[In] => 2019-12-31 13:20:56
[Out] => 2019-12-31 20:56:45
[Total] => 7:35:49
)
)
)
[5] => Array
(
[0] => Jarryd(207)
[1] => 02:40:53
[2] => 02:07:09
[3] => 04:48:02
[4] => 21
[5] => 57
[6] => 8
[7] => 0
[8] => 8
[9] => 22
[10] => 8
[11] => 22
[12] => Jarryd
[13] => 2019-12-31 08:29:27
[14] => 2019-12-31 15:51:02
[15] => Array
(
[0] => Array
(
[In] => 2019-12-31 08:29:27
[Out] => 2019-12-31 15:51:02
[Total] => 7:21:35
)
)
)
[6] => Array
(
[0] => Akhil(211)
[1] => 02:06:45
[2] => 00:02:07
[3] => 02:08:52
[4] => 17
[5] => 27
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] => Akhil
[13] => 2019-12-31 11:09:37
[14] => 2019-12-31 20:40:46
[15] => Array
(
[0] => Array
(
[In] => 2019-12-31 11:09:37
[Out] => 2019-12-31 20:40:46
[Total] => 9:31:9
)
)
)
[7] => Array
(
[0] => Vishal(210)
[1] => 02:28:07
[2] => 02:32:59
[3] => 05:01:06
[4] => 6
[5] => 77
[6] => 6
[7] => 0
[8] => 4
[9] => 29
[10] => 2
[11] => 25
[12] => Vishal
[13] => 2019-12-31 11:13:26
[14] => 2019-12-31 19:46:23
[15] => Array
(
[0] => Array
(
[In] => 2019-12-31 11:13:26
[Out] => 2019-12-31 19:46:23
[Total] => 8:32:57
)
)
)
[8] => Array
(
[0] => Joanne(120)
[1] => 01:18:43
[2] => 00:00:00
[3] => 01:18:43
[4] => 3
[5] => 46
[6] => 7
[7] => 3
[8] => 3
[9] => 18
[10] => 5
[11] => 16
[12] => Joanne
[13] => 2019-12-31 08:35:15
[14] => 2019-12-31 15:54:34
[15] => Array
(
[0] => Array
(
[In] => 2019-12-31 08:35:15
[Out] => 2019-12-31 12:08:44
[Total] => 3:33:29
)
[1] => Array
(
[In] => 2019-12-31 12:09:28
[Out] => 2019-12-31 12:23:50
[Total] => 0:14:22
)
[2] => Array
(
[In] => 2019-12-31 12:30:20
[Out] => 2019-12-31 14:02:53
[Total] => 1:32:33
)
[3] => Array
(
[In] => 2019-12-31 14:05:07
[Out] => 2019-12-31 14:11:04
[Total] => 0:5:57
)
[4] => Array
(
[In] => 2019-12-31 14:11:39
[Out] => 2019-12-31 14:28:32
[Total] => 0:16:53
)
[5] => Array
(
[In] => 2019-12-31 14:30:57
[Out] => 2019-12-31 14:45:47
[Total] => 0:14:50
)
[6] => Array
(
[In] => 2019-12-31 14:46:34
[Out] => 2019-12-31 15:24:04
[Total] => 0:37:30
)
[7] => Array
(
[In] => 2019-12-31 15:25:43
[Out] => 2019-12-31 15:54:34
[Total] => 0:28:51
)
)
)
)

This will help you Adding a newline character within a cell (CSV)
csvString += "\""+"Date Generated: \n" ;
csvString += "Doctor: " + "\n"+"\"" + "\n";

Related

Array concatenation of index in php

i have following array format in php:
Array
(
[item] => Array
(
[0] => RSJ Pole 116 x100 ,11 Mtr
[1] => RSJ Pole 116 x100 ,10 Mtr
[2] => RSJ Pole 116 x 100, 9 Mtr
[3] => RSJ Pole 116 x 100, 8 Mtr
[4] => Line Length in Kms
[5] => RSJ 125x70, 9 m long
[6] => PSC Pole 9 Mtr
[7] => PSC Pole 8 Mtr
[8] => Conductor ACSR Dog 0.1
[9] => Conductor ACSR Rabbit 55 Sq.mm
[10] => Conductor ACSR Raccon 80 Sq.mm
[11] => Conductor Weisel 34 Sq.mm
[12] => Conductor Ant
[13] => Conductor Gnat
[14] => New DTC 100 KVA
[15] => New DTC 63 KVA
[16] => BPL Connection
[17] => SDT
)
[scope] => Array
(
[0] => 1172
[1] => 6637
[2] => 854
[3] => 4
[4] => 653.71
[5] => 558.9
[6] => 5864
[7] => 820
[8] => 745385
[9] => 1188772.2
[10] => 327600
[11] => 18900
[12] => 232015.9
[13] => 70634.3
[14] => 344
[15] => 54
[16] => 13632
[17] => 37
)
[till] => Array
(
[0] => 3
[1] => 4
[2] => 4
[3] => 6
[4] => 5
[5] => 5
[6] => 5
[7] => 5
[8] => 2
[9] => 5
[10] => 5
[11] => 5
[12] => 5
[13] => 5
[14] => 5
[15] => 5
[16] => 5
[17] => 2
)
[value] => Array
(
[0] => 4
[1] => 7
[2] => 1
[3] => 2
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
)
[submit] => submit
)
i want to concatenate with "_" operator before submitting or while inserting into database field for eg. 1172_3_4
You can array_map the 3 arrays
$arr = .... //Your array here
$results = array_map(function($s, $t, $v) {
return $s . "_" . $t . "_" . $v;
}, $arr['scope'], $arr['till'], $arr['value']);
echo "<pre>";
print_r( $results );
echo "</pre>";
$results will have the value:
Array
(
[0] => 1172_3_4
[1] => 6637_4_7
[2] => 854_4_1
[3] => 4_6_2
[4] => 653.71_5_
[5] => 558.9_5_
[6] => 5864_5_
[7] => 820_5_
[8] => 745385_2_
[9] => 1188772.2_5_
[10] => 327600_5_
[11] => 18900_5_
[12] => 232015.9_5_
[13] => 70634.3_5_
[14] => 344_5_
[15] => 54_5_
[16] => 13632_5_
[17] => 37_2_
)
Doc: array_map()
Implement the loop at main array and increment in $i then concatenate $array_variable['scope'][$i].'_'.$array_variable['till'][$i].'_'.$array_variable['value'][$i].

How to Change XLXS excel data into a multidimensional array

enter image description here
How can I convert this XLXS excel file into following array format. Also I attached the Screenshot of Excel file. Please review it and let me know
I am using this library to fetch the data from Excel file which i have mentioned given below:-
require_once "EXCEL/simplexlsx.class.php";
$xlsx = new SimpleXLSX('EXCEL/Testingexcel.xlsx');
Excel Array
$xlsx->rows()
Array
(
[0] => Array
(
[0] => Section / Department
1 => Notes
[2] => No
[3] => Great Offer
[4] => Code
[5] => Use Photo
[6] => Item Number
[7] => English Description
[8] => Greek Description
[9] => Price From
[10] => Price
[11] => Text1
[12] => Exclusive Brand
[13] => Date From
[14] => Date To
)
[1] => Array
(
[0] => Chair
[1] => Cover
[2] => 1
[3] =>
[4] =>
[5] =>
[6] =>
[7] => etge
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[2] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP1
[5] =>
[6] =>
[7] => wefewf
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP2
[5] =>
[6] =>
[7] => TITLE 1
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP3
[5] =>
[6] =>
[7] => TITLE 12
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP4
[5] =>
[6] =>
[7] => TITLE 13
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[6] => Array
(
[0] =>
[1] =>
[2] => 2
[3] =>
[4] => AP5
[5] =>
[6] =>
[7] => TITLE 14
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[7] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP6
[5] =>
[6] =>
[7] => TITLE 15
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[8] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP7
[5] =>
[6] =>
[7] => TITLE 16
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[9] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP8
[5] =>
[6] =>
[7] => TITLE 17
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[10] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP9
[5] => Y
[6] =>
[7] => TITLE 18
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[11] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP10
[5] =>
[6] =>
[7] => TITLE 19
[8] =>
[9] =>
[10] =>
[11] => 40%
[12] =>
[13] =>
[14] =>
)
[12] => Array
(
[0] => Table
[1] => cover
[2] => 2
[3] =>
[4] =>
[5] =>
[6] =>
[7] => TITLE 20
[8] =>
[9] =>
[10] =>
[11] => 1+1
[12] =>
[13] =>
[14] =>
)
[13] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP11
[5] =>
[6] =>
[7] => TITLE 145
[8] =>
[9] =>
[10] =>
[11] => 1+1
[12] =>
[13] =>
[14] =>
)
[14] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => AP12
[5] =>
[6] =>
[7] => TITLE 1657
[8] =>
[9] =>
[10] =>
[11] => 1+1
[12] =>
[13] =>
[14] =>
)
[15] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[16] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
[17] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
)
foreach ($xlsx->rows() as $key => $valueRows){
}
Array [0] => Array
(
[chair] => Array
(
[offer 1] => Array
(
[0] => AP1
[1] => AP2
[2] => AP3
[3] => AP4
)
[offer 2] => Array
(
[0] => AP5
[1] => AP6
[2] => AP7
[3] => AP8
[4] => AP9
)
)
[table] => Array
(
[offer 1] => Array
(
[0] => AP10
)
)
)

Parsing a complex excel file using PHP Excel Library and retrieve data for email templates

I want to parse the following excel file using PHP Excel Library and put the data into email templates(PFA).
Here goes the corresponding code for parsing the data:
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($target_file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($target_file);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($target_file,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
$val=array();
for ($col = 0; $col < $highestColumn; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
// Insert row data array into your database of choice here
print_r(array_values($rowData));
The output is:
Array ( [0] => Array ( [0] => S No [1] => Hotel Name [2] => Room Category [3] => GuestHouse Id [4] => Hotel Email [5] => City [6] => state [7] => 2nd Feb [8] => 3rd Feb [9] => 4th Feb [10] => 5th Feb [11] => 6th Feb [12] => 7th Feb [13] => 8th Feb [14] => 9th Feb [15] => 10th Feb [16] => 11th Feb [17] => 12th Feb [18] => 13th Feb [19] => 14th Feb [20] => 15th Feb ) ) Array ( [0] => Array ( [0] => 1 [1] => Hotel 1 [2] => All [3] => 19 [4] => a#y.com [5] => Ahmedabad [6] => Gujarat [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Standard [3] => [4] => [5] => [6] => [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Superior [3] => [4] => [5] => [6] => [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 ) ) Array ( [0] => Array ( [0] => 2 [1] => Hotel 2 [2] => All [3] => 18 [4] => y#a.in [5] => Mumbai [6] => Maharashtra [7] => 2 [8] => 3 [9] => 4 [10] => 4 [11] => 4 [12] => 4 [13] => 4 [14] => 4 [15] => 4 [16] => 4 [17] => 4 [18] => 4 [19] => 2 [20] => 3 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Standard [3] => [4] => [5] => [6] => [7] => 0 [8] => 1 [9] => 2 [10] => 2 [11] => 2 [12] => 2 [13] => 2 [14] => 2 [15] => 2 [16] => 2 [17] => 2 [18] => 2 [19] => 0 [20] => 1 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Superior [3] => [4] => [5] => [6] => [7] => 2 [8] => 2 [9] => 2 [10] => 2 [11] => 2 [12] => 2 [13] => 2 [14] => 2 [15] => 2 [16] => 2 [17] => 2 [18] => 2 [19] => 2 [20] => 2 ) ) Array ( [0] => Array ( [0] => 3 [1] => Hotel 3 [2] => All [3] => 199 [4] => m#mm.co.in [5] => Amravati [6] => Maharashtra [7] => 20 [8] => 20 [9] => 20 [10] => 20 [11] => 20 [12] => 20 [13] => 20 [14] => 20 [15] => 20 [16] => 20 [17] => 20 [18] => 20 [19] => 20 [20] => 20 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Standard [3] => [4] => [5] => [6] => [7] => 10 [8] => 10 [9] => 10 [10] => 10 [11] => 10 [12] => 10 [13] => 10 [14] => 10 [15] => 10 [16] => 10 [17] => 10 [18] => 10 [19] => 10 [20] => 10 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Superior [3] => [4] => [5] => [6] => [7] => 10 [8] => 10 [9] => 10 [10] => 10 [11] => 10 [12] => 10 [13] => 10 [14] => 10 [15] => 10 [16] => 10 [17] => 10 [18] => 10 [19] => 10 [20] => 10 ) ) Array ( [0] => Array ( [0] => 4 [1] => Hotel 4 [2] => All [3] => 35 [4] => a#c.net [5] => Lonavala [6] => Maharashtra [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 [20] => 1 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Villa [3] => [4] => [5] => [6] => [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 [20] => 1 ) )
Now, what I want is after I have have this multi-dimensional associative array in place, I want to extract the data in such a way so as to get this expected output.
This is the place where I am stuck. I am unable to extract data properly because on one hand I need to skip empty null fields whereas on the other hand I need to skip the "All" field. Any help or suggestion is highly appreciated.

Enjin API (JSON) to PHP part 2

I'm trying to figure out how to get the data from http://www.natomilcorp.com/api/get-tags
But I doesn't work the way it was working with http://www.natomilcorp.com/api/get-users
<?php
$content = file_get_contents('http://natomilcorp.com/api/get-tags');
$array = json_decode($content, TRUE);
foreach ($array as $key => $value) {
}
?>
Should return something like : Major
There's the id for the rank + id of each user with that rank. How could I do that?
Data I'm trying to get.
Array ( [tags] => Array ( [766910] => Array ( [name] => Major [visible] => 2 [users] => Array ( [0] => 200159 ) ) [766912] => Array ( [name] => Captain [visible] => 2 [users] => Array ( [0] => 2261591 ) ) [766913] => Array ( [name] => 1st Lieutenant [visible] => 2 [users] => Array ( [0] => 9054053 ) ) [766914] => Array ( [name] => 2nd Lieutenant [visible] => 2 [users] => Array ( [0] => 8987889 ) ) [773490] => Array ( [name] => Sergeant First Class [visible] => 2 [users] => Array ( [0] => 7725577 ) ) [773492] => Array ( [name] => Sergeant [visible] => 1 [users] => Array ( [0] => 1060956 [1] => 7302252 [2] => 8757300 [3] => 8863799 [4] => 8965561 [5] => 9261041 ) ) [773493] => Array ( [name] => Corporal [visible] => 2 [users] => Array ( [0] => 3021405 [1] => 7007387 [2] => 9139763 [3] => 9213371 ) ) [773494] => Array ( [name] => Private First Class [visible] => 2 [users] => Array ( [0] => 2200849 [1] => 3641961 [2] => 9178511 [3] => 9261322 [4] => 9323849 [5] => 9389371 [6] => 9430088 ) ) [773495] => Array ( [name] => Private [visible] => 2 [users] => Array ( [0] => 114357 [1] => 261041 [2] => 304466 [3] => 549312 [4] => 1118346 [5] => 1710685 [6] => 2141867 [7] => 3211667 [8] => 3310027 [9] => 3553805 [10] => 4528862 [11] => 7379434 [12] => 8403949 [13] => 8428123 [14] => 8650774 [15] => 8910960 [16] => 9010718 [17] => 9141940 [18] => 9223268 [19] => 9276028 [20] => 9308336 [21] => 9324049 [22] => 9343007 [23] => 9379991 [24] => 9388650 [25] => 9388744 [26] => 9501913 ) ) [773499] => Array ( [name] => Recruit [visible] => 2 [users] => Array ( [0] => 89975 [1] => 205838 [2] => 384035 [3] => 1607344 [4] => 3131066 [5] => 6461552 [6] => 7126984 [7] => 7233456 [8] => 8594187 [9] => 9210020 [10] => 9319294 [11] => 9324467 [12] => 9368201 [13] => 9401020 [14] => 9401208 [15] => 9411271 [16] => 9412511 [17] => 9442152 [18] => 9472151 [19] => 9481048 [20] => 9507716 [21] => 9519155 [22] => 9519665 [23] => 9524202 [24] => 9524528 [25] => 9527256 [26] => 9528148 [27] => 9528464 [28] => 9532974 [29] => 9532993 ) ) [794713] => Array ( [name] => NMC Member [visible] => 0 [users] => Array ( [0] => 89975 [1] => 114357 [2] => 200159 [3] => 205838 [4] => 304466 [5] => 384035 [6] => 549312 [7] => 857679 [8] => 870831 [9] => 1060956 [10] => 1118346 [11] => 1607344 [12] => 1710685 [13] => 2141867 [14] => 2200849 [15] => 2261591 [16] => 3021405 [17] => 3131066 [18] => 3211667 [19] => 3310027 [20] => 3553805 [21] => 3641961 [22] => 4528862 [23] => 5888412 [24] => 6461552 [25] => 7007387 [26] => 7126984 [27] => 7233456 [28] => 7302252 [29] => 7379434 [30] => 7725577 [31] => 8403949 [32] => 8428123 [33] => 8594187 [34] => 8650774 [35] => 8757300 [36] => 8863799 [37] => 8910960 [38] => 8965561 [39] => 8987889 [40] => 9010718 [41] => 9017629 [42] => 9054053 [43] => 9139763 [44] => 9141940 [45] => 9178511 [46] => 9210020 [47] => 9213371 [48] => 9223268 [49] => 9261041 [50] => 9261322 [51] => 9276028 [52] => 9289688 [53] => 9308336 [54] => 9319294 [55] => 9323849 [56] => 9324049 [57] => 9324467 [58] => 9343007 [59] => 9361657 [60] => 9368201 [61] => 9379991 [62] => 9388650 [63] => 9388744 [64] => 9389371 [65] => 9401020 [66] => 9401208 [67] => 9411271 [68] => 9412511 [69] => 9430088 [70] => 9442152 [71] => 9472151 [72] => 9481048 [73] => 9501913 [74] => 9507716 [75] => 9519155 [76] => 9519665 [77] => 9524202 [78] => 9524528 [79] => 9527256 [80] => 9528148 [81] => 9528464 [82] => 9532974 [83] => 9532993 ) ) [905780] => Array ( [name] => Reserve / Inactive [visible] => 2 [users] => Array ( [0] => 1116431 [1] => 1146450 [2] => 1327008 [3] => 3488556 [4] => 4057254 [5] => 4109011 [6] => 4442271 [7] => 5075797 [8] => 6348486 [9] => 6727549 [10] => 7482138 [11] => 7669434 [12] => 7810956 [13] => 7813040 [14] => 7845349 [15] => 7873320 [16] => 8163829 [17] => 8610931 [18] => 8720229 [19] => 8758932 [20] => 8944586 [21] => 8988410 [22] => 9009221 [23] => 9016644 [24] => 9143121 ) ) [916781] => Array ( [name] => High Command [visible] => 2 [users] => Array ( [0] => 192868 ) ) [950454] => Array ( [name] => (AF) 2nd Lieutenant [visible] => 2 [users] => Array ( [0] => 870831 [1] => 5888412 [2] => 9017629 [3] => 9289688 [4] => 9361657 ) ) [950472] => Array ( [name] => (AF) 1st Lieutenant [visible] => 2 [users] => Array ( [0] => 857679 ) ) [964515] => Array ( [name] => 8 Week Service Ribbon 1A [visible] => 2 [users] => Array ( [0] => 857679 [1] => 3021405 [2] => 8428123 ) ) [964541] => Array ( [name] => 8 Week Service Ribbon 2A [visible] => 2 [users] => Array ( [0] => 7725577 ) ) [964552] => Array ( [name] => 8 Week Service Ribbon 4A [visible] => 2 [users] => Array ( [0] => 200159 ) ) [964580] => Array ( [name] => Altis War Service Ribbon [visible] => 2 [users] => Array ( [0] => 200159 [1] => 857679 [2] => 870831 [3] => 1060956 [4] => 2261591 [5] => 5888412 [6] => 7302252 [7] => 7725577 [8] => 8757300 [9] => 8863799 [10] => 8965561 [11] => 9054053 ) ) [964632] => Array ( [name] => Commanding Officers Commendation [visible] => 2 [users] => Array ( [0] => 2261591 [1] => 9054053 [2] => 9139763 [3] => 9261322 ) ) [964807] => Array ( [name] => Executive Officers Commendation [visible] => 2 [users] => Array ( [0] => 5888412 [1] => 8863799 [2] => 9017629 ) )
It should be
echo $array['tags'][$key]['name'];
You could see there is a tags key , so you need to add that too while accessing.
Working code..
<?php
$content = file_get_contents('http://natomilcorp.com/api/get-tags');
$array = json_decode($content, TRUE);
$key = "766910";
echo $array['tags'][$key]['name'];

PHP reformatting array

I have an array like this
Array
(
[0] => 125 c.c.
[1] => Array
(
[0] => AK 125 SL
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
[2] => Array
(
[0] => EVO 125 NE
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
[3] => 150 c.c.
[4] => Array
(
[0] => EVO 150 NE
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
[5] => Array
(
[0] => RXT 150
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
)
I want to format it in a way so it ends up like this:
Array
(
[125 c.c.] => Array
(
[0]=>Array
(
[0] => AK 125 SL
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
[1]=> Array
(
[0] => EVO 125 NE
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
)
[150 c.c.] =>
ARRAY
(
[0] => Array
(
[0] => EVO 150 NE
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
[1] => Array
(
[0] => RXT 150
[1] => 12/13
[2] => R
[3] => 1201X
[4] => L
[5] => FX22,0
[6] => 220
[7] => 58
[8] => 4
[9] => 3
[10] => 8,5
[11] => Drum
[12] => -
[13] => -
[14] => -
[15] => -
[16] => -
[17] => -
[18] => -
)
)
)
Currently I've no idea any help would be greatly appreciated.
foreach($array as $val) {
if(!is_array($val))
{
$finalArray[go here as key]=if not then Go here as value to current key;
}
}
$currentKey = '';
$newArray = array();
foreach ($array as $val) {
if (!is_array($val)) {
$currentKey = $val;
} else {
if (isset($newArray[$currentKey]) && is_array($newArray[$currentKey])) {
$newArray[$currentKey][] = $val;
} else {
$newArray[$currentKey] = array($val);
}
}
}
I hope you understand what is happening here?
And of course it'll work only with arrays like first from your question.

Categories