fork download
  1. <?php
  2.  
  3. $arr = [
  4. 'status' => 1,
  5. 'msg' => [
  6. 'total_records' => 5,
  7. 'total_pages' => 1,
  8. 'page_number' => 1,
  9. 'per_page' => 100,
  10. 'return_count' => 5,
  11. 'page_result' => [
  12. 0 => [
  13. 'items' => 223687201,
  14. 'stock' => 'hat',
  15. 'stock' => 0,
  16. ],
  17.  
  18. 1 => array
  19. (
  20. 'items' => 218508001,
  21. 'category' => 'bags',
  22. 'stock' => 3,
  23. ),
  24.  
  25. 2 => array
  26. (
  27. 'items' => 180097801,
  28. 'category' => 'shirt',
  29. 'stock' => 5,
  30. ),
  31.  
  32. 3 => array
  33. (
  34. 'items' => 178000403,
  35. 'category' => 'shirt',
  36. 'stock' => 2,
  37. ),
  38.  
  39. 4 => array
  40. (
  41. 'items' => 200052001,
  42. 'category' => 'shoes',
  43. 'stock' => 1,
  44. ),
  45.  
  46. ],
  47.  
  48. ],
  49.  
  50. 'errcode' => 0,
  51. ];
  52.  
  53. function get_product($array, $search) {
  54. $products = [];
  55. for($i=0; $i < count($array); $i++) {
  56. foreach($array[$i] as $key=>$value) {
  57. if($key == 'category' && $value == $search) {
  58. array_push($products, $array[$i]);
  59. }
  60. }
  61. }
  62. return $products;
  63. }
  64.  
  65.  
  66.  
  67. print_r(get_product($arr['msg']['page_result'], 'shirt'));
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [items] => 180097801
            [category] => shirt
            [stock] => 5
        )

    [1] => Array
        (
            [items] => 178000403
            [category] => shirt
            [stock] => 2
        )

)