Retain same array key value rest other delete in php array - php

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]);

Related

Best Way to Update Multi-dimensional Array Value Using another Array with Key as the value being looked for and the value for what replaces it

I have a a Multidimensional array with a lot of content that includes a label for each piece of content, and I have a separate array with a Key that is the label being searched for and the value that is to replace it.
I want to use the second array to replace nested label values in the first array. The function should find the first Array's label value based on the key of the second array, and replace it using that Key's value.
For Example the content Array:
Array
(
[Name] => Array
(
[0] => Array
(
[label] => Name
[content] => Important Paper
)
[1] => Array
(
[label] => Item Type
[content] => Document
)
[2] => Array
(
[label] => Author
[content] => Bob Jones
)
)
[date] => Array
(
[0] => Array
(
[label] => Date
[content] => 2009
)
)
)
And an example of the array looking to replace the value
Array
(
[Name] => Record
[Author] => Researcher
[Date] => Year
)
The output I would want from this function would resemble
Array
(
[Name] => Array
(
[0] => Array
(
[label] => Record
[content] => Important Paper
)
[1] => Array
(
[label] => Item Type
[content] => Document
)
[2] => Array
(
[label] => Researcher
[content] => Bob Jones
)
)
[date] => Array
(
[0] => Array
(
[label] => Year
[content] => 2009
)
)
)
Right now I am attempting to get the results by using a series of forloops and if statements. I don't actually know the depth that the [label] key will be found at, and it should only update content with the key value [label].
Any insight on a way to do this more efficiently would be great. I've also been looking at all of the different php array functions and trying to find the best combinations.
you can use: array_walk_recursive
from:
php.net
$orig = array('Name' => [['label' => 'Name', 'content' => 'Important Paper'],
['label' => 'Item Type', 'content' => 'Document'],
['label' => 'Author', 'content' => 'Bob Jones']],
'date' => [[['label' => 'Date', 'content' => '2009']]]);
// notice the `&` symbol, this allows us to change the original array on the fly
function replaceIt(&$item){
$replaceArray = ['Name' => 'Record',
'Author' => 'Researcher',
'Date' => 'Year'];
foreach ($replaceArray as $key => $value) {
if($key == $item){ $item = $value; }
}
}
array_walk_recursive($orig, 'replaceIt');
print('<pre>');
print_r($orig);
print('</pre>');

Replacing an array key contents whilst preserving the full array - PHP

I want to replace the contents of array key [menu_order] with my new array part, whilst keeping the entire array together. Which way would be to replace that key?
Original complete array
variable - $full_array
Array
(
[menu_order] => Array
(
[0] => Array
(
[row_position] => 1
[row_identifier] => home
[original_row_position] => 1
[button_styling] => off
[tab] => self
)
[1] => Array
(
[row_position] => 2
[row_identifier] => benefits
[original_row_position] => 2
[button_styling] => off
[tab] => self
)
)
[policy] => Array
(
[override_privacy] => no
[privacy_override_textarea] =>
)
)
[menu_order] new data
variable - $menu_order_array_part
Array
(
[0] => Array
(
[row_position] => 1
[row_identifier] => home
[original_row_position] => 1
[button_styling] => off
)
[1] => Array
(
[row_position] => 2
[row_identifier] => benefits
[original_row_position] => 2
[button_styling] => off
)
)
I can't get the syntax correct for editing the original array whilst replacing a multidimensional array
Thanks,
D
You can use the key.
$full_array['menu_order'] = $menu_order_array_part;

How to chage array key value without change array formate in php

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

php reorder array based on order of other array

Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.

Remove the parent array key in PHP?

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']);

Categories