0

Please help me to convert array to string.

array look like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [red] => 255
                    [green] => 255
                    [blue] => 255
                    [alpha] => 127
                )

            [1] => Array
                (
                    [red] => 255
                    [green] => 255
                    [blue] => 255
                    [alpha] => 127
                )
             ...

        )

)

I tried to use the implode function, but no result...

$string = implode(", ", $pxlCorArr);

PS: Sorry for my english i from ukraine.

5
  • 2
    What do you want the output string to look like? Commented Nov 4, 2012 at 12:30
  • You would surely get some result but it looks like you are converting a 2 dimensional array to a string. As Tim asked, what should the output look like? Commented Nov 4, 2012 at 12:31
  • i whant to output red,green,blue,alpha Commented Nov 4, 2012 at 12:32
  • @VolodyaDaniliv Do you want an array of strings red,green,blue,alpha? Commented Nov 4, 2012 at 12:33
  • yes, i need one string like this: 255.255.255.127,255.255.255.123,... Commented Nov 4, 2012 at 12:35

4 Answers 4

5

Array:

$pxlCorArr = 
  array(
      array (
           array('red' => 255, 
                 'green' => 255,
                 'blue' => 255,
                 'alpha' => 127
           ),

           array('red' => 255,
                 'green' => 255,
                 'blue' => 255,
                 'alpha' => 127
           )
      )
  );

Code:

$output = '';
foreach ($pxlCorArr as $subArray) {
    if(is_array($subArray)) {
        foreach ($subArray as $subArray2) {
            if(is_array($subArray2)) {
                $output .= implode ('.', $subArray);
                $output .= ',';
            }
        }
    }
}
$output = rtrim($output, ',');

Output:

255.255.255.127,255.255.255.127

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

6 Comments

implode() would be less expensive, instead of the whole concatenation and trimming ;-)
tell me please where i need to insert input array
just change $array to the name of your array, which I guess is $pxlCorArr, right?
thx! but result is: Array.Array.Array.Array.Array.Array.Array.Array.Array.
can you help me to fix this trouble?
|
2

That would be another possibility, as a function to which you pass your initial array and the function returns the string you needed:

function getRGBAlpha($pxlCorArr) {

    $rgbVals = array();
    foreach($pxlCorArr as $subArr) {
        if(is_array($subArr)) {
            foreach($subArr as $colValues) {
                $rgbVals[] = implode('.', $colValues);
            }
        }
    }

    return implode(',', $rgbVals);
}

and so you could do the following, somewhere in your code, to get the output you needed:

echo getRGBAlpha($pxlCorArr);

should output:

255.255.255.127,255.255.255.127

Comments

1
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [red] => 255
                    [green] => 255
                    [blue] => 255
                    [alpha] => 127
                )

            [1] => Array
                (
                    [red] => 255
                    [green] => 255
                    [blue] => 255
                    [alpha] => 127
                )
             ...

        )

)


$string = '';
$array = $exists_array[0];
foreach ($array as $key => $value) {
$string .= $key." : ".$value."\n";
}
print $string;

Comments

0

Using array_walk over lambda function:

  $implodevals =  create_function('&$val', '$val = implode(".", $val);');
  array_walk($array, $implodevals);
  print rtrim(implode(", ", $array), ",");

Input array:

   $array = Array
    (
        0 => Array
            (
                "red" => 255,
                "green" => 255,
                "blue" => 255,
                "alpha" => 127
            ),

        1 => Array
            (
                "red" => 255,
                "green" => 255,
                "blue" => 255,
                "alpha" => 127,
            )



     );

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.