AJLX Posted September 27, 2016 Share Posted September 27, 2016 (edited) Hey guys, I have a php array that looks like this: Array ( [opportunity_items] => Array ( [0] => Array ( [id] => 2783 [opportunity_id] => 92 [item_id] => [item_type] => [opportunity_item_type] => 0 [opportunity_item_type_name] => Group [name] => Strobes [transaction_type] => [transaction_type_name] => [accessory_inclusion_type] => [accessory_inclusion_type_name] => [accessory_mode] => The array goes on for ages, but I have only shown the bits I need. I want to create a new array that just contains the [id] key. I can get the first value by doing this: echo $array["opportunity_items"]["0"]["id"]; But I somehow I need to loop through this to get all of the rest of the values. Thanks, Andy Edited September 27, 2016 by AJLX Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/ Share on other sites More sharing options...
requinix Posted September 27, 2016 Share Posted September 27, 2016 Try using array_column on $array["opportunity_items"]. Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537843 Share on other sites More sharing options...
AJLX Posted September 27, 2016 Author Share Posted September 27, 2016 Thanks for your reply. I'm still on PHP 5.4, so this hasn't been implimented yet, and upgrading isn't an option. Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537849 Share on other sites More sharing options...
requinix Posted September 27, 2016 Share Posted September 27, 2016 Then try writing the equivalent code. It uses a foreach loop and... well, that's it really. A single foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537850 Share on other sites More sharing options...
AJLX Posted September 27, 2016 Author Share Posted September 27, 2016 I finally got there! foreach ($obj as $key) { foreach ($key as $id) { print_r($id['id']); } } Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537852 Share on other sites More sharing options...
Barand Posted September 27, 2016 Share Posted September 27, 2016 If you worked for me and my requirement was I want to create a new array that just contains the [id] key. and you came up with that as a solution, then you wouldn't be getting paid. Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537853 Share on other sites More sharing options...
AJLX Posted September 28, 2016 Author Share Posted September 28, 2016 Out of interest what would your solution be? This is for my own personal use, you'll be pleased to know I don't do this as a day job. Any opportunity to learn new things is appreciated... Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537865 Share on other sites More sharing options...
Barand Posted September 28, 2016 Share Posted September 28, 2016 print_r() is a debugging tool for viewing an array, it does not produce an array. This will give you the desired array. $id_array = []; // initialize array foreach ($data['opportunity_items'] as $k => $item) { $id_array[$k] = $item['id']; } Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537866 Share on other sites More sharing options...
AJLX Posted September 28, 2016 Author Share Posted September 28, 2016 Ah yes! The code I used above with print_r was just to prove that the result was correct. The code I used to actually build the array looked like this: $assets = array(); foreach ($obj as $key) { foreach ($key as $id) { $assets[] = ($id['id']); } } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/302246-php-array-help/#findComment-1537869 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.