I have this array from a query.
Array
(
[0] => Array
(
[user_id] => 5
[first_name] => Diyaa
[profile_pic] => profile/user5.png
)
[1] => Array
(
[user_id] => 8
[first_name] => Raj
[profile_pic] => profile/user8.jpg
)
[2] => Array
(
[user_id] => 10
[first_name] => Vanathi
[profile_pic] => profile/user10.jpg
)
)
I need to set array index as like array value (user_id) as given below:
Array
(
[5] => Array
(
[user_id] => 5
[first_name] => Diyaa
[profile_pic] => profile/user5.png
)
[8] => Array
(
[user_id] => 8
[first_name] => Raj
[profile_pic] => profile/user8.jpg
)
[10] => Array
(
[user_id] => 10
[first_name] => Vanathi
[profile_pic] => profile/user10.jpg
)
)
Note: user_id is an unique value, it won't repeat again. No need to worry about index value.
How to convert and get that array as specified index value..?
This is exactly what array_column() is for:
$result = array_column($array, null, 'user_id');
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
column_key
The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).
You can try this code, here I do some extra work. Refer to AbraCadaver's clever answer $result = array_column($array, null, 'user_id');.
array_combine(array_column($array, 'user_id'), $array);
Both structures are unnecessarily complex and redundant. Why not
$foo = array(5 =>
array('first_name' => 'Diyaa',
'profile_pic' => 'profile/user5.png'),
8 =>
array('first_name' => 'Raj',
'profile_pic' => 'profile/user8.png'),
...
);
Then access it via $foo[$user_id], which will give you a 2-element associative array such as
array('first_name' => 'Raj',
'profile_pic' => 'profile/user8.png'),
For changing a profile_pic:
$foo[$user_id]['profile_pic'] = $new_pic;
Related
This question already has answers here:
Sorting a php array of arrays by custom order
(8 answers)
Closed 9 months ago.
Good day,
So I have the following array to start with that has items added to it. The order the items are added in the list have no specific sort to them to begin with other than the order they were originally added to the array in. For ease of reading, I put them in descending order to explain the result I am looking for. The values share a sub key of "tid" for this example.
Array
(
[0] => Array
(
[tid] => value_5
)
[1] => Array
(
[tid] => value_4
)
[2] => Array
(
[tid] => value_3
)
[3] => Array
(
[tid] => value_2
)
[4] => Array
(
[tid] => value_1
)
)
I now want to be able to feed a second array into a function, and have the array resorted so that the values supplied, which correlates to the "tid" of the field, are resorted to the front of the array in the order which they are given, and then leave the rest of the array in the order it currently is. For example, if I pass the following array as a sort key:
array("value_2", "value_3");
Then the value_2 and value_3 rows should become array key 0 and 1 respectively, but the rest of the array should stay in the same order like the following
Array
(
[0] => Array
(
[tid] => value_2
)
[1] => Array
(
[tid] => value_3
)
[2] => Array
(
[tid] => value_5
)
[3] => Array
(
[tid] => value_4
)
[4] => Array
(
[tid] => value_1
)
)
Alternatively, if a value that doesn't exist like value_9 is passed in the second array, we would just want to ignore that and skip past it.
I have explored a few options like a foreach and for loop and compared the values but its slow and doesn't really have recursion which seems like may be needed on this function.
As array sorting is a hot topic in PHP due to the myriad of ways to do so, I am looking to you guys for the best way to handle this problem. Thanks!
If I understood correctly your problem, here's a possible solution:
$firstArray = [
[ 'tid' => 'value_5' ],
[ 'tid' => 'value_4' ],
[ 'tid' => 'value_3' ],
[ 'tid' => 'value_2' ],
[ 'tid' => 'value_1' ],
];
$tidToAdd = [ 'value_2', 'value_3', 'value_9' ];
// The solution is below:
$intersect = array_intersect(array_column($firstArray, 'tid'), $tidToAdd);
$firstArrayFiltered = array_filter($firstArray, fn($elem) => !in_array($elem['tid'], $intersect));
$result = array_reduce($intersect, function($payload, $item) {
array_unshift($payload, ['tid' => $item]);
return $payload;
}, $firstArrayFiltered);
I'm trying very simply to use in_array() to check a key is in an array and then echo its value.
$array = Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
And I have value 556729685 which I want to check that this value exists or not? So I am using in_array() function for this.
in_array(556729685, array_keys($array));
in_array(556729685, array_values($array));
in_array(556729685, $array);
All above three i have used but result always showing NULL means blank.
I am really frustrated to find the solution. I don't understand what's happening.
You should use array_column() which will return the values from a single column in the input array as an array.
$product_auto_ids = array_column($array['cart_item'], 'product_auto_id');
In this case, it would return the following:
Array
(
[0] => 556729685
[1] => 951790801
)
Then you can use in_array() like you currently are.
in_array(556729685, $product_auto_ids);
I have a PHP array as follows:
print_r($myarray);
Array
(
[0] => Array
(
[JAN] => 484603732
[FEB] => 350203732
[MAR] => 133347732
[APR] => 203347732
[MEI] => 79797732
[JUNI] => 112047732
[JULI] => 380597732
[AGS] => 76597732
[SEP] => 86597732
[OKT] => 120397732
[NOV] => 391597732
[DES] => 58597732
)
)
I want to delete element like [JAN], [FEB], [MAR], ...
But I little bit confused about how to handle it with array_column
So far, I've tried:
$array = array_column($myarray, 'JAN','FEB','MAR','APR','MEI','JUNI','JULI','AGS','SEP','OKT','NOV','DES');
But, still facing error
array_column() expects at most 3 parameters, 13 given
My expected output:
Array
(
[0] => 484603732
[1] => 350203732
[2] => 133347732
[3] => 203347732
[4] => 79797732
[5] => 112047732
[6] => 380597732
[7] => 76597732
[8] => 86597732
[9] => 120397732
[10] => 391597732
[11] => 58597732
)
Any well thought to advise will be appreciated.
Thanks.
You don't need array_column() here. According to PHP documentation
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
You can simply use array_values() to remove all the keys.
$array = array_values($myarray[0])
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]);
I need to check if an exact pair of two values exists in a multidimensional array together.
I have an array like this:
Array
(
[code] => 200
[response] => Success
[0] => Array
(
[email] => example123#sample.com
[status] => Approved: Printed & Cleared
)
[1] => Array
(
[email] => xxexample123#sample.com
[status] => Pending
)
[2] => Array
(
[email] => example1345#sample.com
[status] => Approved
)
[3] => Array
(
[email] => example1235#sample.com
[status] => Approved: Printed & Cleared
)
)
Then I have an array that looks like this:
Array
(
[email] => xxexample123#sample.com
[status] => Pending
)
I need to check if that exact pair exists in the multidimensional array. Not just that the status and email appear seperate from each other.
You can use array_search() in the same manner if you need to get the key, but at its simplest (assuming $array1 only has the keys and values that are being search for and in the same order):
$array2 = array('email' => 'example123#sample.com',
'status' => 'Pending');
if(in_array($array2, $array1)) {
//yes
} else {
//no
}
See the Demo showing found and not found.