Jump to content

[SOLVED] Selecting 1 field from a Multi-Dimensional Array


MasterACE14

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. :-\

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.