问题
I have saved data from a form with Serialize array and Implode and Json_encode like bellow:
Serialize
+-------+----------------------------------------+-------------------------------------+----------------------------------------+
| Agent | Customers | Cars | Money |
+-------+----------------------------------------+-------------------------------------+----------------------------------------+
| SMITH | a:2:{i:0;s:4:"Jack";i:1;s:4:"Mike";} | a:2:{i:0;s:3:"BMW";i:1;s:4:"Audi";} | a:2:{i:0;s:5:"1000$";i:1;s:5:"1500$";} |
| ... | ... | ... | ... |
+-------+----------------------------------------+-------------------------------------+----------------------------------------+
Implode
+-------+------------+----------+-------------+
| Agent | Customers | Cars | Money |
+-------+------------+----------+-------------+
| SMITH | Jack,Mike | BMW,Audi | 1000$,1500$ |
| ... | ... | ... | ... |
+-------+------------+----------+-------------+
Json_encode
+-------+-----------------+----------------+-------------------+
| Agent | Customers | Cars | Money |
+-------+-----------------+----------------+-------------------+
| SMITH | ["Jack","Mike"] | ["BMW","Audi"] | ["1000$","1500$"] |
| .... | .... | ... | ... |
+-------+-----------------+----------------+-------------------+
I want to show data in admin panel like bellow:
Agent SMITH sold BMW car to Jack with 1000$ and Audi car to Mike with 1500$
---------------------
How can i do this with 3 way Unserialize, Unimplode and Json_decode ?
回答1:
So i am Considering Your Array
$yourArray = [
'Agent' => 'SMITH',
'Customers' => ["Jack", "Mike"],
'Cars' => ["BMW", "Audi"],
'Money' => ["1000$", "1500$"],
];
And Your Expected Output is
$yourExpectedOutPut = 'Agent SMITH sold BMW car to Jack with 1000$ and Audi car to Mike with 1500$';
So first import this to your name space
use Illuminate\Support\Arr;
$agentName = Arr::get($yourArray, 'Agent');
$customers = Arr::get($yourArray, 'Customers');
$cars = Arr::get($yourArray, 'Cars');
$Money = Arr::get($yourArray, 'Money');
foreach ( $customers as $customerIndex => $customerName)
{
$perparedArray[] = $cars[$customerIndex] . ' car to '. $customerName.' with '. $Money[$customerIndex];
}
$preparedString = 'Agent '. $agentName.' sold '.implode(' and ', $perparedArray);
echo $preparedString;
Agent SMITH sold BMW car to Jack with 1000$ and Audi car to Mike with 1500$
if you have any doubt or found any issues or if the answer is not what you are looking for kindly comment below
来源:https://stackoverflow.com/questions/56289138/how-can-i-unserialize-or-json-decode-or-un-implode-data-from-database-in-laravel