This is mymysql table
id name ssn phone email**
1 Asok 5466 9865893265 asok#gmail.com
2 Sokan 7856 9562358965 sakan#gmail.com
......
.....
when I am using select query, i will get result as:
Array ( [0] => Array ( [id] => 1 [name] => Asok [sin] => 5466 [phone] => 9865893265 [email] => asok#gmail.com ) [1] => Array ( [id] => 2 [name] => Sokan [sin] => 7856 [phone] => 9562358965 [email] => sakan#gmail.com ) ...)`
I need to get this result as
Array ( [5466] => Array ( [id] => 1 [name] => Asok [sin] => 5466 [phone] => 9865893265 [email] => asok#gmail.com )
[7856] => Array ( [id] => 2 [name] => Sokan [sin] => 7856 [phone] => 9562358965 [email] => sakan#gmail.com ) ...)
using sql query
Here the index 5466 and 7856 are the field 'ssn' (this is a unique no to that person)
Do you want like this?
SQL column name ssn, result array index sin. I wrote as sin
$newArray = array();
foreach ($results as $row)
{
$newArray[$row['sin']] = $row;
}
Look https://github.com/EllisLab/CodeIgniter/pull/429
this link same is being discussed
functions map_array($key_field, $value_field) and map($key_field, $value_field)
that return a map (dictionary) from the result set
Try mySQL Index Hint Syntax
index hint syntax
Related
Here is my Sample table:
Here is My Query:
SELECT * FROM table
Result for above query is:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[country] => x
)
[1] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
I need to get the field names in the first element of the array.
Expected result array is:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
How can I modify the Query to get this result?
Thanks in advance...
Display issues should generally be dealt with in application code, not SQL queries. If you have your values in an array (called $results, say), you can use this code to add the desired entry:
array_unshift($results, array_combine(array_keys($results[0]), array_keys($results[0])));
print_r($results);
Output:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
Demo on 3v4l.org
SELECT 'id' id, 'name' name, 'country' country
UNION ALL
SELECT id, name, country FROM table
ORDER BY 'id' != id
One way to do it with array_keys, array_combine and array_unshift. If I were you I will get the SQL result as $array variable and do some processing on php side like below on query side.
<?php
$array = [['id'=>1,'name'=>'a','country'=>'x'],['id'=>1,'name'=>'b','country'=>'y']];
$keys = array_keys($array[0]);
$first = array_combine($keys,$keys);
array_unshift($array,$first);
print_r($array);
?>
WORKING DEMO: https://3v4l.org/IqZVk
I need to create a db function for multidimensional array. How deep the array currently dont know, bcoz they will come from xml file.
I have a sample array
Array
(
[employee] => Array
(
[0] => Array
(
[name] => Array
(
[lastname] => Kelly
[firstname] => Grace
)
[hiredate] => October 15, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Printer
[id] => 111
[price] => $111.00
)
[1] => Array
(
[product] => Laptop
[id] => 222
[price] => $989.00
)
)
)
)
[1] => Array
(
[name] => Array
(
[lastname] => Grant
[firstname] => Cary
)
[hiredate] => October 20, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Desktop
[id] => 333
[price] => $2995.00
)
[1] => Array
(
[product] => Scanner
[id] => 444
[price] => $200.00
)
)
)
)
[2] => Array
(
[name] => Array
(
[lastname] => Gable
[firstname] => Clark
)
[hiredate] => October 25, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Keyboard
[id] => 555
[price] => $129.00
)
[1] => Array
(
[product] => Mouse
[id] => 666
[price] => $25.00
)
)
)
)
)
)
I need to enter these type of array to db and then retrieve them in a good non programmer readable format
I created 2 table... 1st for array key with array level field and another for key=value
I tried this
function array_Dump($array, $d=1){
if (is_array($array)){
foreach($array as $key=>$val){
for ($i=0;$i<$d;$i++){
$level=$i;
}
if (is_array($val)){
if (is_int($key)){
array_Dump($val, $d+1);
}else{
$query = "insert into xml_array (level, input) VALUES ('$level','$key')";
insert_sql($query);
array_Dump($val, $d+1);
}
} else {
$query = "insert into xml_data (array_id,level_id, array_key,array_value) VALUES ('$insert_id','$level','$key','$val')";
insert_sql($query);
}
}
}
}
Create a table like this:
attributes(id, parent_id, properties)
where id will be the primary key, parent_id will be the id of the parent record and properties will be a small json field with the atomic properties. This way you support any depth the XML may throw towards your direction.
As about non-programmer representation. For instance you could use a table (you can solve that with divs as well) which will contain a row for each element in the top level array. Such a row would contain separate columns for each property. When a property is an array, then the given cell will be a table of its own, which will be handled similarly as the first table. It is advisable to make the inner tables collapsible, so if one wants to see the main levels only, the user will not have to scroll for a long while.
I have an existing array, and I want to add some items in it from a mysql row
$extendedadmindetails = full_query("SELECT * FROM `tbladmins` WHERE `id`='{$_SESSION['adminid']}'");
$extendedadmindetailsrow = mysql_fetch_assoc ($extendedadmindetails);
array_push($apiresults, $extendedadmindetailsrow);
This returns an array in an array:
Array
(
[result] => success
[adminid] => 1
[name] => My Name
[notes] =>
[signature] =>
[allowedpermissions] => My Name
[departments] => 1
[requesttime] => 2017-02-26 12:44:06
[0] => Array
(
[id] => 1
[uuid] => sqdqsdqsdqsdq454
[roleid] => 1
[username] => Myname
[password] => $dfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf
[passwordhash] => $jghjghjghjghjghjghjghjghjg
[updated_at] => 0000-00-00 00:00:00
)
)
while I need:
Array
(
[result] => success
[adminid] => 1
[name] => My Name
[notes] =>
[signature] =>
[allowedpermissions] => My Name
[departments] => 1
[requesttime] => 2017-02-26 12:44:06
[id] => 1
[uuid] => sqdqsdqsdqsdq454
[roleid] => 1
[username] => Myname
[password] => $dfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf
[passwordhash] => $jghjghjghjghjghjghjghjghjg
[updated_at] => 0000-00-00 00:00:00
)
I believe I should use array_push to add to an existing array, but I'm not sure how to proceed from there. Do I need to loop trough the extendedadmindetailsrow array and add items 1 by 1?
Any one can help me out with this?
Thanks!!
use of array_merge is better in case
// Considering your mysql is returning only 1 row
foreach ($extendedadmindetailsrow as $key => $row) {
$arr = $row;
}
// after this if you will try array_push also that will work
$result = array_merge($apiresults, $arr);
print_r($result);
Take a look at array_merge
array_merge($apiresults, $extendedadmindetailsrow);
You can:
$result = $apiresults + $extendedadmindetailsrow;
Use array_merge()
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
Output will be
Array (
[0] => red
[1] => green
[2] => blue
[3] => yellow
)
I have a problem. I am trying to count the array keys in my array using an specific element inside my array in the main array
public function index()
{
$user=User::all();
$lead=Lead::all();
$role=Role::all();
$lead=DB::table('lead')
->select(DB::raw('lead.id','users.number','users.username', 'lead.leadid','lead.first_name', 'lead.last_name','lead.email' ,count('lead.email') ))
->where ('lead.email', '>', 1)
->groupBy('lead.email')
->leftJoin('users', 'users.number', '=', 'lead.assign')
->get();
$user=DB::table('users')->select('users.number','users.username')->get();
echo "<pre>";
print_r($lead);
die();
}
Here is the output when I print it out
Array
(
[0] => Array
(
[id] => 63
[number] => 3
[username] => shankar
[leadid] => zcrm_125720000016007771
[first_name] =>
[last_name] => Amoah Owusu Richmond
[email] => ramoahhowusu50#gmail.com
)
[1] => Array
(
[id] => 64
[number] => 3
[username] => shankar
[leadid] => zcrm_125720000016007733
[first_name] => Deus
[last_name] => mathew
[email] => mathewdeus#gmail.com
)
[2] => Array
(
[id] => 65
[number] => 2
[username] => james
[leadid] => zcrm_125720000016007737
[first_name] => bari
[last_name] => safi
[email] => barisafi57#gmail.com
)
[3] => Array
(
[id] => 66
[number] => 11
[username] => nishupandey
[leadid] => zcrm_125720000016007741
[first_name] => Noorahmad
[last_name] => Noor
[email] => noorahmad.noor81#gmail.com
)
[4] => Array
(
[id] => 67
[number] => 12
[username] => ravi123
[leadid] => zcrm_125720000016007747
[first_name] => munsanje
[last_name] => nakeempa
[email] => mnakeempa#yahoo.com
)
[5] => Array
(
[id] => 68
[number] => 8
[username] => veerkishor
[leadid] => zcrm_125720000016007751
[first_name] => Noorahmad
[last_name] => Noor
[email] => noorahmad.noor71#gmail.com
)
[6] => Array
(
[id] => 69
[number] => 13
[username] => rahul
[leadid] => zcrm_125720000016007755
[first_name] => painad
[last_name] => sherzad
[email] => painda12sherzad#gmail.com
)
)
I want to count the elements using the email and check how many times the element is appearing in the array. Am trying to use the array_count_values but am not quite sure how to use it. Thanks in advance
Try this:
In your DB::raw, you need to put "count" inside quotes. Otherwise, you will just run php count (like sizeof).
<?php
$lead=DB::table('lead')
->select(['lead.id','users.number','users.username', 'lead.leadid','lead.first_name', 'lead.last_name','lead.email'])
->select( DB::raw( "count('lead.email') as total") )
->where ('lead.email', '>', 1)
->groupBy('lead.email')
->leftJoin('users', 'users.number', '=', 'lead.assign')
->get();
If you want to count the number of arrays with specific property inside on big array then you need to loop over all the outer array elements and check the inner array elements.
To count how many elements have the same e-mail, I guess you have to do it manually...
$emails = array();
foreach ($lead as $lineNumber => $line) {
$email = $line['email'];
if(array_key_exists("$email",$emails) {
$emails["$email"]++;
} else {
$emails["$email"] = 1;
}
}
echo "<pre>";
print_r($emails);
Is that what you want ?
Simply Use Count ($array) which u want to count
hope it will work
I have an array structure like this :
Array (
[donate] => Array
(
[amount_other] => 222
[pay_method] => Array
(
[5] => Array
(
[first_name] => sam
[last_name] => indi
[cc_type] => mc
[cc_number] => 5123456789012346
[cc_ccv2] => 111
[cc_exp_month] => 10
[cc_exp_year] => 20
)
)
[notes] => Test comment.
)
)
I want to remove key [5] from the array, so that the new array becomes :
Array
(
[donate] => Array
(
[amount_other] => 222
[pay_method] => Array
(
[first_name] => sam
[last_name] => indi
[cc_type] => mc
[cc_number] => 5123456789012346
[cc_ccv2] => 111
[cc_exp_month] => 10
[cc_exp_year] => 20
)
[notes] => Test comment.
)
)
I want this because the array key changes and I want to access the inner array directly so that I don't have to change the key each time in the code. If there are other ways to achieve this.. Please help.
Thanks in advance.
$array['donate']['pay_method'] = current($array['donate']['pay_method']);
$array['donate']['pay_method'] = array_shift($array['donate']['pay_method']);