MasterACE14 Posted October 1, 2007 Share Posted October 1, 2007 Evening Everyone, I want to know how do I select just 1 field from a Multi-Dimensional Array. I know how to select all the fields, by using the following format: <?php foreach($fields as $key => $val){ echo $val; } But how can I do a similar thing, but to just select 1 specific field, not all of them? Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/ Share on other sites More sharing options...
hemlata Posted October 1, 2007 Share Posted October 1, 2007 Hello, If you know the 'key' for the multidimensional array then you can directly get the value as.. Eg. $array['key1']; Hope this might solve your issue. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359005 Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 <?php $a = array( 'foo' => array('a','b'), 'bar' => array('x','y') ); echo $a['foo'][1]; // echo 'b' ?> Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359006 Share on other sites More sharing options...
MasterACE14 Posted October 1, 2007 Author Share Posted October 1, 2007 I understand your example. But I think it needs to be alittle different in my situation, and I dont know how. I am making a function that I will be able to use, to 1) select an Array, and 2) select 1 field from that Array. Here is 1 of my arrays: <?php $weapons = array( array( id => 0, weapon => "L85A2", price => 250, damage => 15, type => "Rifle", rarity => "Basic", description => "The L85A2 is a bullpup assault rifle, it is very powerful and a iron-sight comes standard.", options => "You can give it a Laser." ) ); and here is what I've got so far for the function: <?php // Select 1 field from an Array function select_array($array,$field) { return $array[$field]; } in your example you had [1] after the ['foo'], in my case I dont think I need to use a numeric value. Am I able to use a string? something like this? <?php return $array[$field]['price']; or something like that? Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359013 Share on other sites More sharing options...
MasterACE14 Posted October 1, 2007 Author Share Posted October 1, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359035 Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Am I able to use a string? something like this? Of course. Why don't you try it and see? Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359060 Share on other sites More sharing options...
MasterACE14 Posted October 1, 2007 Author Share Posted October 1, 2007 I setup my function like this: <?php // Select 1 field from an Array function select_array($array,$field,$value) { return $array[$field][$value]; } and I tried this: <?php echo select_array($weapons,"price",250); ?> using the array I showed before. And it doesnt echo anything :-\ to me my function, logically doesnt seem to be correct. In your example you named your arrays, 'foo' and 'bar'. But in my Array their not named. It doesnt really seem possible for me to get past the $array part of my function, as my arrays arent named. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359662 Share on other sites More sharing options...
BlueSkyIS Posted October 1, 2007 Share Posted October 1, 2007 you passed the function the array $weapons, which contains 1 element which is an array. get hold of the internal array like this: $array1 = $weapons[0]; then you can use the keys of $array1: $id = $array1['id']; $weapon = $array1['weapon']; etc. added: or to fix your code: function select_array($array,$field) { return $array[0][$field][$value]; } however, if you're going to store an array of arrays, you'll want to know which array in the array: function select_array($elemnum, $array,$field) { return $array[$elemnum][$field]; } Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359694 Share on other sites More sharing options...
teng84 Posted October 2, 2007 Share Posted October 2, 2007 i think i get you! please show your sample array and sample output you want Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359712 Share on other sites More sharing options...
MasterACE14 Posted October 2, 2007 Author Share Posted October 2, 2007 ok, my sample array is this: <?php $weapons = array( array( id => 0, weapon => "L85A2", price => 250, damage => 15, type => "Rifle", rarity => "Basic", description => "The L85A2 is a bullpup assault rifle, it is very powerful and a iron-sight comes standard.", options => "You can give it a Laser." ) ); and say I wanted to echo the price. my output would be this: 250 now I just want my function to be able to do just that, select a specific field/value from the array. Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359725 Share on other sites More sharing options...
MasterACE14 Posted October 2, 2007 Author Share Posted October 2, 2007 Its working perfectly now! <?php // Select 1 field from an Array function select_array($array,$num,$field) { return $array[$num][$field]; } Thanks heaps guys Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/71351-solved-selecting-1-field-from-a-multi-dimensional-array/#findComment-359781 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.