I have the following array and by using array_push & I am getting not the right result.
Array:
Array
(
[0] => 1039
[1] => 417
[2] => 418
)
Array Push:
array_push($array, array("a","b","c"));
Result:
Array
(
[0] => 1039
[1] => 417
[2] => 418
[3] => Array
(
[0] => a
[1] => b
[2] => c
)
)
I want the a,b,c append to value 417 for example .
Disirable result:
Array
(
[1039] => 1039
[417] => Array
(
[0] => a
[1] => b
[2] => c
)
[418] => 418
)
How can this be done?
SOLUTION:
$data = Array (
0 => 1039,
1 => 417,
2 => 418,
);
foreach( $data as $key => $val ) {
$new_data[$val] = 0;
}
foreach( $new_data as $k => $v ){
if( $k == 417 ){
$new_data[$k] = array( 'p' => 50, 'pp' => 75 );
}
}
print_r($new_data);
It doesn't really make sense, but this will do what you show in your example:
$array[1] .= print_r(array("a","b","c"), true);
.= does string concatenation, and passing true as the second argument to print_r makes it return the string that it would have printed.
The result of this is that $array[1] is a string that begins with 417 and is followed by the printed representation of the added array. There's no actual array in there. I'm not sure what you plan to do with this, but it matches your example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With