Saint Robson
Saint Robson

Reputation: 5525

How To Perform array_search In Multi Dimensional Array?

I have this code :

$order_list = array ( array ("tangible", 1, 8, 33, 19000),
                      array ("tangible", 1, 9, 8, 19000),
                      array ("tangible", 6, 3, 24, 19000),
                      array ("tangible", 6, 2, 10, NULL),
                      array ("tangible", 1, 17, 11, 28000));

$key = array_search(3, array_column($order_list, $order_list[2]));

and I want to get the value of $order_list[$i][3] based on $order_list[$i][2].

for example, if I put :

3 I will get 24 in return

9 I will get 8 in return

and so on...

I tried to use array_search :

$key = array_search(3, array_column($order_list, $order_list[2]));

but I got this error :

Warning: array_column(): The column key should be either a string or an integer in /home/***/public_html/***.php on line 8

Warning: array_search() expects parameter 2 to be array, boolean given in /home/***/public_html/***.php on line 8

how to perform array_serach in this case? thanks before.

Upvotes: 3

Views: 119

Answers (3)

Chetan Ameta
Chetan Ameta

Reputation: 7896

I created a general purpose function to get next value of current value in 2'd array. have a look on below function. Also look at variable description of function to understand input in function:

/***
 * @param array $array input array
 * @param $search_value value that need to be searched
 * @param $search_index index of inner array where current value exists
 * @return next value of current value
 */
function getNextSequence(array $array, $search_value, $search_index)
{
    $result = null;
    $key = array_search($search_value, array_column($array, $search_index));
    if ($key !== false) {
        $result = (isset($array[$key][$search_index + 1])) ? $array[$key][$search_index + 1] : null;
    }

    return $result;
}

$order_list = array(
    array("tangible", 1, 8, 33, 19000),
    array("tangible", 1, 9, 8, 19000),
    array("tangible", 6, 3, 24, 19000),
    array("tangible", 6, 2, 10, NULL),
    array("tangible", 1, 17, 11, 28000)
);

var_dump(getNextSequence($order_list, 3, 2)); //output: int(24)
var_dump(getNextSequence($order_list, 9, 2)); //output: int(8)
var_dump(getNextSequence($order_list, 10, 2)); //output: Null
var_dump(getNextSequence($order_list, 2, 2)); //output: int(10)

Upvotes: 1

Glynne Turner
Glynne Turner

Reputation: 149

another way.....

$search = 9;

$order_list = array ( array ("tangible", 1, 8, 33, 19000),
                  array ("tangible", 1, 9, 8, 19000),
                  array ("tangible", 6, 3, 24, 19000),
                  array ("tangible", 6, 2, 10, NULL),
                  array ("tangible", 1, 17, 11, 28000));




foreach ($order_list as $string){
    if ($string[2] == $search){
    print_r( $string);  
    }  
}

Upvotes: 1

Glynne Turner
Glynne Turner

Reputation: 149

<?php

$search = 9;

$order_list = array ( array ("tangible", 1, 8, 33, 19000),
                  array ("tangible", 1, 9, 8, 19000),
                  array ("tangible", 6, 3, 24, 19000),
                  array ("tangible", 6, 2, 10, NULL),
                  array ("tangible", 1, 17, 11, 28000));




foreach ($order_list as $string){

    if (in_array($search,$string)){
       //repsonse here
    }
}
?>

Upvotes: 1

Related Questions