I need to print out a table, starting from a PHP array, adding some columns for further utilization.
The array is this:
Array ( [0] => Array ( [tid] => 1 [token] => andrea [participant_info] => Array ( [firstname] => Andrea [lastname] => AndreaLastName [email] => andrea#email.com ) ) [1] => Array ( [tid] => 3 [token] => 1 [participant_info] => Array ( [firstname] => 1FirstName [lastname] => 1LastName [email] => 1#email.com ) ) [2] => Array ( [tid] => 4 [token] => 2 [participant_info] => Array ( [firstname] => 2FirstName [lastname] => 2LastName [email] => 2#email.com ) ) [3] => Array ( [tid] => 5 [token] => 3 [participant_info] => Array ( [firstname] => 3FirstName [lastname] => 3LastName [email] => 3#email.com ) ) [4] => Array ( [tid] => 6 [token] => 4 [participant_info] => Array ( [firstname] => 4FirstName [lastname] => 4LastName [email] => 4#email.com ) ) [5] => Array ( [tid] => 7 [token] => 5 [participant_info] => Array ( [firstname] => 5FirstName [lastname] => 5LastName [email] => 5#email.com ) ) [6] => Array ( [tid] => 8 [token] => 6 [participant_info] => Array ( [firstname] => 6FirstName [lastname] => 6LastName [email] => 6#email.com ) ) [7] => Array ( [tid] => 9 [token] => 7 [participant_info] => Array ( [firstname] => 7FirstName [lastname] => 7LastName [email] => 7#email.com ) ) [8] => Array ( [tid] => 10 [token] => test [participant_info] => Array ( [firstname] => testFirstName [lastname] => testLastName [email] => test#email.com ) ) [9] => Array ( [tid] => 11 [token] => test3 [participant_info] => Array ( [firstname] => firstnameTest [lastname] => lastnameTest [email] => test2#email.com ) ) )
And it's look like that using some online tool:
What I need is to create an HTML table ( using classic < table> or < div> doesn't care )
That looks like this:
Where the token is not directly printed but it's available as $variable to use for some script ( I need to add icon to download a file called "$token.pdf" )
Thanks for any suggestion.
I found some function that prints the array directly as it appear but I do not know how to adapt to my needs:
function build_table($array){
// start table
$html = '<table border="1" style="width:100%">';
// header row
$html .= '<tr>';
foreach($array[0] as $key=>$value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
I remade the array to give a good example.
As you see i use 2 foreach loops.
The first one is to loop trough the main array '$array'.
The second foreach, in the first foreach, loops trough the array from 'participant_info'.
The $i stands for the value from the array.
So if you type: $i['did'], it loops trough all the values from 'tid'.
I hope this explains it.
Good luck!
<?php
// Made array
$array[0] = array("tid" => 1, "token" => "andrea", "participant_info" => array("firstname" => "Andrea", "lastname" => "AndreaLastName", "email" => "andrea#email.com" ));
$array[1] = array("tid" => 3, "token" => 1, "participant_info" => array("firstname" => "1FirstName", "lastname" => "1LastName", "email" => "1#email.com" ));
$array[2] = array("tid" => 4, "token" => 2, "participant_info" => array("firstname" => "2FirstName", "lastname" => "2LastName", "email" => "2#email.com" ));
$array[3] = array("tid" => 5, "token" => 3, "participant_info" => array("firstname" => "3FirstName", "lastname" => "3LastName", "email" => "3#email.com" ));
$array[4] = array("tid" => 6, "token" => 4, "participant_info" => array("firstname" => "4FirstName", "lastname" => "4LastName", "email" => "4#email.com" ));
$array[5] = array("tid" => 7, "token" => 5, "participant_info" => array("firstname" => "5FirstName", "lastname" => "5LastName", "email" => "5#email.com" ));
$array[6] = array("tid" => 8, "token" => 6, "participant_info" => array("firstname" => "6FirstName", "lastname" => "6LastName", "email" => "6#email.com" ));
$array[7] = array("tid" => 9, "token" => 7, "participant_info" => array("firstname" => "7FirstName", "lastname" => "7LastName", "email" => "7#email.com" ));
$array[8] = array("tid" => 10, "token" => "test", "participant_info" => array("firstname" => "testFirstName", "lastname" => "testLastName", "email" => "test#email.com" ));
$array[9] = array("tid" => 11, "token" => "test3", "participant_info" => array("firstname" => "firstnameTest", "lastname" => "lastnameTest", "email" => "test2#email.com" ));
?>
<table>
<tr>
<th>tid</th>
<th>token</th>
<th>participant_info</th>
<th>firstname</th>
<th>lastname</th>
<th>email</th>
</tr>
<? foreach($array as $h => $i): ?>
<tr>
<td><?=$i['tid']?></td>
<td><?=$i['token']?></td>
<? foreach($i['participant_info'] as $p): ?>
<td><?=$p?></td>
<? endforeach; ?>
</tr>
<? endforeach; ?>
</table>
Related
I have an array($data) like this.
Array
(
[0] => Array
(
[PID] => 1
[USER_NAME] => JOHN
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 23233.80
[TOPUP_AMOUNT] => 58000.00
[TOTAL_EXPENSES] => 3114.41
)
.....
I need to get a new key called TOTAL_BALANCE and it should get as follows,
TOTAL_BALANCE=JOINED_VALUE+TOPUP_AMOUNT+TOTAL_EXPENSES
Final output should look as follows,
Array
(
[0] => Array
(
[PID] => 1
[USER_NAME] => JOHN
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 23233.80
[TOPUP_AMOUNT] => 58000.00
[TOTAL_EXPENSES] => 3114.41
[TOTAL_BALANCE] => 78119.39
)
.....
Can someone help me to achieve this?
Here is my try, but I am not sure this is correct or not.
$r = [];
$keys = array_keys($key1+$key2);
foreach($keys as $v){
??
}
Do it like this:
$data = Array(
0 => Array(
'PID' => 1,
'USER_NAME' => 'JOHN',
'JOINED_DATE' => '2022-01-31',
'JOINED_VALUE' => 23233.80,
'TOPUP_AMOUNT' => 58000.00,
'TOTAL_EXPENSES' => 3114.41,
),
1 => Array(
'PID' => 2,
'USER_NAME' => 'JOHN_2',
'JOINED_DATE' => '2022-01-31',
'JOINED_VALUE' => 1234.80,
'TOPUP_AMOUNT' => 1000.00,
'TOTAL_EXPENSES' => 3114.41,
)
);
foreach($data as &$value){
// Sum and store
$value['TOTAL_BALANCE'] = $value['JOINED_VALUE'] + $value['TOPUP_AMOUNT'] + $value['TOTAL_EXPENSES'];
}
print_r($data);
Output:
Array
(
[0] => Array
(
[PID] => 1
[USER_NAME] => JOHN
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 23233.8
[TOPUP_AMOUNT] => 58000
[TOTAL_EXPENSES] => 3114.41
[TOTAL_BALANCE] => 84348.21
)
[1] => Array
(
[PID] => 2
[USER_NAME] => JOHN_2
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 1234.8
[TOPUP_AMOUNT] => 1000
[TOTAL_EXPENSES] => 3114.41
[TOTAL_BALANCE] => 5349.21
)
)
This question already has answers here:
How to find array / dictionary value using key?
(2 answers)
Closed 3 years ago.
I need to form the final array, its not displaying how i am expecting, here is the code i used.
I need to print the final array in the expected format, I have attached the output how i am looking for in the below section,
<?php
$order_id = 12345;
$array1 = array(
"firstName" => "Test Fname",
"lastName" => "Test Lname",
"address1" => "Test Address",
"city" => "Test City",
"country" => "IND",
"email" => "test#blank.in"
);
$array2 = array(
"firstName" => "Test Fname",
"lastName" => "Test Lname",
"address1" => "Test Address",
"city" => "Test City",
"country" => "IND",
"email" => "test#blank.in"
);
$result = array("user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100");
$itemDetails = array();
foreach($result as $res){
$itemDetails[] =
array(
"User" => array(
"uservalue" => $res['user_id'],
"imageId" => $res['item_id']
),
"price" => $res['price'],
"qty" => $res['qty']
);
}
$final_array = array(
"id" => $order_id,
"billing" => $array1,
"shipping" => $array2,
"item" => $itemDetails
);
echo '<pre>';
print_r($final_array);die;
?>
Output of current Code
Array
(
[id] => 12345
[billing] => Array
(
[firstName] => Test Fname
[lastName] => Test Lname
[address1] => Test Address
[city] => Test City
[country] => IND
[email] => test#blank.in
)
[shipping] => Array
(
[firstName] => Test Fname
[lastName] => Test Lname
[address1] => Test Address
[city] => Test City
[country] => IND
[email] => test#blank.in
)
[item] => Array
(
[0] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
[1] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
[2] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
)
)
Expected Result should be like below
array
(
"id" => 12345
"billing" => array
(
[firstName] => Test Fname,
[lastName] => Test Lname,
[address1] => Test Address,
[city] => Test City,
[country] => IND,
[email] => test#blank.in
)
"shipping" => array
(
[firstName] => Test Fname,
[lastName] => Test Lname,
[address1] => Test Address,
[city] => Test City,
[country] => IND,
[email] => test#blank.in
)
"item" => array
(
array
(
"User" => Array
(
"uservalue" => 9999,
"imageId" => cloth
)
[price] => 500,
[qty] => 100
)
)
)
I am expecting output like above, can we display the final array in that format, can anyone look into it and update me your thoughts. Thanks!!
if you allow only single item in itemdetails
$itemDetails[] =
[
"User" => [
"uservalue" => $result['user_id'],
"imageId" => $result['item_id'],
],
"price" => $result['price'],
"qty" => $result['qty'],
];
Single item demo
if itemdetails allow multiple items
$result = [
["user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100"],
["user_id" =>"9999","item_id" =>"cloth","price" =>"400","qty" =>"200"]
];
$itemDetails = array();
foreach($result as $res){
$itemDetails[] =
array(
"User" => array(
"uservalue" => $res['user_id'],
"imageId" => $res['item_id']
),
"price" => $res['price'],
"qty" => $res['qty']
);
}
Multiple items demo
If $result has multiple values, this should work.
$result = array(array("user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100"),array("user_id" =>"500","item_id" =>"cloth123","price" =>"511","qty" =>"80"));
$itemDetails = array();
foreach($result as $res){
$itemDetails[] =
array(
"User" => array(
"uservalue" => $res['user_id'],
"imageId" => $res['item_id']
),
"price" => $res['price'],
"qty" => $res['qty']
);
}
$final_array = array(
// "id" => $order_id,
// "billing" => $array1,
// "shipping" => $array2,
"item" => $itemDetails
);
echo '<pre>';
print_r($final_array);
Output
Array
(
[item] => Array
(
[0] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
[1] => Array
(
[User] => Array
(
[uservalue] => 500
[imageId] => cloth123
)
[price] => 511
[qty] => 80
)
)
)
I have an associative array like this, i want to generate a table with these data, like breakfast, snacks, lunch, supper, dinner in row the following is the code I've tried, but I am stuck where to break the table row because when the array contains more than one item.i wants to Print associative array to table
*--------------------------------------------------*
| **Breakfast Snacks Lunch Supper Dinner** |
| test test test testfrom
|testfrom
*--------------------------------------------------*
array output is as follow
Array
(
[meal_plan_id] => 17
[calorie_limit] => 1
[total_calorie] => 0
[date] => 2017-12-29
[meal_plan] => Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 107
[label] => test
[quantity] => 10
[unit] => g
[status] => bf
)
[1] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 108
[label] => test
[quantity] => 121
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => su
)
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => sn
)
[1] => Array
(
[id] => 116
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 113
[label] => test500
[quantity] => 20
[unit] => oz
[status] => dn
)
)
)
)
)
below is the code I've tried
// $daily_meals = show_daily_meals() // contains the array
if(!empty($daily_meals['meal_plan'])){
echo '<tr>';
foreach($daily_meals['meal_plan'] as $meal_plan){
foreach ($meal_plan['data'] as $data){
if(!empty($data['id']))
echo '<td class="'.$meal_type.'" data-meals-id="'.$data['id'].'"><span class="left">'.$data['label'].'</span> <span class="right">'.$data['quantity'].' <a class="delete_row"><i class="fa fa-trash" aria-hidden="true"></i></a></span><div class="row_loader"></div></td>';
else echo '<td></td>';
}
$i++;
if($i%5 == 0) echo '</tr>';
}
}
Here is your solution
Input
<?php
$array = array(
array(
'meal_type' => 'bf',
'label' => 'Breakfast',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array(
'id' => 107,
'label' => 'test',
'quantity' => 10,
'unit' => 'g',
'status' => 'bf'
),
array(
'id' => 109,
'label' => 'testfrom',
'quantity' => 12,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'sn',
'label' => 'Snacks',
'calorie_limit' => 10,
'total_calorie' => 0,
'data' => array(
array(
'id' => 108,
'label' => 'test',
'quantity' => 121,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'lu',
'label' => 'Lunch',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(array('status' => 'su'))
),
array(
'meal_type' => 'su',
'label' => 'Supper',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array('status' => 'sn'),
array(
'id' => 116,
'label' => 'test',
'quantity' => 200,
'unit' => 'oz'
),
)
),
array(
'meal_type' => 'dn',
'label' => 'Dinner',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(
array(
'id' => 113,
'label' => 'test500',
'quantity' => 20,
'unit' => 'oz',
'status' => 'dn'
)
)
)
);
Solution
1st foreach for parent array. Then second for its child..
foreach($array as $r){
$$r['label'] = '<table width="100%">';
foreach($r['data'] as $s){
if(isset($s['label']))$$r['label'] .= '<tr><td align="center">'.$s['label'].'</td></tr>'; // The if condition check weather the label is exist or not if yes then add that in particular label's table
}
$$r['label'] .= '</table>';
}
echo '
<table width="100%" border="1">
<thead>
<tr>
<th>Breakfast</th>
<th>Snacks</th>
<th>Lunch</th>
<th>Supper</th>
<th>Dinner</th>
</tr>
</thead>
Here all labels canverted into variable by $$r['label']. And all have there own table Now we add all these tables into the master table to get the output.
<tbody>
<tr>
<td>'.$Breakfast.'</td>
<td>'.$Snacks.'</td>
<td>'.$Lunch.'</td>
<td>'.$Supper.'</td>
<td>'.$Dinner.'</td>
</tr>
</tbody>
</table>';
//?// echo "<pre>";print_r($array);
?>
Output
Try storing all your column names in an array and create a table with it:
foreach($array as $row) {
$column[$i]['label'] = $row['label'];
$column[$i]['data'] = $this->getdata($row['data']);
$i++;
}
function getdata($array) {
$data = '';
foreach($array as $row) {
$data. = $row['label'].',';
}
return $data;
}
Create a table with the column names and try inserting data using a loop with respect to insert into
I want to merge two array's using key(product_id) and adding that values(usage).
Array 1
Array
(
[0] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
[1] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 1
[product_id] => 14
)
)
Array 2
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 2
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
)
I need an out put like this merging and adding usage values.
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 2
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 3
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
[5] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
)
This is my code for creating arrays
foreach($query->rows as $product){
$top_products[]=array(
'name'=>$product['name'],
'usage'=>$product['pusage'],
'product_id'=>$product['product_id']
);
}
foreach($query_2->rows as $product){
$top_point_products[]=array(
'name'=>$product['name'],
'usage'=>$product['p_usage'],
'product_id'=>$product['product_id']
);
}
$first =array(
array(
"name" => "Reschedule A Service",
"usage" => 1,
"product_id" => 8
),
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Each Calendar Event",
"usage" => 1,
"product_id" => 14
)
);
$second =array(
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Schedule A Service",
"usage" => 3,
"product_id" => 11
),
array(
"name" => "Each Calendar Event",
"usage" => 2,
"product_id" => 14
),
array(
"name" => "Sales Performance Dashboard",
"usage" => 2,
"product_id" => 30
),
array(
"name" => "Quote",
"usage" => 1,
"product_id" => 32
)
);
$result = array_unique(array_merge($first,$second), SORT_REGULAR);
Use array_unique & array_merge
Use the array_merge function, like this:
$C = array_merge($A, $B);
print_r($C);
Read manual Array merge
try this code
<?php
$array1=array
(
0 => array(
'name' => "Reschedule A Service",
'usage' => 1,
'product_id' => 8
),
1 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 2,
'product_id' => 14
)
);
$array2=array
(
0 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
1 => Array
(
'name' => "Schedule A Service",
'usage' => 3,
'product_id' => 11
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 5,
'product_id' => 14
),
3 => Array
(
'name' => "Sales Performance Dashboard",
'usage' => 2,
'product_id' => 30
),
4 => Array
(
'name' => "Quote",
'usage' => 1,
'product_id' => 32
)
);
$product_id1=array_column($array1, 'product_id');
$product_id2=array_column($array2, 'product_id');
$new=array_intersect($product_id1,$product_id2);
foreach ($new as $key => $value) {
if(in_array($new[$key],$product_id2)){
$array2[array_search($new[$key],$product_id2)]['usage']+=$array1[$key]['usage'];
}
}
$new1=array_diff($product_id1,$product_id2);
foreach ($new1 as $key => $value) {
$array2[]=$array1[$key];
}
foreach ($array2 as $key => $value) {
echo "[".$key."]=><br>";
foreach ($value as $key1 => $value1) {
echo "      ";
echo "[".$key1."]=>".$value1."<br>";
}
echo "<br>";
}
?>
output
[0]=>
[name]=>Adding An Image
[usage]=>2
[product_id]=>5
[1]=>
[name]=>Schedule A Service
[usage]=>3
[product_id]=>11
[2]=>
[name]=>Each Calendar Event
[usage]=>7
[product_id]=>14
[3]=>
[name]=>Sales Performance Dashboard
[usage]=>2
[product_id]=>30
[4]=>
[name]=>Quote
[usage]=>1
[product_id]=>32
[5]=>
[name]=>Reschedule A Service
[usage]=>1
[product_id]=>8
Use array_merge and a simple foreach loop to check your condition and update the usagevalues.
See below
$result = array_merge($arrArray1, $arrArray2);
$result2 = array();
foreach($result as $key => $value){
if(array_key_exists($value['product_id'], $result2)){
$result2[$value['product_id']]['usage'] += $value['usage'];
} else{
$result2[$value['product_id']] = $value;
}
}
print_r($result2);
If you want to reset your resultant array indexes use array_merge again like this
$result2 = array_merge($result2);
Hope this will help
I have an php array format:
[0] => Array
(
[post_id] => 37
[post_title] => الأَبْجَدِيَّة العَرَبِيَّة
[post_image] =>
[post_status] => 1
)
[1] => Array
(
[post_id] => 36
[post_title] => TEST open for text
[post_image] => post_1463052793.jpeg
[post_status] => 1
)
[2] => Array
(
[post_id] => 35
[post_title] => Hey Sushovan
[post_image] => post_1463038438.jpg
[post_status] => 1
)
Now, I want to append an extra index with value. For this, I am using this code:
$all_data = $this->master_model->fetch_all_data_order_by($entity, $selectString, $entity.'_publish_date', 'DESC', $limit, $offset = $page);
$data['all_data']=$all_data;
foreach($all_data as $ad => $row)
{
$fetch = '*';
$table = 'chat';
$cond = $table."_to = 'A' AND post_id = '".$row['post_id']."' AND chat_view_status = 0";
$count = $this->master_model->count_data_by_condition($fetch,$table,$cond);
$pushArr = array('chat_count' => $count);
array_push($row,$pushArr);
}
However, the I can't push the data into the original $all_data.
How can I achieve this?
[0] => Array
(
[post_id] => 37
[post_title] => الأَبْجَدِيَّة العَرَبِيَّة
[post_image] =>
[post_status] => 1
[chant_count] => 2
)
The chat count is retrieved by calling count_data_by_condition() method.
You just need to push the chat_count to correct array. I think your "$all_data" variable is now a part of $data array with key 'data'.
Sample code:
$all_data = $this->master_model->fetch_all_data_order_by($entity, $selectString, $entity.'_publish_date', 'DESC', $limit, $offset = $page);
$data['all_data']=$all_data;
foreach($data['all_data'] as $ad => $row)
{
$fetch = '*';
$table = 'chat';
$cond = $table."_to = 'A' AND post_id = '".$row['post_id']."' AND chat_view_status = 0";
$count = $this->master_model->count_data_by_condition($fetch,$table,$cond);
$data['all_data'][$ad]['chat_count'] = $count;
}
Hope it helps !
Try below code:
$array = array(array("title" => "test", "desc" => "test2"), array("title" => "aaa", "desc" => "bbb"));
echo "before==>";
print_r($array);
foreach ($array as $key => $value) {
$array[$key]["chat_count"] = "123456";
}
echo "<br/>after==>";
print_r($array);
Your array
$arr = array(
0=> array('post_id'=> 7,'post_title'=> 'Title 7'),
1=> array('post_id'=> 8,'post_title'=> 'Title 8'),
);
echo '<pre>';
print_r($arr);
Code snippet
$i=0;
foreach($arr as $eacharr):
$eacharr['chant_count'] = 'Your chat count';
$arr[$i] = $eacharr;
$i++;
endforeach;
echo '<pre>';
print_r($arr);
Output before loop
Array
(
[0] => Array
(
[post_id] => 7
[post_title] => Title 7
)
[1] => Array
(
[post_id] => 8
[post_title] => Title 8
)
)
Output after loop
Array
(
[0] => Array
(
[post_id] => 7
[post_title] => Title 7
[chant_count] => Your chat count
)
[1] => Array
(
[post_id] => 8
[post_title] => Title 8
[chant_count] => Your chat count
)
)
try this example
<?php
$ss = array("0" => Array
(
"post_id" => '37',
"post_title" =>'ss',
"post_image" =>'dsd' ,
"post_status" => '1'
),
"1" => Array
(
"post_id" => '36',
"post_title" => 'TEST open for text',
"post_image" => 'post_1463052793.jpeg',
"post_status" => '1'
),
"2" => Array
(
"post_id" => '35',
"post_title" => 'Hey Sushovan',
"post_image" => 'post_1463038438.jpg',
"post_status" => '1'
)
);
print_r($ss);
$i=1;
foreach($ss as $key=>$row)
{
$ss[$key]['mm']=$i;
$i++;
}
echo "<pre>";
print_r($ss);
?>
OUTPUT
Array
(
[0] => Array
(
[post_id] => 37
[post_title] => ss
[post_image] => dsd
[post_status] => 1
[mm] => 1
)
[1] => Array
(
[post_id] => 36
[post_title] => TEST open for text
[post_image] => post_1463052793.jpeg
[post_status] => 1
[mm] => 2
)
[2] => Array
(
[post_id] => 35
[post_title] => Hey Sushovan
[post_image] => post_1463038438.jpg
[post_status] => 1
[mm] => 3
)
)