i have a code like this
$table=table_name;
$sql="SELECT * from $table where student_id='X' and
class='Y' and section='Z'";
$result=$conn->query($sql);
while($row=mysqli_fetch_array($result)
{
$column[]=$row;
}
//printing $columns array
print_r($column);
output
ARRAY ( [0] => ARRAY ( [0] => 1 [SNO] => 1 [1] => 21DBAAACAC0 [STUDENT_ID] => 21DBAAACAC0 [2]
=> WEEKEND [EXAM_NAME] => WEEKEND [3] => 8TH-STANDARD [CLASS] => 8TH-STANDARD [4] =>
A [SECTION] => A [5] => 0 [PERCENTAGE] => 0 [6] => 2021-03-29 [CONDUCTED_DATE]
=> 2021-03-29 [7] => [TELUGU] => [8] => 35 [ENGLISH] => 35 [9] => [MATHEMATICS] => [10] =>
[SOCIAL] => [11] => [HINDI] => [12] => [SCIENCE] => [13] => [NATURAL SCIENCE] => ) )
i need to store column names in to an array like this
column[0]=sno;
column[1]=student_id;
column[2]=exan_name;
column[3]=class; and so on..
ultimately i need column names(of dynamically created table) into an array
use following code to get keys of associative array
<?php
$new = call_user_func_array('array_merge', 'array name here');
for($i=0;$i<sizeof($new)-1;$i++)
{
echo $new[$i]."<br>";
}
?>
it will give output
sno(column 1 name)
student_name(column 2 name)
student_id(column 3 name)...up to size-1
well you can use:
while ($row = $result->fetch_assoc()) {
//code here
}
you'll have an associative array like this:
$row(
"column_key0" => "column_value0",
"column_key1" => "column_value"
//and so on
);
then you can access it with a foreach
foreach ($row as $column_key => $value) {
//code here ($column_key is the name of your column)
};
then you can do what you want like save it in another array checking for duplicates or simply use it for your needs.
This way also if you don't know the table structure you can use column names.
so:
$result_set = $result->fetch_assoc();
foreach ($result_set as $row) { // here you can replace with as $row_number => $row if needed
foreach ($row as $column_key => $column_value){
//code here ($column_key is the name of your column)
}
};
is the final result;
Related
I have two array, $result and $social_result. I have to merge both tables. social_icons_id of $result matches id of $social_result. If match then show link of $result array otherwise blank.
I have an array
Array
(
[0] => Array
(
[social_icons_id] => 14
[link] => www.instagram.com
[edittemplate_id] => 218
[name] => Email
[image] => email.png
)
[1] => Array
(
[social_icons_id] => 16
[link] => www.instagram.com
[edittemplate_id] => 218
[name] => Blogger
[image] => blogger.png
)
)
Another is:
Array
(
[0] => Array
(
[id] => 13
[name] => Address
[image] => address.png
)
[1] => Array
(
[id] => 14
[name] => Email
[image] => email.png
)
[2] => Array
(
[id] => 15
[name] => Fax
[image] => fax.png
)
[3] => Array
(
[id] => 16
[name] => Text
[image] => text.png
)
[4] => Array
(
[id] => 17
[name] => Website
[image] => Website.png
)
)
Now I have to merge both table in one table like:
Array
(
[0] =>
[1] => www.instagram.com
[2] =>
[3] =>
[4] =>
[5] => www.instagram.com
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
)
id of both tables matches and make one table.
I tried-
$result = $obj->select_social_ids($id); // for first table
$social_result = $obj->show_social_icons(); // for second table
for($j=0;$j<count($social_result);$j++)
{
if(in_array($social_result[$j]['id'], $result)) { // search value in the array
$link[] = $result[$j]['link'];
}
else
{
$link[] = '';
}
}
But not working.
Depending on where you're getting this information from (e.g. a database table), doing this operation in SQL may make more sense.
That said, given the data and code you've provided, I think your in_array() check is incorrect, as it will only check the top level of $result. The 'social_icon_id' value that you seem to want to compare to $social_results[$j]['id'] is contained in a nested array within $result.
You could do something like this:
<?php
$results = $obj->select_social_ids($id);
$results_ids = array_map(
function ($result) { return $result['id']; },
$results
);
$results = array_combine($results_ids, $results);
$social_results = $obj->show_social_icons();
foreach ($social_results as $social_result) {
$id = $social_result['id'];
if (isset($results[$id])) {
$link[] = $results[$id]['link'];
}
else
{
$link[] = '';
}
}
If I understand your question correctly, you want to loop thru $social_result and compare ID to those keys in $result, maybe something like this will work.
$link = array();
foreach($social_result as $social){
$key = array_search($social['id'], array_column($result, 'social_icons_id'));
if($key != ''){
$link[] = $result[$key]['link'];
}else{
$link[] = '';
}
}
I tested this code and it works to do what I beliueve you are trying to accomplish
$a = array('social_icons_id' => '14','link' => 'www.instagram14.com','edittemplate_id' => '218','name' => 'Email','image' => 'email.png');
$b = array('social_icons_id' => '16','link' => 'www.instagram16.com','edittemplate_id' => '218','name' => 'Blogger','image' => 'blogger.png');
$result = array($a,$b);
$social_result = array(array('id'=>'14','name'=>'address0','image'=>'adress.png'),array('id'=>'15','name'=>'address1','image'=>'adress.png'), array('id'=>'16','name'=>'address2','image'=>'adress.png'),array('id'=>'17','name'=>'address3','image'=>'adress.png'),array('id'=>'18','name'=>'address4','image'=>'adress.png'),array('id'=>'19','name'=>'address5','image'=>'adress.png'));
$link = array();
foreach($social_result as $social){
$key = array_search($social['id'], array_column($result, 'social_icons_id'));
echo "<p> k ".$key;
if($key != ''){
$link[] = $result[$key]['link'];
}else{
$link[] = '';
}
}
print_r($link);
Simply you can create a simple left join query to generate a flat array containing social image links.
select social_image.link
from social_icons
left join social_image
on social_icons.id = social_image.social_icons_id
order by social_icons.id
But be carefully with array size limitation on php, therefore that needs a proper result limitation.
select social_image.link
from social_icons
left join social_image
on social_icons.id = social_image.social_icons_id
order by social_icons.id
limit 1000
Hope this helps.
I dynamically pull the following array from mysql table and need to assign index to each of the element.
Array
(
[yma] => 65.0000
[mhip] => 65.0000
[tzp] => 40.0000
[mzp] => 45.0000
[su] => 40.0000
[tkp] => 74.0000
)
here is my expected output.
Array
(
[0][yma] => 65.0000
[1][mhip] => 65.0000
[2][tzp] => 40.0000
[3][mzp] => 45.0000
[4][su] => 40.0000
[5][tkp] => 74.0000
)
Or
Array
(
[0] => 65.0000
[1] => 65.0000
[2] => 40.0000
[3] => 45.0000
[4] => 40.0000
[5] => 74.0000
)
There can be issue with the answer below if my desired output is like below:
Array
(
[0] => 'yma'
[1] => 'mhip'
[2] => 'tzp'
[3] => 'mzp'
[4] => 'su'
[5] => 'tkp'
)
I am using this code for pulling the data:
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[$row['pawl']] = $row['contribution'];
}
How can I take the array I am fetching/building from MySQL and generate the desired output?
Use array_values. "Array_values() returns all the values from the array and indexes the array numerically."
Also,
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[] = $row['contribution'];
}
This will assign a numeric index starting from 0.
Now, if you need a specific numeric index to match each pawl code,
Then build a map:
$map = array
(
['yma'] => 1,
['mhip'] => 2,
['tzp'] => 3,
['mzp'] => 4,
['su'] => 5,
['tkp'] => 6,
);
And Remap the pawl codes.
$remappedArray = array();
foreach($myarray as $pawlCode => $value){
$newIndex = $map[$pawlCode];
$remappedArray[$newIndex] = $value;
}
How you want to handle missing/new codes is up to you.
In order to create the array you want (I will focus on example two because it is more reasonable) you just need one function - array_values.
Given an input array of
Array
(
[yma] => 65.0000
[mhip] => 65.0000
[tzp] => 40.0000
[mzp] => 45.0000
[su] => 40.0000
[tkp] => 74.0000
)
It will return a numerically indexed array of just the values, which will look like this:
Array
(
[0] => 65.0000
[1] => 65.0000
[2] => 40.0000
[3] => 45.0000
[4] => 40.0000
[5] => 74.0000
)
If you need the keys, rather than the values you can simply use the array_keys function on the same starting array (see input array above) - and you will get the following output:
Array
(
[0] => 'yma'
[1] => 'mhip'
[2] => 'tzp'
[3] => 'mzp'
[4] => 'su'
[5] => 'tkp'
)
PHP has a large suite of array functions and in general you can most likely find a function to help you get/do whatever you need with an array. I encourage you to take some time and scan through these at some point. It pays to know what is available to you.
Use this for your first expected array:
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[] = array($row['pawl'] => $row['contribution']);
}
And this for your second expected array:
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[] = $row['contribution'];
}
I have an array of dates that looks like this:
Array
(
[0] => '2014-01-01'
[1] => '2014-01-02'
[2] => '2014-01-03'
[3] => '2014-01-04'
[4] => '2014-01-05'
[5] => '2014-01-06'
[6] => '2014-01-07'
)
and I have another array of dates and counts that looks like this:
[All] => Array
(
[0] => Array
(
[count] => 2
[date] => 2014-01-06
)
[1] => Array
(
[count] => 1
[date] => 2014-01-03
)
[2] => Array
(
[count] => 43
[date] => 2013-12-11
)
[3] => Array
(
[count] => 103
[date] => 2013-12-10
)
[4] => Array
(
[count] => 128
[date] => 2013-12-09
)
[5] => Array
(
[count] => 75
[date] => 2013-12-08
)
[6] => Array
(
[count] => 107
[date] => 2013-12-07
)
I want to make a new associative array where all the keys are the dates from the first array above and all of the values are either the count matched up with the corresponding date or "0".
So for instance, the new array would look like this:
Array
(
[2014-01-01] => 0
[2014-01-02] => 0
[2014-01-03] => 1
[2014-01-04] => 0
[2014-01-05] => 0
[2014-01-06] => 2
[2014-01-07] => 0
)
Does that make sense? Please feel free to ask any questions you may have. Thank you!
Try this code:
$result = array();
foreach($firstArray as $f){
foreach($secondArray as $s){
if($s['date'] == $f) $result[$f] = $s['count'];
}
if(!array_key_exists($f, $result)) $result[$f] = 0;
}
$result = array();
foreach($secondArray as $s){
if(in_array($s['date'], $firstArray) {
unset($firstArray[$s['date']]);
$result[$s['date']] = $s['count'];
}
}
// if items left in first array that are not found within foreach:
if (!empty($firstArray))
$result = array_merge($result, array_fill_keys($firstArray, 0));
// sort by key so dates go ascending
ksort($result);
$new = array();
foreach($all as $row)
{
$new[$row['date']] = $row['count'];
}
array_merge ($new, $old);
Here $all is the array with the date and count indices.
$old is the exisisting array.
That's a 2-liner:
// use dates as index and set everything to 0
$result = array_fill_keys($x, 0));
// copy over existing counts
array_walk($all, function($v) use (&$result) { if (array_key_exists($v['date'], $result)) { $result[$v['date']] = $v['count'];}});
I need to create a multidimensional array with a foreach loop and a while loop.
The first array contains this:
Array
(
[0] => 13-10-14
[1] => 13-10-15
[2] => 13-10-16
[3] => 13-10-17
[4] => 13-10-18
[5] => 13-10-19
)
I need to make it look like this:
Array
(
[0] => Array
(
[date] => 13-10-14
[id] => Array
(
[0] => 012643
[1] => 012667
[2] => 013362
[3] => 016169
[4] => 016839
[5] => 035288
[6] => 035369
[7] => 037664
[8] => 038979
[9] => 039014
[10] => 039036
[11] => 039505
)
)
)
The first array I do a foreach loop with the second I need to make while as it is a sql query.
Here is the code:
foreach ($rs as $results) {
$rowT = $db->query("SELECT id FROM users WHERE LIMIT 10");
while ($rsT = $db->fetch_assoc($rowT)) {
$results['id'][] = $rsT;
}
$l_array[] = $results;
}## Heading ##
print_r($l_array);
Is returning the error:
Fatal error: Can not use string offset to an array
If your $rs is this:
array([0] => 13-10-14
[1] => 13-10-15
[2] => 13-10-16
[3] => 13-10-17
[4] => 13-10-18
[5] => 13-10-19)
Your foreach() would set $results to: 13-10-15, 13-10-16, etc. I think that's your error, since those are strings, not arrays. How about this:
$final_array = array();
foreach ($rs as $results) {
$tmp_array = array('date'=>$results);
$rowT = $db->query("SELECT id FROM users WHERE LIMIT 10");
while ($rsT = $db->fetch_assoc($rowT)) {
$tmp_array['id'][] = $rsT;
}
$final_array[] = $tmp_array;
}## Heading ##
print_r($final_array);
I have the following array and am trying to loop through it with php, generate a list of the "Type" and count how many. The Type is the key, but there will be unique values such as Call, To-do, Meeting, Proposal, etc. My goal is to have the following out put:
Call 2
To-do 1
Meeting 3
Proposal 4
The above are not the values that will be output from the following array, but I wanted you to have an idea of what I am trying to accomplish. Please help!
Array (
[0] => Array (
[0] => Call
[Type] => Call
[1] => fxxxx#xxxxentllc.com
[EmailAddress] => xxxxr#xxxxentllc.com
[2] => 3xxxx00
[Phone] => 31xxxx00
[3] => 31xxxx871
[MobilePhone] => 31xxxx871
[4] => 102795
[CustomerID] => 102795
[5] => Nortxxxxal
[Company] => Noxxxxal
[6] => Frank
[FirstName] => Frank
[7] => Syxxxxer
[LastName] => Sxxxxter
[8] => 3
[Priority] => 3
[9] => invite to Haxxxxales for lunch
[Details] => invite to Hafxxxxales for lunch
[10] => 4503
[ActivityID] => 4503
[11] => 05/23/13
[DueDate] => 05/23/13
)
[1] => Array (
[0] => To-do
[Type] => To-do
[1] => fsxxxxer#summxxxxntllc.com
[EmailAddress] => fsxxxxer#summixxxxtllc.com
[2] => 315xxxx000
[Phone] => 3154xxxx0
[3] => 315xxxx1
[MobilePhone] => 315xxxx1
[4] => 102795
[CustomerID] => 102795
[5] => Norxxxxl
[Company] => Norxxxxcal
[6] => Frxxxxk
[FirstName] => Fxxxxk
[7] => Sxxxxr
[LastName] => Syxxxxer
[8] => 3
[Priority] => 3
[9] => find out who contact is for xxxxdical center
[Details] => find out who contact is foxxxxcal center
[10] => 4504
[ActivityID] => 4504
[11] => 05/23/13
[DueDate] => 05/23/13
)
)
This should do it:
$type_counts = array_count_values(array_map(function($x) {return $x['Type'];}, $array));
The array_map will return an array containing all the Type elements, then array_count_values will count the number of each of these and return this as an associative array.
something along the lines of:
foreach ($array as $key => $value) {
if (isset($result[$key])) {
$result[$key]++;
} else {
$result[$key] = 1;
}
}
if it is a muldi-dimensional array, just put another for each in there but that should give you an idea
$types = array();
foreach($array as $item) {
isset(${$item['Type']}) ? ${$item['Type']}++ : ${$item['Type']} = 1;
if(!in_array($item['Type'], $types) {
$types[] = $item['Type'];
}
foreach($types as $type) {
echo "$type ${$type}\n";
}
I think you can loop in your array and count each occorrence
$Call = 0;
$To_do = 0;
$Meeting = 0;
$Proposal = 0;
foreach($array as $data)
{
if($data['Type'] == 'Call')
{
$Call++;
} else if($data['Type'] == 'To-do')
{
$To_do++;
} else if($data['Type'] == 'Meeting')
{
$Meeting++;
} else if($data['Type'] == 'Proposal')
{
$Proposal++;
}
}
In this way you will have stored in each variable $Call $To_do $Meeting $Proposal its relative count
you can use (array_count_values($array));
http://php.net/manual/en/function.array-count-values.php
This gives count of all the keys in an array.
If you want only for the specific keys then use foreach loop
The one Petros mentioned is the best way to do it.
I would do something along the lines of:
$your_array = /* Your Array */
$types_count = array();
foreach ($your_array as $type_array){
$types_count[$type_array['Type']]++;
}