handling an Array of objects - php

I have a JSON array of objects each object contains a MySQL database row that also contains information about an item. my question is how can I get a certain key-value from an object depending on another key? let me get it straight, items is a JSON array contains a couple of objects each object is generated with both numeric and string keys, So what I need is to get the value of the price key that in the same Object with a specific codenumber basically getting the price value from the codenumber key
the Second thing is how to add a new key within a new value to the object? like if I have a variable called $Count how can I add his value to the object within a new key like Count? is the function array_push can add a new key to the Object?
the JSON array
Array (
[0] => stdClass Object (
[0] => 1
[1] => o4
[2] => Efexor
[3] => Eklil
[4] => 1
[5] => 30
[6] => 15
[7] => Phezer
[8] => Venlafaxine
[9] =>
[10] => 0
[11] => 2020-03-25
[12] => 2021-08-25
[13] => 1
[14] => 0
[15] => 0
[16] => caps
[17] => ..\upload\Efexor.jpg
[id] => 1
[qty] => 15
[Sale] => 1
[name] => Efexor
[bonus] => 0
[price] => 30
[company] => Phezer
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\Efexor.jpg
[owned-com] => Eklil
[pack-size] => 0
[codenumber] => o4
[chemicalcom] => Venlafaxine
[dosage-form] => caps
[concentration] =>
[expiration-date] => 2021-08-25
[production-date] => 2020-03-25
)
[1] => stdClass Object (
[0] => 2
[1] =>
[2] => Efexor
[3] => Eklil
[4] => 1
[5] => 15
[6] => 10
[7] => Phezer
[8] => Venlafaxine
[9] =>
[10] => 0
[11] => 2020-03-25
[12] => 2021-08-25
[13] => 0
[14] => 0
[15] => 0
[16] => caps
[17] => ..\upload\Efexor.jpg
[id] => 2
[qty] => 10
[Sale] => 0
[name] => Efexor
[bonus] => 0
[price] => 15
[company] => Phezer
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\Efexor.jpg
[owned-com] => Eklil
[pack-size] => 0
[codenumber] =>
[chemicalcom] => Venlafaxine
[dosage-form] => caps
[concentration] =>
[expiration-date] => 2021-08-25
[production-date] => 2020-03-25
)
[2] => stdClass Object (
[0] => 3
[1] =>
[2] => panadol
[3] => com2
[4] => 1
[5] => 5
[6] => 12
[7] => GSK
[8] => Paracetamol
[9] =>
[10] => 0
[11] => 2020-04-09
[12] => 2021-02-09
[13] => 0
[14] => 0
[15] => 2
[16] => taps
[17] => ..\upload\panadol.png
[id] => 3
[qty] => 12
[Sale] => 0
[name] => panadol
[bonus] => 2
[price] => 5
[company] => GSK
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] => com2
[pack-size] => 0
[codenumber] =>
[chemicalcom] => Paracetamol
[dosage-form] => taps
[concentration] =>
[expiration-date] => 2021-02-09
[production-date] => 2020-04-09
)
[3] => stdClass Object (
[0] => 4
[1] => codenumber
[2] => name
[3] => owned
[4] => 1
[5] => 9
[6] => 1
[7] => gsk
[8] => gscom
[9] => 12mg
[10] => 3
[11] => 2020-03-25
[12] => 2021-02-09
[13] => 1
[14] => 0
[15] => 0
[16] => caps
[17] => ..\upload\panadol.png
[id] => 4
[qty] => 1
[Sale] => 1
[name] => name
[bonus] => 0
[price] => 9
[company] => gsk
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] => owned
[pack-size] => 3
[codenumber] => codenumber
[chemicalcom] => gscom
[dosage-form] => caps
[concentration] => 12mg
[expiration-date] => 2021-02-09
[production-date] => 2020-03-25
)
[4] => stdClass Object (
[0] => 5
[1] => d322
[2] => fsdfds
[3] => ewfwef
[4] => 1
[5] => 3232
[6] => 222
[7] => none
[8] => nonoe
[9] => nonoe
[10] => 12
[11] => 2020-03-25
[12] => 2021-02-09
[13] => 1
[14] => 0
[15] => 2
[16] => caps
[17] => ..\upload\panadol.png
[id] => 5
[qty] => 222
[Sale] => 1
[name] => fsdfds
[bonus] => 2
[price] => 3232
[company] => none
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] => ewfwef
[pack-size] => 12
[codenumber] => d322
[chemicalcom] => nonoe
[dosage-form] => caps
[concentration] => nonoe
[expiration-date] => 2021-02-09
[production-date] => 2020-03-25
)
[5] => stdClass Object (
[0] => 6
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 0
[6] => 0
[7] =>
[8] =>
[9] =>
[10] => 0
[11] => 0000-00-00
[12] => 0000-00-00
[13] => 0
[14] => 0
[15] => 0
[16] => eyedrop
[17] => ..\upload\panadol.png
[id] => 6
[qty] => 0
[Sale] => 0
[name] =>
[bonus] => 0
[price] => 0
[company] =>
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] =>
[pack-size] => 0
[codenumber] =>
[chemicalcom] =>
[dosage-form] => eyedrop
[concentration] =>
[expiration-date] => 0000-00-00
[production-date] => 0000-00-00
)
[6] => stdClass Object (
[0] => 7
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 0
[6] => 0
[7] =>
[8] =>
[9] =>
[10] => 0
[11] => 0000-00-00
[12] => 0000-00-00
[13] => 0
[14] => 0
[15] => 0
[16] =>
[17] => ..\upload\panadol.png
[id] => 7
[qty] => 0
[Sale] => 0
[name] =>
[bonus] => 0
[price] => 0
[company] =>
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] =>
[pack-size] => 0
[codenumber] =>
[chemicalcom] =>
[dosage-form] =>
[concentration] =>
[expiration-date] => 0000-00-00
[production-date] => 0000-00-00
)
[7] => stdClass Object (
[0] => 8
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 0
[6] => 0
[7] =>
[8] =>
[9] =>
[10] => 0
[11] => 0000-00-00
[12] => 0000-00-00
[13] => 0
[14] => 0
[15] => 0
[16] => eardrop
[17] => ..\upload\panadol.png
[id] => 8
[qty] => 0
[Sale] => 0
[name] =>
[bonus] => 0
[price] => 0
[company] =>
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] =>
[pack-size] => 0
[codenumber] =>
[chemicalcom] =>
[dosage-form] => eardrop
[concentration] =>
[expiration-date] => 0000-00-00
[production-date] => 0000-00-00
)
)
my attempt to make the first function core.php:-
<?PHP
public function getPrice($id,$codenumber){
if($this->connected === true){
try{
$items = $this->connection->prepare("SELECT `items` FROM `bills` WHERE id=:id");
$items->bindParam(":id",$id);
$items->execute();
$fetchedItems = $items->fetchColumn();
$items = json_decode($fetchedItems);
foreach ($items as $item){
$price = $item->price;
echo $price; // What i want is to echo the Price-Key for the value that has the $codenumber [key]
}
}
catch(PDOException $e){
if($this->errors === true){
return $this->error($e->getMessage());
}else{
return false;
}
}
}
}
?>

Assuming that codenumber is unique in the array, just re-index your array with codenumber and then it's simple (PHP >= 7):
$codenumber = 'o4';
$array = array_column($array, null, 'codenumber');
echo $array[$codenumber]->price; // outputs 30
That's reusable multiple times. If you just need to do it once:
echo array_column($array, null, 'codenumber')[$codenumber]->price;

I believe what you are looking for is something like this, where $id is the value you are searching for and $json is your array. If count doesn't exist, it will automatically be created with a value of 0.
$id = 100;
$items = json_decode($fetchedItems, true);
foreach($items as $k => $v){
if(isset($json[$k]["count"]) === false){
$json[$k]["count"] = 0;
}
if($v["codenumber"] == $id){
$json[$k]["count"] += $v["price"];
}
}

Related

How to Parse an StdClass object

I encoded a MySQL table array to JSON and insert it to a table in specific JSON datatype column because I want to Parse this array into an (HTML table) using PHP of course, so when I run this code block
<?PHP
$stmts = $conn->prepare('SELECT `items` FROM `bills` WHERE id=3');
$stmts->execute();
$result = $stmts->fetchColumn();
$decoded = json_decode($result);
print_r($decoded);
foreach ($decoded[0] as $key => $value) {
echo "<br>Column: ".$key."<br>";
echo "value: ".$value."<br>";
}
?>
I realized that $decoded is a StdClass Object and The Output from print_r is:-
Array (
[0] => stdClass Object (
[0] => 1
[1] => o4
[2] => Efexor
[3] => Eklil
[4] => 1
[5] => 30
[6] => 15
[7] => Phezer
[8] => Venlafaxine
[9] =>
[10] => 0
[11] => 2020-03-25
[12] => 2021-08-25
[13] => 1
[14] => 0
[15] => 0
[16] => caps
[17] => ..\upload\Efexor.jpg
[id] => 1
[qty] => 15
[Sale] => 1
[name] => Efexor
[bonus] => 0
[price] => 30
[company] => Phezer
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\Efexor.jpg
[owned-com] => Eklil
[pack-size] => 0
[codenumber] => o4
[chemicalcom] => Venlafaxine
[dosage-form] => caps
[concentration] =>
[expiration-date] => 2021-08-25
[production-date] => 2020-03-25
)
[1] => stdClass Object (
[0] => 2
[1] =>
[2] => Efexor
[3] => Eklil
[4] => 1
[5] => 15
[6] => 10
[7] => Phezer
[8] => Venlafaxine
[9] =>
[10] => 0
[11] => 2020-03-25
[12] => 2021-08-25
[13] => 0
[14] => 0
[15] => 0
[16] => caps
[17] => ..\upload\Efexor.jpg
[id] => 2
[qty] => 10
[Sale] => 0
[name] => Efexor
[bonus] => 0
[price] => 15
[company] => Phezer
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\Efexor.jpg
[owned-com] => Eklil
[pack-size] => 0
[codenumber] =>
[chemicalcom] => Venlafaxine
[dosage-form] => caps
[concentration] =>
[expiration-date] => 2021-08-25
[production-date] => 2020-03-25
)
[2] => stdClass Object (
[0] => 3
[1] =>
[2] => panadol
[3] => com2
[4] => 1
[5] => 5
[6] => 12
[7] => GSK
[8] => Paracetamol
[9] =>
[10] => 0
[11] => 2020-04-09
[12] => 2021-02-09
[13] => 0
[14] => 0
[15] => 2
[16] => taps
[17] => ..\upload\panadol.png
[id] => 3
[qty] => 12
[Sale] => 0
[name] => panadol
[bonus] => 2
[price] => 5
[company] => GSK
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] => com2
[pack-size] => 0
[codenumber] =>
[chemicalcom] => Paracetamol
[dosage-form] => taps
[concentration] =>
[expiration-date] => 2021-02-09
[production-date] => 2020-04-09
)
[3] => stdClass Object (
[0] => 4
[1] => codenumber
[2] => name
[3] => owned
[4] => 1
[5] => 9
[6] => 1
[7] => gsk
[8] => gscom
[9] => 12mg
[10] => 3
[11] => 2020-03-25
[12] => 2021-02-09
[13] => 1
[14] => 0
[15] => 0
[16] => caps
[17] => ..\upload\panadol.png
[id] => 4
[qty] => 1
[Sale] => 1
[name] => name
[bonus] => 0
[price] => 9
[company] => gsk
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] => owned
[pack-size] => 3
[codenumber] => codenumber
[chemicalcom] => gscom
[dosage-form] => caps
[concentration] => 12mg
[expiration-date] => 2021-02-09
[production-date] => 2020-03-25
)
[4] => stdClass Object (
[0] => 5
[1] => d322
[2] => fsdfds
[3] => ewfwef
[4] => 1
[5] => 3232
[6] => 222
[7] => none
[8] => nonoe
[9] => nonoe
[10] => 12
[11] => 2020-03-25
[12] => 2021-02-09
[13] => 1
[14] => 0
[15] => 2
[16] => caps
[17] => ..\upload\panadol.png
[id] => 5
[qty] => 222
[Sale] => 1
[name] => fsdfds
[bonus] => 2
[price] => 3232
[company] => none
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] => ewfwef
[pack-size] => 12
[codenumber] => d322
[chemicalcom] => nonoe
[dosage-form] => caps
[concentration] => nonoe
[expiration-date] => 2021-02-09
[production-date] => 2020-03-25
)
[5] => stdClass Object (
[0] => 6
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 0
[6] => 0
[7] =>
[8] =>
[9] =>
[10] => 0
[11] => 0000-00-00
[12] => 0000-00-00
[13] => 0
[14] => 0
[15] => 0
[16] => eyedrop
[17] => ..\upload\panadol.png
[id] => 6
[qty] => 0
[Sale] => 0
[name] =>
[bonus] => 0
[price] => 0
[company] =>
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] =>
[pack-size] => 0
[codenumber] =>
[chemicalcom] =>
[dosage-form] => eyedrop
[concentration] =>
[expiration-date] => 0000-00-00
[production-date] => 0000-00-00
)
[6] => stdClass Object (
[0] => 7
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 0
[6] => 0
[7] =>
[8] =>
[9] =>
[10] => 0
[11] => 0000-00-00
[12] => 0000-00-00
[13] => 0
[14] => 0
[15] => 0
[16] =>
[17] => ..\upload\panadol.png
[id] => 7
[qty] => 0
[Sale] => 0
[name] =>
[bonus] => 0
[price] => 0
[company] =>
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] =>
[pack-size] => 0
[codenumber] =>
[chemicalcom] =>
[dosage-form] =>
[concentration] =>
[expiration-date] => 0000-00-00
[production-date] => 0000-00-00
)
[7] => stdClass Object (
[0] => 8
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 0
[6] => 0
[7] =>
[8] =>
[9] =>
[10] => 0
[11] => 0000-00-00
[12] => 0000-00-00
[13] => 0
[14] => 0
[15] => 0
[16] => eardrop
[17] => ..\upload\panadol.png
[id] => 8
[qty] => 0
[Sale] => 0
[name] =>
[bonus] => 0
[price] => 0
[company] =>
[draftid] => 1
[discount] => 0
[imageurl] => ..\upload\panadol.png
[owned-com] =>
[pack-size] => 0
[codenumber] =>
[chemicalcom] =>
[dosage-form] => eardrop
[concentration] =>
[expiration-date] => 0000-00-00
[production-date] => 0000-00-00
)
)
so how can I parse this Object to a (table)

how can I print multiple lines into one csv cell from 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";

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.

Loop to read array

sorry if that can be a simple question. I need to read the index value of an array. The code that I have and try to correct read only the index 0. I have try with a ciclebut continue to make mistakes.
<? if($entrata != ""){
$nome_condomino = getCondominoByIdEntrata($entrata[0]['id']);
$saldo += (float) $entrata[0]['importo_versato'];
//echo '<pre>' . print_r( $entrata,true ) . '</pre>';
?>
<tr class="gradeX">
<td class="center"><?echo $giorno;?></td>
<td class="center"><?echo $nome_condomino[0]['nome_cognome'];?></td>
<td class="center"><b><?echo "€ ".$entrata[0]['importo_versato'];?></b></td>
<td class="center"><?echo "€ ".$saldo;?></td>
<td class="center"><?echo $giorno_ok;?></td>
<td class="center"><?echo $entrata_id[0];?></td>
</tr>
<? } ?>
I need to show the other index value for the variables $nome_condominio and $entrata
That is print_r( $entrata,true )
Array
(
[0] => Array
(
[0] => 41
[id] => 41
[1] => 35
[id_condominio] => 35
[2] => 38
[id_condomino] => 38
[3] => 12
[importo_versato] => 12
[4] => 2013-11-30
[data] => 2013-11-30
[5] =>
[numero_ricevuta] =>
[6] =>
[allegato_ricevuta] =>
[7] => 0
[consumi_idrici_importo] => 0
[8] => 0000-00-00
[data2] => 0000-00-00
[9] =>
[allegato_consumi_idrici] =>
[10] => 0
[lavori_straordinari_importo] => 0
[11] => 0000-00-00
[data3] => 0000-00-00
[12] =>
[causale] =>
[13] =>
[causale_altro] =>
[14] => 10_2013
[causale_mese] => 10_2013
[15] => Ottobre 2013
[causale_mese_periodo_esteso] => Ottobre 2013
)
[1] => Array
(
[0] => 40
[id] => 40
[1] => 35
[id_condominio] => 35
[2] => 52
[id_condomino] => 52
[3] => 18.2
[importo_versato] => 18.2
[4] => 2013-11-30
[data] => 2013-11-30
[5] =>
[numero_ricevuta] =>
[6] =>
[allegato_ricevuta] =>
[7] => 0
[consumi_idrici_importo] => 0
[8] => 0000-00-00
[data2] => 0000-00-00
[9] =>
[allegato_consumi_idrici] =>
[10] => 0
[lavori_straordinari_importo] => 0
[11] => 0000-00-00
[data3] => 0000-00-00
[12] => 2
[causale] => 2
[13] =>
[causale_altro] =>
[14] =>
[causale_mese] =>
[15] =>
[causale_mese_periodo_esteso] =>
)
)
Array
(
[0] => Array
(
[0] => 39
[id] => 39
[1] => 35
[id_condominio] => 35
[2] => 34
[id_condomino] => 34
[3] => 12.5
[importo_versato] => 12.5
[4] => 2013-12-01
[data] => 2013-12-01
[5] =>
[numero_ricevuta] =>
[6] =>
[allegato_ricevuta] =>
[7] => 0
[consumi_idrici_importo] => 0
[8] => 0000-00-00
[data2] => 0000-00-00
[9] =>
[allegato_consumi_idrici] =>
[10] => 0
[lavori_straordinari_importo] => 0
[11] => 0000-00-00
[data3] => 0000-00-00
[12] => 2
[causale] => 2
[13] =>
[causale_altro] =>
[14] =>
[causale_mese] =>
[15] =>
[causale_mese_periodo_esteso] =>
)
[1] => Array
(
[0] => 44
[id] => 44
[1] => 35
[id_condominio] => 35
[2] => 51
[id_condomino] => 51
[3] => 10
[importo_versato] => 10
[4] => 2013-12-01
[data] => 2013-12-01
[5] =>
[numero_ricevuta] =>
[6] =>
[allegato_ricevuta] =>
[7] => 0
[consumi_idrici_importo] => 0
[8] => 0000-00-00
[data2] => 0000-00-00
[9] =>
[allegato_consumi_idrici] =>
[10] => 0
[lavori_straordinari_importo] => 0
[11] => 0000-00-00
[data3] => 0000-00-00
[12] => 2
[causale] => 2
[13] =>
[causale_altro] =>
[14] =>
[causale_mese] =>
[15] =>
[causale_mese_periodo_esteso] =>
)
)
Array
(
[0] => Array
(
[0] => 42
[id] => 42
[1] => 35
[id_condominio] => 35
[2] => 39
[id_condomino] => 39
[3] => 10
[importo_versato] => 10
[4] => 2013-12-03
[data] => 2013-12-03
[5] =>
[numero_ricevuta] =>
[6] =>
[allegato_ricevuta] =>
[7] => 0
[consumi_idrici_importo] => 0
[8] => 0000-00-00
[data2] => 0000-00-00
[9] =>
[allegato_consumi_idrici] =>
[10] => 0
[lavori_straordinari_importo] => 0
[11] => 0000-00-00
[data3] => 0000-00-00
[12] => 3
[causale] => 3
[13] =>
[causale_altro] =>
[14] =>
[causale_mese] =>
[15] =>
[causale_mese_periodo_esteso] =>
)
)
Array
(
[0] => Array
(
[0] => 43
[id] => 43
[1] => 35
[id_condominio] => 35
[2] => 38
[id_condomino] => 38
[3] => 9
[importo_versato] => 9
[4] => 2013-12-05
[data] => 2013-12-05
[5] =>
[numero_ricevuta] =>
[6] =>
[allegato_ricevuta] =>
[7] => 0
[consumi_idrici_importo] => 0
[8] => 0000-00-00
[data2] => 0000-00-00
[9] =>
[allegato_consumi_idrici] =>
[10] => 0
[lavori_straordinari_importo] => 0
[11] => 0000-00-00
[data3] => 0000-00-00
[12] => 2
[causale] => 2
[13] =>
[causale_altro] =>
[14] =>
[causale_mese] =>
[15] =>
[causale_mese_periodo_esteso] =>
)
)
the function:
function getCondominoByIdEntrata($idEntrata){
$sql1 = "SELECT * FROM entrate WHERE id = '".$idEntrata."'";
$r1 = executeQuery($sql1);
$id_condomino = $r1[0]['id_condomino'];
$sql2 = "SELECT * FROM anagrafe WHERE id = '".$id_condomino."'";
$r2 = executeQuery($sql2);
return $r2;
}
The print_r( $nome_condomino):
Array
(
[0] => Array
(
[0] => 38
[id] => 38
[1] => 35
[id_condominio] => 35
[2] => Carlo Vincenzo
[nome_cognome] => Carlo Vincenzo
[3] =>
[codice_fiscale] =>
[4] =>
[dati_catastali_foglio] =>
[5] =>
[dati_catastali_particella] =>
[6] =>
[dati_catastali_subalterno] =>
[7] =>
[dati_catastali_piano] =>
[8] =>
[recapito] =>
[9] => 2
[civico] => 2
[10] =>
[citta] =>
[11] => TA
[provincia] => TA
[12] =>
[telefono] =>
[13] =>
[cellulare] =>
[14] =>
[fax] =>
[15] =>
[email] =>
[16] =>
[pec] =>
[17] => si
[occupante_uguale_titolare] => si
[18] =>
[occupante] =>
[19] =>
[occupante_rapporto] =>
[20] =>
[occupante_telefono] =>
[21] =>
[occupante_cellulare] =>
[22] =>
[occupante_email] =>
[23] =>
[occupante_pec] =>
[24] =>
[nome_utente] =>
)
)
Array
(
[0] => Array
(
[0] => 52
[id] => 52
[1] => 35
[id_condominio] => 35
[2] => Mario Giancarlo
[nome_cognome] => Mario Giancarlo
[3] =>
[codice_fiscale] =>
[4] =>
[dati_catastali_foglio] =>
[5] =>
[dati_catastali_particella] =>
[6] =>
[dati_catastali_subalterno] =>
[7] =>
[dati_catastali_piano] =>
[8] => Via Rossi
[recapito] => Via Rossi
[9] => 5
[civico] => 5
[10] =>
[citta] =>
[11] =>
[provincia] =>
[12] =>
[telefono] =>
[13] =>
[cellulare] =>
[14] =>
[fax] =>
[15] =>
[email] =>
[16] =>
[pec] =>
[17] => si
[occupante_uguale_titolare] => si
[18] =>
[occupante] =>
[19] =>
[occupante_rapporto] =>
[20] =>
[occupante_telefono] =>
[21] =>
[occupante_cellulare] =>
[22] =>
[occupante_email] =>
[23] =>
[occupante_pec] =>
[24] =>
[nome_utente] =>
)
)
You want foreach statement to get all data from array $entrata & $nome_condominio. Something like this maybe work:
UPDATED:
<?php if($entrata != ""){
//echo '<pre>' . print_r( $entrata,true ) . '</pre>';
$i = 0;
foreach($entrata as $entr){
$nome_condomino = getCondominoByIdEntrata($entr['id']);
$saldo += (float) $entr['importo_versato'];
?>
<tr class="gradeX">
<td class="center"><?echo $giorno;?></td>
<td class="center"><?echo $nome_condomino['nome_cognome'];?></td>
<td class="center"><b><?echo "€ ".$entr['importo_versato'];?></b></td>
<td class="center"><?echo "€ ".$saldo;?></td>
<td class="center"><?echo $giorno_ok;?></td>
<td class="center"><?echo $entr['id'];?></td>
</tr>
<?php
$i++;
} ?>
<? } ?>
Seems like $entrata isn't an array or is empty? Try changing
if ($entrata != '') {
to
if (!empty($entrata) && is_array($entrata)) {
will ensure that you're getting a non empty array

Referencing an index in an array in SMARTY template

I have a variable that is an array in Smarty, and I am trying to figure out how to retrieve that information. The variable is called $COMMENTS and I did $COMMENTS|#print_r and this is what I got:
Array (
[0] => ModComments_CommentsModel Object (
[data:private] => Array (
[0] => 11686
[crmid] => 11686
[1] => 1679
[smcreatorid] => 1679
[2] => 1679
[smownerid] => 1679
[3] => 0
[modifiedby] => 0
[4] => ModComments
[setype] => ModComments
[5] =>
[description] =>
[6] => 2011-06-08 15:00:31
[createdtime] => 2011-06-08 15:00:31
[7] => 2011-06-08 15:00:31
[modifiedtime] => 2011-06-08 15:00:31
[8] => 2011-06-29 12:00:23
[viewedtime] => 2011-06-29 12:00:23
[9] =>
[status] =>
[10] => 0
[version] => 0
[11] => 1
[presence] => 1
[12] => 0
[deleted] => 0
[13] => 11686
[modcommentsid] => 11686
[14] => aasd
[commentcontent] => aasd
[15] => 6730
[related_to] => 6730
[16] =>
[parent_comments] =>
[17] => 11686
)
)
[1] => ModComments_CommentsModel Object (
[data:private] => Array (
[0] => 11685
[crmid] => 11685
[1] => 1679
[smcreatorid] => 1679
[2] => 1679
[smownerid] => 1679
[3] => 0
[modifiedby] => 0
[4] => ModComments
[setype] => ModComments
[5] =>
[description] =>
[6] => 2011-06-08 14:58:42
[createdtime] => 2011-06-08 14:58:42
[7] => 2011-06-08 14:58:42
[modifiedtime] => 2011-06-08 14:58:42
[8] =>
[viewedtime] =>
[9] =>
[status] =>
[10] => 0
[version] => 0
[11] => 1
[presence] => 1
[12] => 0
[deleted] => 0
[13] => 11685
[modcommentsid] => 11685
[14] => comment
[commentcontent] => comment
[15] => 6730
[related_to] => 6730
[16] =>
[parent_comments] =>
[17] => 11685
)
)
[2] => ModComments_CommentsModel Object (
[data:private] => Array (
[0] => 6731
[crmid] => 6731
[1] => 1679
[smcreatorid] => 1679
[2] => 1679
[smownerid] => 1679
[3] => 0
[modifiedby] => 0
[4] => ModComments
[setype] => ModComments
[5] =>
[description] =>
[6] => 2010-11-02 10:15:06
[createdtime] => 2010-11-02 10:15:06
[7] => 2010-11-02 10:15:06
[modifiedtime] => 2010-11-02 10:15:06
[8] =>
[viewedtime] =>
[9] =>
[status] =>
[10] => 0
[version] => 0
[11] => 1
[presence] => 1
[12] => 0
[deleted] => 0
[13] => 6731
[modcommentsid] => 6731
[14] => Test comment
[commentcontent] => Test comment
[15] => 6730
[related_to] => 6730
[16] =>
[parent_comments] =>
[17] => 6731
)
)
)
I am trying to retrieve the 11686 number from that. Any help? I have tried $COMMENTS[0][data:private][0] but that didn't work.
Any help greatly appreciated :)
First, put print_r() output into <pre> tags so it is readable.
Array (
[0] => ModComments_CommentsModel Object (
[data:private] => Array (
[0] => 11686
[crmid] => 11686
[1] => 1679
[smcreatorid] => 1679
[2] => 1679
...
)
)
...
)
You're trying to access the first object in your array, $COMMENTS[0]. Since that it is object, and your data property is private, you cannot access it in Smarty. You would have to edit the ModComments_CommentsModel class to give you access to either the data property or the crmid key of the property.
Example:
class ModComments_CommentsModel {
// ...
public function CrmId(){
return $this->data['crmid'];
}
// ...
}
{* template *}
{$COMMENTS[0]->CrmId()}
{* might have to assign before using method call *}
{assign var='comment' value=$COMMENTS[0]}
{$comment->CrmId()}

Categories