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']);
Related
I have below PHP array
Array
(
[attendees] => Array
(
[322] => Array
(
[0] => Array
(
[attendee_name] => Amy Rainwater
[attendee_email] => arainwater#azdps.gov
[attendee_phone] => 345676575
)
[1] => Array
(
[attendee_name] => Lisa Hernandez
[attendee_email] => lhernandez#azdps.gov
[attendee_phone] => 34565
)
)
)
[registration] => Array
(
[user_name] => Amy Rainwater
[user_email] => arainwater#azdps.gov
[dbem_name] => Lisa Hernandez
[dbem_email] => lhernandez#azdps.gov
[dbem_address] => PO Box 6638
[dbem_phone] => 343545546
[dbem_city] => Ph
[dbem_state] => Arz
[dbem_zip] => 85334
[dbem_company] => Arizona Department of Public Safety
)
[gateway] => authorize_aim
)
I want to retain [attendees] [322] [0] this key value rest other [1] remove from the attendees array. Currently you can see there is three arrays. So in first array (attendees) we have value "amy rainwater" so I want to retain this key value and other key value i.e [0]->Lisa Hernandez this sholud be removed from there. So please confrim how can I filter this array.
Thanks
You can use reset:
reset($res_booking_meta['attendees'][322]);
Can you please explain how to change one array value to second value.
Please find below example.
First array
Array
(
[20239802] => one test
[20239801] => two testttttt
)
Second array
Array (
[content] => Array (
[0] => Array (
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239802
[track] => Lede Inthati Santhosham
[duration] => 0
)
[1] => Array (
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239801
[track] => Lede Inthati Santhosham
[duration] => 0
)
)
[autoshuffle_pack] => no
)
We need to replace [track] value in second array if match first array [20239802] with second array [content_id]
Need out put:-
Array
(
[content] => Array
(
[0] => Array
(
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239802
[track] => one test
[duration] => 0
)
[1] => Array
(
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239801
[track] => two testttttt
[duration] => 0
)
)
[autoshuffle_pack] => no
)
Check [track] value change in my need out put
as per depend first array.
Array
(
[20239802] => one test
[20239801] => two testttttt
)
with second array
[content_id] => 20239801
[track] => two testttttt
Is this you want to do :
foreach($second_array as $key => $second_row) {
$content_id = $second_row['content_id'];
if(isset($first_array[$content_id]) && $first_array[$content_id] != '') {
$second_array['track'] = $first_array[$content_id];
}
}
You can do you own function.
Foreach records in your first array:
Loop though the second and search for key == content_id
If it matched, set the value of array2[$index][track] to array1[key]
If not just continue
You can also use array_search() function to find the match faster, for I'll let you take a look at the PHP documentation
I queried my database and it stores the results into an array. It looks like this:
Array
(
[0] => Array
(
[Submission_ID] => 111
[First_Name] => Dylan
[Last_Name] => Taylor
[Abstract_Title] => Research 1
)
[1] => Array
(
[Submission_ID] => 222
[First_Name] => Michael
[Last_Name] => Jones
[Abstract_Title] => Research 2
)
[2] => Array
(
[Submission_ID] => 333
[First_Name] => Wills
[Last_Name] => Adams
[Abstract_Title] => Research 3
)
)
This is all placed in a variable called $results. Currently, I'm displaying bits of data on my page like so:
echo $results[0][Abstract_Title]
It all works, however, it be convenient if the index keys were replaced with the Submission ID:
Array
(
[111] => Array
(
[Submission_ID] => 111
[First_Name] => Dylan
[Last_Name] => Taylor
[Abstract_Title] => Research 1
)
[222] => Array
(
[Submission_ID] => 222
[First_Name] => Michael
[Last_Name] => Jones
[Abstract_Title] => Research 2
)
[333] => Array
(
[Submission_ID] => 333
[First_Name] => Wills
[Last_Name] => Adams
[Abstract_Title] => Research 3
)
)
So I can do this instead (otherwise I would have to print the array just to look up the index key every time):
echo $results[111][Abstract_Title]
Any ideas? (I'm new to programming.) The closest answer I've found is: php replace array id keys but I can't get my head around the solution. I figured a forloop is the best option? Should the new array be placed in the same variable or new variable?
You can simply use array_combine and array_column like as
$final_result = array_combine(array_column($result,'Submission_ID'),$result);
print_r($final_result);
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
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