I have a multi-dimensional array like so:
$a = array(
'potatoe'=> array(
'weight'=>24,
'label'=>'kon',
'year'=>2001,
),
'apple'=> array(
'weight'=>55,
'label'=>'krakat',
'year'=>1992,
)
);
I am looking for a way to searching the fruit name (with its values) when I only know weight is 55 and year is 1992. How to do that?
Something like this perhaps
foreach ($a as $key => $value) {
if ($value['weight'] == 55 && $value['year'] == 1992) {
echo $key;
}
}
Outputs
apple
You will have to iterate over it using foreach and test every element.
function findFruit($array,$weight, $year){
foreach($array as $key => $fruit){
if($fruit['weight'] == $weight && $fruit['year'] == $year ){
return $key;
}
}
}
then, just use the function:
$theFruit = $a[findFruit($a,55,1992)];
Related
I have an array that has several amounts (based on $$$ sales) attached to an id (where some of the ids are the same based on whom made a sale). My goal is to gather a total of amounts and attach each total to whichever id made the sale (truncating the identical ids from the array). I'm honestly not sure how to go about this as I'm still learning.
while ($row) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
//$operearnedArray returns the array with each "amount" attached to "id"
foreach ($operearnedArray as $key => $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
if(array_key_exists($value['id'], $operSums)) {
$operSums[$value['id']] += $value['amount'];
} else {
//I'm not sure where to go from here...
}
}
Your code looks fine to me, as for the comment, a simple
$operSums[$value['id']] = $value['amount'];
should do the trick. Of course it is adviseable to check for existent keys, but
foreach ($operearnedArray as $key => $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
$operSums[$value['id']] += $value['amount'];
}
should work just as well, as you can see in this demo.
try:
while ($row) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
$operSums = array();
foreach ( $operearnedArray as $sale ) {
if ( ! $sale['id'] ) {
continue;
}
$operSums[$sale['id']] += $sale['amount'];
}
// do something with the result, let's just look at it:
var_dump( $operSums );
It works: http://codepad.org/xGMNQauW
I want to know what is the value of the 2nd index of my array.
I have something like this:
$a[][1]= 10;
$a[][0]= 20;
$a[][1]= 12;
$a[][0]= 25;
I want to get only the values from $a wich 2nd index is 1.
I need to do a comparison and if the answer is correct, get the value
From this example, elements will be:
$a[0][1]
$a[2][1]
I know about array_key_exists but i dont know how to use it with Multidimensional arrays.
Any help?
Thanks.
Solution I was looking for.
$a = array();
$a[][1] = b1;
$a[][0] = b2;
$a[][0] = c1;
$a[][1] = c2;
foreach ($a AS $key => $aVal )
if (array_key_exists(1,$aVal))
echo $a[$key][1] . "<br>";
Have you tried isset($a[$specified_index])?
Here is some code:
foreach ( $a AS $key => $aVal ) {
if ( array_key_exists(1,$aVal) ) {
var_dump($key,$aVal);
}
}
foreach($a as $key => $value)
{
if(isset($value[1]))
{
if($value[1] == 1)
{
echo "value is 1";
}
else
{
echo "value is not 1 but is ".$value[1];
}
}
}
thx to #Havelock
array:
array(
['name'=>'kevin','value'=>'10'],
['name'=>'sam','value'=>'20']
);
how can i return value where name='sam' for example ?
and what how can i do it in even deeper array
array(
[0]=>array( 'inputs'=>
array(['name'=>'kevin','value'=>'10'],['name'=>'sam','value'=>'20']
),
[1]=>array( 'inputs'=>
array(['name'=>'kim','value'=>'10'],['name'=>'kirki','value'=>'20']
)
);
you need a recursive array_search - all answers above handle an exact amount of depth (in this case 2) only.
something like
function recursive_array_search($needle,$haystack) {
foreach ($haystack as $key=>$value) {
if ($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
return $value['value'];
}
}
return false;
}
recursive_array_search('sam', $start_array);
$arr = array(
array("name"=>"A","info"=>"one"),
array("name"=>"B","info"=>"two"),
array("name"=>"C","info"=>"three")
);
foreach($arr as $v){
if ($v['name']==="A"){
echo $v['info'];
}
}
In Deep Level
$arr = array(
array("input"=>array(
"name"=>"A",
"info"=>"one"
)),
array("input"=>array(
"name"=>"B",
"info"=>"Two"
))
);
foreach($arr as $subarr){ // First foreach iterate through arrays and next foreach iterate through values of each sub array
foreach($subarr as $v){
if ($v['name']==="A"){
echo $v['info'];
}
}
}
for($i=0;$i<count($array);$i++)
{
if($array['name']=="sam")
{
echo $array['value'];
}
}
and for next array you can do like this....
for($i=0;$i<count($array);$i++)
{
for($j=0;$j<count($array[$i]['inputs']);$j++)
{
if($array[$i]['inputs'][$j]['name']=="sam")
{
echo $array[$i]['inputs'][$j]['info'];
}
}
}
$new_array = array();
foreach ($old_array as $value) {
$new_array[$value['name']] = $value['value'];
}
var_dump($new_array['kevin']); // prints 10
i am trying to generate dynamic array as
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key]=$value;
}
and got output as
array(
$data['Hieght'] => 5,
$data['Width'] =>6
)
But i need output as
array(
$data['Hieght'] >= => 5,
$data['Width'] >= =>6
)
i tried this
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key].">=".=$value;
}
this is not Working.Anybody have an Idea About this.
Thanks in Advance.
I'm going to go out on a limb and guess that you want to concatenate the string ">=" to each key if the value is not empty:
$data = array();
foreach ($this->data['Carcase'] as $key => $value) {
if ($value) {
$data[$key . ' >='] = $value;
}
}
I need to get one time occurence on my array, with my code I get only first result here is my example code:
$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
for ($i=0; $i<count($arr); $i++)
{
if($arrs[$arr[$i]]==1)
{
//do something...in this example i expect to receive b c and d
}
}
Thanks in advance
ciao h
$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
for ($i=0; $i<count($arr); $i++)
{
if($arrs[$arr[$i]]==1)
{
echo $arr[$i];
}
}
That should display bcd
$arr=array("a","a","b","c","d");
$result = array();
$doubles = array();
while( !empty( $arr ) ) {
$value = array_pop( $arr );
if( !in_array( $value, $arr )
&& !in_array( $value, $doubles ) ) {
$result[] = $value;
}
else {
$doubles[] = $value;
}
}
May be you've miss your real results:
$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
/*
now $arrs is:
array (
'a' => 2,
'b' => 1,
'c' => 1,
'd' => 1,
)
*/
foreach($arrs as $id => $count){
if($count==1) {
// do your code
}
}
/*******************************************************/
/* usefull version */
/*******************************************************/
$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
foreach($arr as $id ){
if($arrs[$id]==1){
// do your code
echo "$id is single\n";
}
}
You just need to retrieve any value which only occurs once in the array, right? Try this:
$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
foreach ($arrs as $uniqueValue => $count)
{
if($value == 1) {
echo $uniqueValue;
}
}
array_count_values returns an associative array where the key is the value found and its value is the number of times it occurs in the original array. This loop simply iterates over each unique value found in your array (i.e. the keys from array_count_values) and checks if it was only found once (i.e. that key has a value of 1). If it does, it echos out the value. Of course, you probably want to do something a bit more complex with the value, but this works as a placeholder.
$count = 0;
foreach(array("a","a","b","c","d") as $v){
if($v == 1){$count++;}
}