Reputation: 91
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.
Upvotes: 0
Views: 1895
Reputation: 3310
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,
)
);
Upvotes: 0
Reputation: 18933
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
Upvotes: 5
Reputation: 6986
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
Upvotes: 2
Reputation: 1386
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;
Upvotes: 1