How Can I Unserialize Or Json Decode Or Un Implode Data From Database In Laravel Blade?

喜夏-厌秋 提交于 2019-12-13 07:47:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!