0

How can I extract [p_id] value and pass as arguments in a function?

Array
(
    [0] => Array
        (
            [p_name] => X-Dot Motorbike Helmet
            [p_id] => 1001
            [p_price] =>  1.60
            [p_alt-variation-1] => Red
        )

    [1] => Array
        (
            [p_name] => Salt and Lemon Candy
            [p_id] => 1002
            [p_price] => 1.20
        )

    [2] => Array
        (
            [p_name] => Romoss Rolink Hybrid Cable
            [p_id] => 1003
            [p_price] => 13.90
        )

)

for example, I need these id's pass into a function for SQL statement:

function process($ids = array('1001','1002','1003')) {

    $sql = "SELECT * FROM tbl WHERE pid IN('1001','1002','1003') GROUP BY categoryId";

    ...

}
Nanny Boy
  • 107
  • 6

4 Answers4

2

use array_map() to get all the ids into $ids array:

   $ids = array_map(function($v){return $v[p_id];}, $array);

then you can pass the $ids array as a parameter of you function.

LF00
  • 27,015
  • 29
  • 156
  • 295
2

Suppose $array is your two-dimentional array, loop over it and add all the IDs to another array, and pass that as an argument

$ids = array(); // Declare array which will contain IDs
foreach ($array as $value) {
    // Loop over array, and get the IDs, put it into your $ids array
    $ids[] = $value['p_id'];
}

// Pass it as an argument
process($ids);

Then you'll need to adapt your query, as it's currently static. Use implode() to make an array into a string, like this

function process($ids = array() {
    $sql = "SELECT * FROM tbl WHERE pid IN('".implode("', '", $ids)."') GROUP BY categoryId";

    ...
}
Tolios
  • 219
  • 3
  • 12
  • thanks, but how to add single quote for these implode id? – Nanny Boy Dec 23 '16 at 10:30
  • I updated my answer with quotes around the strings. Simply put, you add quotes before and after the `implode()`, and then use it as part of the "glue" as it's called. – Tolios Dec 23 '16 at 10:33
  • 1
    If the IDs are integers though, shouldn't need quotes around them. Only if they are strings. Better yet though, just use prepared statements. – Tolios Dec 23 '16 at 10:35
1

The solution using array_column and implode functions:

// $data is your initial array
$ids = array_column($data, 'p_id');

function process($ids = []) {
    if (!empty($ids)) {
        $sql = "SELECT * FROM tbl WHERE pid IN(".implode(", ", $ids).") GROUP BY categoryId";
    } 

    ...

}
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1
  1. Firstly extract out value from p_id by using array_column (ie: http://php.net/manual/en/function.array-column.php)

    $value = array_column($array, 'p_id');
    
  2. then u can pass the $value into your function

    process($value);
    
  3. you implode your array inside your function before passing it into your sql

    function process($value = array()) {
        $value = "'".implode("', '", $value)."'";
        $sql = "SELECT * FROM tbl WHERE pid IN({$value}) GROUP BY categoryId";
    }
    
Tolios
  • 219
  • 3
  • 12
bugscoder
  • 425
  • 2
  • 6