Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 IlluminateSupportArr;

$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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...