My array looks like this:
{
"fileinfo": {
"database": "homestead",
"git": "master",
"date": 12,
"year": 2018,
"month": "October"
}
}
I want to pickup gitBranch, date, month, year from the array and print as
1 string
I tried vsprintf and sprintf, but cannot figured out.
Assuming you have this array as $array
$str = sprintf('Branch: %s, Created: %s %s %s.',
$array['fileinfo']['gitBranch'],
$array['fileinfo']['date'],
$array['fileinfo']['month'],
$array['fileinfo']['year']
);
echo $str;
<?php
$array = array(
"metadata" => array(),
"fileinfo" =>array(
"database" =>"homestead",
"connection" => "mysql",
"gitBranch" => "master",
"minutes" => 41,
"hours" => 13,
"date" => 12,
"year" => 2018,
"month" => "October"
)
);
echo "Branch: {$array['fileinfo']['gitBranch']}, Created: {$array['fileinfo']['date']} {$array['fileinfo']['month']} {$array['fileinfo']['year']}";
This creates the output you desire.
For multiple arrays, do a foreach()
through them with that inside, but obviously the $array
variable will be whatever you set it to in the loop.