1

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?

2
  • 1
    var_export maybe? Not really sure what result you are looking for though... Commented Dec 18, 2017 at 7:33
  • 1
    There's probably a million ways to convert an array to a string. What is your goal? Commented Dec 18, 2017 at 7:48

4 Answers 4

4

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}

Doc: http://php.net/manual/en/function.json-encode.php

Sign up to request clarification or add additional context in comments.

Comments

1

Another way is the classic foreach plus string concatenation

$string = '';
foreach ($arr as $value) {
    $string .= $value . ', ';
}
echo rtrim($string, ', ');

Comments

1

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

0
$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, 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.