Archive for the ‘Arrays’ Category

Convert array to string

Friday, October 19th, 2007

Problem: You want to convert an array to string so that it is easier to print the result instead of looping inside the array and have this printed

Old Solution:

$aArr = array("Have you ever seen",
"the beauty of the world.",
"The wonderful creation of the Lord." );
foreach($aArr as $sString) {
    print $sString;
}

Other Solution:

$aArr = array("Have you ever seen",
"the beauty of the world.",
"The wonderful creation of the Lord." );
$sString = implode(" ", $aArr);
print $sString;

In Action: I want to execute a linux command via PHP and get its result and extract the desire content. The example below will try to extract the ip address from the ifconfig command

exec("ifconfig", $aOutput);
$sTempString = implode(" ", $aOutput);
preg_match("/inet\saddr:(.*?)\s/i", $sTempString, $aMatch);
print $aMatch[1]; //