I have an array output results in this format
Array ( [0] => stdClass Object ( [ID] => 8 [Value] => 7 ) [1] => stdClass Object ( [ID] => 9 [Value] => 8 ) [2] => stdClass Object ( [ID] => 10 [Value] => 7 ) [3] => stdClass Object ( [ID] => 11 [Value] => 7 ) [4] => stdClass Object ( [ID] => 12 [Value] => 6 ) [5] => stdClass Object ( [ID] => 13 [Value] => 9 ) [6] => stdClass Object ( [ID] => 14 [Value] => 5 ) [7] => stdClass Object ( [ID] => 15 [Value] => 7 ) [8] => stdClass Object ( [ID] => 16 [Value] => 7 ) [9] => stdClass Object ( [ID] => 17 [Value] => 3 ) [10] => stdClass Object ( [ID] => 18 [Value] => 5 ) )
Now I would like to search the array for a specific ID and retrieve its associated value.
For example say $searchID = 13. So search the arrary for ID 13 and should return the result of Value as 9.
I have tried multiple solutions but each time results in null value which means I am doing something wrong.
I can simply print a specific array value like this $outputarray[5]->Value which would output the array to me. But any way to search and retrieve values as per above array?
Any help is highly appreciated.
Thanks
Ok I found the solution searching the forum and the web. Thanks. The thread may be closed
Please post solution for the benefit of others.
Sure I am not sure if I could post solutions from other forums on this forum. If its a yes, I would gladly post the solution
Yes please post the solution.
Ok this is the solution that worked for me
$arrayval = here the array comes;
$id = X the value you wish to search for ID;
foreach($arrayval as $item){
if ($item->ID == $id)
return $item->Value;
}
return "not found";
}
I have just been playing and discovered:
<?php
$arrayVal = array
(
8 => 7,
9 => 8,
10 => 7,
11 => 7,
12 => 6,
13 => 9,
14 => 5,
15 => 7,
16 => 7,
17 => 3,
18 => 5,
);
$obj2 = new stdClass(); // on-the-fly!
$obj2->{8} = 7;
$obj2->{9} = 8;
$obj2->{10} = 7;
$obj2->{11} = 7;
$obj2->{12} = 6;
$obj2->{13} = 9;
$obj2->{14} = 5;
$obj2->{15} = 7;
$obj2->{16} = 7;
$obj2->{17} = 3;
$obj2->{18} = 5;
$id = 13;
echo '<pre>';
echo '$arrayVal = ';
print_r($arrayVal);
echo 'function getIdFromObject( $id, $arrayVal)<br />';
echo 'result = ' .getIdFromObject( $id, $arrayVal );
echo '<br /><br /><br />';
echo '$obj2 = ';
print_r($obj2);
echo 'getIdFromObject( $id, $obj2 )<br />';
echo 'result = ' .getIdFromObject( $id, $obj2 );
echo '<br /><br /><br />';
$obj = (object) $arrayVal;
echo '$obj = ';
print_r($obj);
echo 'getIdFromObject( $id, $obj )<br />';
echo 'result = ' .getIdFromObject( $id, $obj );
echo '</pre>';
function getIdFromObject( $id, $objArray )
{
$result = 'cannot find $id = ' .$id;
// accepts both Obj AND arrays
foreach( $objArray as $key => $value )
{
if ($key == $id):
$result = 'Found $id = ' .$value;
endif;
}
return $result;
}
Output:
$arrayVal = Array
(
[8] => 7
[9] => 8
[10] => 7
[11] => 7
[12] => 6
[13] => 9
[14] => 5
[15] => 7
[16] => 7
[17] => 3
[18] => 5
)
function getIdFromObject( $id, $arrayVal)
result = Found $id = 9
$obj2 = stdClass Object
(
[8] => 7
[9] => 8
[10] => 7
[11] => 7
[12] => 6
[13] => 9
[14] => 5
[15] => 7
[16] => 7
[17] => 3
[18] => 5
)
getIdFromObject( $id, $obj2 )
result = Found $id = 9
$obj = stdClass Object
(
[8] => 7
[9] => 8
[10] => 7
[11] => 7
[12] => 6
[13] => 9
[14] => 5
[15] => 7
[16] => 7
[17] => 3
[18] => 5
)
getIdFromObject( $id, $obj )
result = Found $id = 9
Ok I think @John_Betong would work great as we simply create a function which could handle both the arrays as well as object arrays. Great solution. Thanks for the help