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
Related
$email_users[] = $row;
print_r($email_users);
Result:
Array (
[0] => Array (
[user_id] => 87436
[username] => Admin
[user_email] => email#online.us
[user_lang] => de
[allowed] => 1 )
[1] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
[2] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
)
As you can see, the user_id 68013 is double. I have to remove the double Array. The Result should look like this:
Array (
[0] => Array (
[user_id] => 87436
[username] => Admin
[user_email] => email#online.us
[user_lang] => de
[allowed] => 1 )
[1] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
)
I read and tried several solutions I found on stack. For example:
How to get unique value in multidimensional array
$email_users[] = $row;
$user_ids = array();
foreach ($email_users as $h) {
$user_ids[] = $h['user_id'];
}
$email_users = array_unique($user_ids);
print_r($email_users);
But the print_r is only:
Array ( [0] => 87436 [1] => 68013 )
Thanks for your time.
You should get distinct value from your query. Use SELECT DISTINCT * FROM tablename and you will get Distinct value.
Or use $email_users= array_unique($email_users, SORT_REGULAR);
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
I got this result when I printed the array print_r($post);
Array ( [0] => Array ( [0] => stdClass Object ( [subscriber_id] => 80010055 [cto_id] => [name] => n [mobile] => 1234564444 [state] => [city] => fsd [cto_subscriber_id] => 0 [password] => e10adc3949ba59abbe56e057f20f883e [email] => n#n.com [email_verified] => 1 [ip_address] => ::1 [last_login_time] => 2012-10-24 11:37:19 ) ) [1] => Array ( [0] => stdClass Object ( [subscriber_id] => 80010055 [ip_address] => [landline_number] => [sex] => 0 [dob] => 0000-00-00 00:00:00 [marital_status] => hfg [state] => [addres] => fgh [city] => fghfg [pincode] => fghfgh ) ) )
How can I get single values
Typecast it to array?
$arr = (array)$post[0][0];
And then you'll be able to loop through it as through normal array
foreach ($arr as $key => $value){....}
foreach $post as $sub {
foreach $sub as $single {
echo $single;
}
}
Think you should learn the basics of arrays and objects first.
Anyway in order to get the value of lets say subscriber_id you should use the following:
$post[0][0]->subscriber_id
First you get into the array and then get the value out of the Object
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']);
Array ( [kanye] => Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane [4] => Kaye ) [wst] => Array ( [0] => ST [1] => St [2] => st [3] => EST [4] => West ) )
Array
(
[0] => Kanya
[1] => Janaye
[2] => Kayne
[3] => Kane
[4] => Kaye
)
Array
(
[0] => ST
[1] => St
[2] => st
[3] => EST
[4] => West
)
I've got those two arrays inside one array. The top array holds them both, then below is each one individually. When I am displaying the individual arrays, how do I echo their name?
So the first would be kanye, then list the contents, etc.
Hope that makes sense. I know it will be a simple bit of code but it's stumping me.
You can use a foreach statement to get the key value pair of the array:
$outer_arr = array('kanye' => array('Kanya', 'Janaye', 'Kayne', 'Kane'));
foreach($outer_arr as $key => $val) {
print($key); // "kanye"
print_r($val); // Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane )
}
If you just need to get the keys, you can use array_keys
$myArray = array(
"Kanye" => array("Kane", ...)
"West" => array("Wst", ...)
);
print_r(array_keys($myArray));
/*
array (
0 => Kanye
1 => West
)
*/
How about just print_r on the outer array?