Create existing array array

Asked

Viewed 107 times

2

I have the following line of code:

$valores = implode(", ", array_values($dados));

and, by giving a var_dump(array($valores)); the following information is returned:

array(1) { [0]=> string(26) "Igor, Test, [email protected]" }

However, I need every comma to return each item in a new array (in this case, an array with 3 items). That is, create an array of that other array($valores).

What’s the best way to do that?

EDITED

I’ve tried using the explode:

$arrValores = array($valores);
$itemValores = explode(", ", $arrValores);

foreach($itemValores as $item) {
    $item;
}

var_dump($item);

But what is returned to me is:

string(5) "Array"

Thank you!

  • 1

    Or just not using the implode, only the array_values.

  • Yes, @Sergio. I edited the question.

  • 1

    @Igor and not use implode?????????

  • 1

    That’s right, @Guilhermenascimento. I have to use the implode, turns out, I was just creating another variable responsible for array_values only, and I hadn’t even thought about it. Thank you for clearing my mind!

1 answer

1


If you need to use the implode, then you can declare two variables, one for the formed string and the other for the array:

$valoresArray = array_values($dados);
$valoresStr = implode(", ", $valoresArray);

var_dump($valoresArray);
var_dump($valoresStr);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.