php - How to split evenly and oddly a string to form an array of even and odd results OK Like -


i have php string formed images , corresponding prices ok like

$mystring = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100"; 

i know if do:

$myarray = explode(',', $mystring); print_r($myarray); 

i :

array (     [0] => ddb94-b_mgr3043.jpg     [1] => 3800     [2] => 83acc-b_mgr3059.jpg     [3] => 4100 ) 

but how split string can have associative array of form?

array (     "ddb94-b_mgr3043.jpg" => "3800"     "83acc-b_mgr3059.jpg" => "4100" ) 

easier way below:-

<?php $mystring = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100"; $chunks = array_chunk(explode(',', $mystring), 2); //chunk array 2-2 combination $final_array = array(); foreach($chunks $chunk){ //iterate on array   $final_array[trim($chunk[0])] = trim($chunk[1]);//make key value pair } print_r($final_array); //print final array 

output:-https://eval.in/859757


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -