hecate0211's avatar

PHP array output

i have an array like this

    $arrays = array(array('a','b','c','d','e'), array(1,2,3,4,5));

and i want to show this array like this [['a','b','c','d','e'],[1,2,3,4,5]]

i have try using print_r and dd but it cant show up like that.. can u help me?

0 likes
4 replies
goatshark's avatar

@hecate0211 Are you trying to re-order the elements in the outer array? You could try array_reverse($arrays).

shakti's avatar

@hecate0211 Sorry but your question is not clear

You just want to show that array if yes them print_r, dd, var_dump,var_export these function show array result in their format

lindstrom's avatar
Level 15

Super duper answermagic based on literal interpretation of question:

echo '[';
foreach ($arrays as $v) {
    foreach ($v as $x) {
        echo '[' . $x . ']';
    }
    if (! next($arrays) === false) {
        echo ',';   
    }
}
echo ']';

In all seriousness, could you provide some more detail on what you are trying to accomplish? Where are you wanting to print your array formatted that way? For what purpose?

Jaytee's avatar

In the future, make sure your question is understandable.

Fortunately, I was able to know what you need. You want the results to be the values and not the keys/indexes.

You need to use array_values

without array_values

array:2 [
  0 => array:5 [
    0 => "a"
    1 => "b"
    2 => "c"
    3 => "d"
    4 => "e"
  ]
  1 => array:5 [
    0 => 1
    1 => 2
    2 => 3
    3 => 4
    4 => 5
  ]
]

** with array_values **

array_values($arr);
[
     [
       "a",
       "b",
       "c",
       "d",
       "e",
     ],
     [
       1,
       2,
       3,
       4,
       5,
     ],
]

Please or to participate in this conversation.