php - Explode string and create a new array after every 4 instances -


i have string this.

$string = "title1|1.99|www.website.com|www.website.com/img1.jpg|title2|5.99|www.website2.com|www.website2.com/img2.jpg|title3|1.99|www.website3.com|www.website3.com/img3.jpg|"; 

i wish explode string array create new array every 4 instances of pipe. nice name keys below.

eg:

    array (             [title] => title1             [price] => 1.99             [url] => www.website.com             [gallery] => www.website.com/img1.jpg   )      array (             [title] => title2             [price] => 5.99             [url] => www.website2.com             [gallery] => www.website2.com/img2.jpg     ) 

and on...

how can go achieving this?

you're looking array_chunk():

$string = 'title1|1.99|www.website.com|www.website.com/img1.jpg|title2|5.99|www.website2.com|www.website2.com/img2.jpg|title3|1.99|www.website3.com|www.website3.com/img3.jpg';  $keys = array('title', 'price', 'url', 'gallery');  $array = explode('|', $string); $array = array_chunk($array, 4);  $array = array_map(function($array) use ($keys) {     return array_combine($keys, $array); }, $array);  echo '<pre>', print_r($array, true), '</pre>'; 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -