I Have an Array like this,
$arr = array_diff($cart_value_arr,$cart_remove_arr);
I Want to convert it to string without using implode method anyother way is there? And Also i want to remove $cart_remove_arr from $cart_value_arr the array_diff method i used it is correct?
4 Answers
You can use json_encode
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
This will also work on multi dimensional arrays
Will result to:
{"a":1,"b":2,"c":3,"d":4,"e":5}
Comments
Yet another way is to use serialize() (http://php.net/manual/en/function.serialize.php), as simple as...
$string = serialize($arr);
This can handle most types and unserialize() to rebuild the original value.
Comments
$cart_remove_arr = array(0 => 1, 1=> 2, 2 => 3);
$cart_value_arr = array(0 => 5, 1=> 2, 2 => 3, 3 => 4, 4 => 6);
$arr = array_diff($cart_value_arr, $cart_remove_arr);
echo '<pre>';
print_r($arr);
$string = "";
foreach($arr as $value) {
$string .= $value.', ';
}
echo $string;
**Output: Naveen want to remove $cart_remove_arr from $cart_value_arr
Remaining array**
Array
(
[0] => 5
[3] => 4
[4] => 6
)
he want a similar out as implode()
**String** 5, 4, 6,
var_exportmaybe? Not really sure what result you are looking for though...