array_resize - chung-leong/qb GitHub Wiki

array_resize - Change the dimension of a variable-length array

void array_resize( array &$array, uint32 $dim1 [, uint32 $...] )

array_resize() changes the dimensions of an variable-length array. Existing elements will maintain their original indices. If the operation results in an expansion of the array, new elements are set to zero. If the operation results in contraction of the array, elements beyond the new dimension are removed.

Only dimensions that were declared to be variable can be changed. If a dimension was declared to be fixed, you must either omit it or provide a constant number matching that in the type declaration.

Parameters

array - The array to resize.

dim1 - The first dimension.

... - Additional dimensions. If omitted, a given dimension does not change.

Return Value

None.

Example

<?php

/**
 * @engine	qb
 * @local	int32[][]	$a
 * @return	void
 */
function test_function() {
	echo "$a\n";  
	// []

	// expand both dimensions to 2 
	array_resize($a, 2, 2);
	echo "$a\n";  
	// [0, 0], [0, 0](/chung-leong/qb/wiki/0,-0],-[0,-0)

	// expand both dimensions to 3 
	$a = [1, 2], [3, 4](/chung-leong/qb/wiki/1,-2],-[3,-4);
	array_resize($a, 3, 3);
	echo "$a\n";
	// [1, 2, 0], [3, 4, 0], [0, 0, 0](/chung-leong/qb/wiki/1,-2,-0],-[3,-4,-0],-[0,-0,-0)

	// expand first dimension to 4, shrink second to 2 
	$a = [1, 2, 3], [4, 5, 6], [7, 8, 9](/chung-leong/qb/wiki/1,-2,-3],-[4,-5,-6],-[7,-8,-9);
	array_resize($a, 4, 2);
	echo "$a\n";
	// [1, 2], [4, 5], [7, 8], [0, 0](/chung-leong/qb/wiki/1,-2],-[4,-5],-[7,-8],-[0,-0)

	// shrink first dimension to 2 
	array_resize($a, 2);
	echo "$a\n";
	// [1, 2], [4, 5](/chung-leong/qb/wiki/1,-2],-[4,-5)
}

qb_compile();
test_function();

?>

Notes

If array had came from a GD image and the parameter was passed by reference, the image will be resize accordingly when the PHP+QB function returns. A fatal error will occur if the new width or height is zero.

Version

1.4 and above.