-1

I have an array like this and what I want to know is for example search the value 9375 in this array and then get the others value in this key.

Array
(
[0] => Array
    (
        [0] => Afghanistan
        [1] => 93
        [2] => 0.2
        [3] => 2015-06-09
    )

[1] => Array
    (
        [0] => Afghanistan Cdma Afghan Telecom
        [1] => 9375
        [2] => 0.22
        [3] => 2015-03-31
    )

[2] => Array
    (
        [0] => Afghanistan mobile
        [1] => 937
        [2] => 0.158
        [3] => 2015-03-31
    )

I tried to get the key like this but it doesnt works

$position = array_search('937', $array);

echo "<td>".$array[$position][0]."</td>";
echo "<td>".$array[$position][1]."</td>";
echo "<td>".$array[$position][2]."</td>";
echo "<td>".$array[$position][3]."</td>";
I_G
  • 413
  • 1
  • 4
  • 18

3 Answers3

3
$searchingValue = '9375';
$Values = [];


foreach($Array as $Record) {
 if(in_array($searchingValue, $Record)) {
  $Values[] = $Record;
 }
}
John V
  • 875
  • 6
  • 12
2
$arr = array(array((0)=>('Afghanistan'),(1)=>(93),(2)=>(0.2),(3)=>(2015-06-09)),
array((0)=>('Afghanistan Cdma Afghan Telecom'),(1)=>(9375),(2)=>(0.22),(3)=>(2015-03-31)),
array((0)=>('Afghanistan mobile'),(1)=>(937),(2)=>(0.158),(3)=>(2015-03-31)));

$search = '93';

foreach($arr as $value):
if(in_array($search, $value)):

echo "<td>".$value[0]."</td>";
echo "<td>".$value[1]."</td>";
echo "<td>".$value[2]."</td>";
echo "<td>".$value[3]."</td>";

endif;
endforeach;

@sarikaya Your Full Code, If You do have any query plz tell. Thank U

CodeLove
  • 462
  • 4
  • 14
1

array_filter is very good for taking a large array and searching through it to return a new array that contains only values that matched the search

$search = 9375;
$result = array_filter(
    $myArray,
    function ($value) use ($search) {
        return in_array($search, $value);
    }
);

If you're only looking for the search key in the second element of each sub-array:

$search = 9375;
$result = array_filter(
    $myArray,
    function ($value) use ($search) {
        return $value[1] == $search;
    }
);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385