Insert function & html button inside each array

I got this code, how to insert function & html button inside each array?

<?php
 
$arr = [
    'status' => 1,
    'msg' => [
            'total_records' => 5,
            'total_pages' => 1,
            'page_number' => 1,
            'per_page' => 100,
            'return_count' => 5,
            'page_result' => [
                    0 => [
                          'items' => 223687201,
                          'stock' => 'hat',
                          'stock' => 0,
                      ],
 
                    1 => array
                      (
                          'items' => 218508001,
                          'category' => 'bags',
                          'stock' => 3,
                      ),
 
                    2 => array
                      (
                          'items' => 180097801,
                          'category' => 'shirt',
                          'stock' => 5,
                      ),
 
                    3 => array
                        (
                            'items' => 178000403,
                            'category' => 'shirt',
                            'stock' => 2,
                        ),
 
					4 => array
                        (
                            'items' => 200052001,
                            'category' => 'shoes',
                            'stock' => 1,
                        ),
 
                ],
 
        ],
 
    'errcode' => 0,
];
 
function get_product($array, $search) {
	$products = [];
	for($i=0; $i < count($array); $i++) {
		foreach($array[$i] as $key=>$value) {
			if($key == 'category' && $value == $search) {
				array_push($products, $array[$i]);
			}	
		}
	}
		return $products;
}
 
 
 
print_r(get_product($arr['msg']['page_result'], 'shirt'));

?>
$arr[] = function(){ return 123; };
$arr[] = '<button>do something</button>';

where to put the code?

Just after your array.

Its not working. Its not displaying anything.

just use print_r($arr);

Its only displaying all the array and adding 1 new array as below

 [0] => Closure Object
        (
        )

    [1] => <button>do something</button>

Exactly, that’s what you asked for:

sorry sir, but its not in each array

i wanted it to be like this

Array
(
[0] => Array
(
[items] => 223687201
[category] => hat
[stock] => 0
[button] => ‘value-223687201’
)

[1] => Array
    (
        [items] => 218508001
        [category] => bags
        [stock] => 3
        [button] => '<button>value-218508001</button>'
    )
[2] => Array
    (
        [items] => 180097801
        [category] => shirt
        [stock] => 5
        [button] => '<button>value-180097801</button>'
    )
[3] => Array
    (
        [items] => 178000403
        [category] => shirt
        [stock] => 2
        [button] => '<button>value-178000403</button>'
    )
[4] => Array
    (
        [items] => 200052001
        [category] => shoes
        [stock] => 1
        [button] => '<button>value-200052001</button>'
    ) 

)

use array_walk

array_walk($arr['msg']['page_result'], function(&$item){
    ...
});

How to use the code?

copy and paste, modify to your needs, use sample i already posted.

ok, thank you. I have completed the code

Sponsor our Newsletter | Privacy Policy | Terms of Service