3

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.

3
  • 1
    what will be output for the 6? Commented Feb 12, 2016 at 9:53
  • 6 is not the key to perform search, bro. 6 is actually vendor_id. and 3 and 2 are product_id. we don't do search based on vendor_id, but based on product_id. and the result is 24 or 10. which are number of pcs. Commented Feb 12, 2016 at 9:59
  • 1
    see my answer hope this will help you Commented Feb 12, 2016 at 10:04

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

bro, I tried : var_dump(getNextSequence($order_list, 8, 2)); this should give me 33 as result. but why your code give me NULL?
it seems your code skip the first row of $order_list
@RobertHanson its because if($key) condition its escaping 0 as well. Corrected my answer. Please have a look
1
<?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
    }
}
?>

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.