array filter for array values

sujara :

i have this code which does the job for searching option name how can i use it to search the option value from array.

$productspp ='[{
    "id": 4674388066436,
    "title": "1st march",
    "options": [{
        "id": 6046836162692,
        "product_id": 4674388066436,
        "name": "Size",
        "position": 1,
        "values": ["12", "24", "36"]
    }, {
        "id": 6067871875204,
        "product_id": 4674388066436,
        "name": "z",
        "position": 2,
        "values": ["blue", "green"]
    }, {
        "id": 6067871907972,
        "product_id": 4674388066436,
        "name": "Material",
        "position": 3,
        "values": ["silk", "cotton"]
    }],
}, {
    "id": 4674394325124,
    "title": "2nd march",
    "options": [{
        "id": 6046844190852,
        "product_id": 4674394325124,
        "name": "Title",
        "position": 1,
        "values": ["Default Title"]
    }],
}, {
    "id": 4679851704452,
    "title": "3rd marchhh",
    "options": [{
        "id": 6053112545412,
        "product_id": 4679851704452,
        "name": "Title",
        "position": 1,
        "values": ["Default Title"]
    }]
}]';

$array = json_decode($productspp,1);

    $filter_name555 ='options';
    $dummytstt ='values';
    $filter_value=  blue;


 $expected = array_filter($array, function($el) use ($filter_name555, $dummytstt, $filter_value) {

    return ( stripos($el[$filter_name555][0][$dummytstt], $filter_value) !== false ); 
}

}); 

if the user searched option_value and it matches then it should list that product so in this case if user searches silk then it should list that product else not

for option name it works for option value it does not work as stripos expect it to be string but here in data it is array.

we tried in_array also to filter but that also did not work

when we search anything like 12 or 24 or 36 or blue or green then it should list this part of json. i mean this product and the code i have given above does the same but for option name. u can see that option value is array. it can have more than one values so my code is failing.

{
    "id": 4674388066436,
    "title": "1st march",
    "options": [{
        "id": 6046836162692,
        "product_id": 4674388066436,
        "name": "Size",
        "position": 1,
        "values": ["12", "24", "36"]
    }, {
        "id": 6067871875204,
        "product_id": 4674388066436,
        "name": "z",
        "position": 2,
        "values": ["blue", "green"]
    }, {
        "id": 6067871907972,
        "product_id": 4674388066436,
        "name": "Material",
        "position": 3,
        "values": ["silk", "cotton"]
    }],
}
Ja͢ck :

You need to distinguish between an array value or regular value, because they need to be matched differently.

One thing you could do is write logic for if a value is an array, and then force any other kind of value into an array of just one element.

$key = 'options';
$attr = 'values';
$search = 'blue';

$expected = array_filter($array, function($el) use ($key, $attr, $search) {
  $values = $el[$key];
  if (is_array($value)) {
    $values = array_column($value, $attr);
  } else {
    $value = array($value);
  }

  foreach ($value as $body) {
    foreach ((array)$body as $contents) {
      if (stripos($contents, $search) !== false) {
        return true;
      }
    }
  }
  return false;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350560&siteId=1