Create a multicolumn array in PHP from string - php

I want to create a multicolumn array from a string where the spearator for the first level array is ";" and for the second level array is ","
This string is given:
$string = ("de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3")
This is the structure of the array that i want as result:
Array
(
array([de] => text1, text2, text3),
array([en] => text1, text2, text3),
array([pl] => text1, text2, text3),
)

The solution using array_map, substr, strpos and explode functions:
$string = ("de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3");
$items = array_map(function($v){
$sep_pos = strpos($v, ","); // position of key/values separator
return [substr($v, 0, $sep_pos) => substr($v, $sep_pos + 1)];
}, explode(";", $string));
print_r($items);
The output:
Array
(
[0] => Array
(
[de] => text1,text2,text3
)
[1] => Array
(
[en] => text1,text2,text3
)
[2] => Array
(
[pl] => text1,text2,text3
)
)

You can use array_map and pass array by using explode, and inside callback you can explode again with , and store it in a variable.
there after you can grab the value using array_shift, which needed to be used as a key, and return a new array.
$string = "de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3";
$result = array_map(function($value) {
$temp = explode(',', $value);
return [array_shift($temp) => implode(',', $temp)];
}, explode(';', $string));
print_r($result);
Example: https://eval.in/581893

Try:
$string = "de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3";
$mainArr = explode(";",$string);
$finalArr = array();
foreach($mainArr as $arr) {
$tempArr = explode(",",$arr);
reset($tempArr);
$mainK = $tempArr[0];
unset($tempArr[key($tempArr)]);
$finalArr[][$mainK] = implode(",", $tempArr);
}
print '<pre>';print_r($finalArr);print '</pre>';
Output:
Array
(
[0] => Array
(
[de] => text1,text2,text3
)
[1] => Array
(
[en] => text1,text2,text3
)
[2] => Array
(
[pl] => text1,text2,text3
)
)

Try This Code
$string = "de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3";
$array1 = explode(';', $string);
foreach($array1 as $value){
$temp=explode(',', $value);
$key=$temp[0];
unset($temp[0]);
$new_array[][$key]=$temp;
}

Related

Convert a one dimensional array to two dimensional array

I have an array, whose structure is basically like this:
array('id,"1"', 'name,"abcd"', 'age,"30"')
I want to convert it into a two dimensional array, which has each element as key -> value:
array(array(id,1),array(name,abcd),array(age,30))
Any advice would be appreciated!
I tried this code:
foreach ($datatest as $lines => $value){
$tok = explode(',',$value);
$arrayoutput[$tok[0]][$tok[1]] = $value;
}
but it didn't work.
Assuming you want to remove all quotation marks as per your question:
$oldArray = array('id,"1"', 'name,"abcd"', 'age,"30"')
$newArray = array();
foreach ($oldArray as $value) {
$value = str_replace(array('"',"'"), '', $value);
$parts = explode(',', $value);
$newArray[] = $parts;
}
You can do something like this:
$a = array('id,"1"', 'name,"abcd"', 'age,"30"');
$b = array();
foreach($a as $first_array)
{
$temp = explode("," $first_array);
$b[$temp[0]] = $b[$temp[1]];
}
$AR = array('id,"1"', 'name,"abcd"', 'age,"30"');
$val = array();
foreach ($AR as $aa){
$val[] = array($aa);
}
print_r($val);
Output:
Array ( [0] => Array ( [0] => id,"1" ) [1] => Array ( [0] => name,"abcd" ) [2] => Array ( [0] => age,"30" ) )
With array_map function:
$arr = ['id,"1"', 'name,"abcd"', 'age,"30"'];
$result = array_map(function($v){
list($k,$v) = explode(',', $v);
return [$k => $v];
}, $arr);
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => "1"
)
[1] => Array
(
[name] => "abcd"
)
[2] => Array
(
[age] => "30"
)
)

Combine two arrays together

I have an array that looks like this:
array
(
[name] => name
[description] => description here
[first] => Array
(
[0] => weight
[1] => height
)
[second] => Array
(
[0] => 20 kg
[1] => 50 cm
)
[company_id] => 1
[category_id] => 7
)
what function will allow me to combine these into something that looks like the following?
array
(
[together]
(
[0] => weight 20kg
[1] => height 50cm
)
)
Update
For that current array you need to use the loop.
$first = $second = array();
foreach($yourArray as $key => $array) {
if(in_array($key, array('first', 'second')) {
$first[] = $array[0];
$second[] = $array[1];
}
}
$final['together'] = array($first, $second);
According to the first array
You can try this -
$new = array(
'together' => array(
implode(' ', array_column($yourArray, 0)), // This would take out all the values in the sub arrays with index 0 and implode them with a blank space
implode(' ', array_column($yourArray, 1)), // Same as above with index 1
)
);
array_column is supported PHP >= 5.5
Or you can try -
$first = $second = array();
foreach($yourArray as $array) {
$first[] = $array[0];
$second[] = $array[1];
}
$final['together'] = array($first, $second);
you also can try array_map as below
function merge($first,$second)
{
return $first ." ".$second;
}
$combine = array_map('merge', $yourArray[0],$yourArray[1]);

multidimensional array implode whole inner array elements function in php

Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
)
[2] => Array
(
[0] => d
[1] => e
[2] => f
)
)
I want to convert above two dimensional array into following array using implode or any other function in php
Array
(
[0]=>a,b
[1]=>c
[2]=>d,e,f
)
You can use a loop and implode them and store them -
foreach($yourArray as $key => $array) {
$yourArray[$key] = implode(',', $array);
}
Or array_map also help -
$new = array_map(function($array) {
return implode(',', $array);
}, $yourArray);
$newArray = array();
foreach ($array $k => $v) {
$newArray[$k] = implode(",", $v);
}
If you want to change your existing array,
array_walk($your_array, function(&$v){
$v = implode(',', $v);
});
If you want a new array,
$new_array = array_map('implode', $your_array,
array_fill(0, count($your_array), ',')
);

How to convert string "str1:1,str2:2,str33:33... " to array on PHP

Please, tell me, how I can convert string "str1:1,str2:2,str33:33... " to this array:
Array
(
[1] => Array
(
[mystr] => str1
)
[33] => Array
(
[mystr] => str33
)
[2] => Array
(
[mystr] => str2
)
)
Go with the foreach way....
Explanation :
Explode the elements using the comma operator and pass them as the arguments for your foreach construct. Now explode() the argument using : as the delimiter and then add the first exploded element as the value, the second exploded element as the key.
foreach(explode(',',$str) as $v)
{
$v = explode(':',$v);
$newarr[$v[1]]['mystr']=$v[0];
}
print_r($newarr);
Working Demo
The array_map() way...
<?php
$newarr = array();
array_map(function ($v) use(&$newarr){ $v = explode(':',$v);$newarr[$v[1]]['mystr']=$v[0]; },explode(',','str1:1,str2:2,str33:33'));
print_r($newarr);
Try this:
$str = "str1:1,str2:2,str33:33";
$array = explode(",",$str);
$newarray = array();
foreach($array as $key=>$value){
$value = explode(":",$value);
$newarray[$value[1]]['mystr'] = $value[0];
}
print_r($newarray);
PHP explode() function breaks a string into an array use explode for break your string comma (,) and foreach iteration to again explode colon (:) sign.
Check this demo
<?php
$str = "str1:1,str2:2,str33:33";
foreach(explode(',',$str) as $s){
$s = explode(':',$s);
$array[$s[1]]['mystr']=$s[0];
}
print_r($array);
?>
Result
Array ( [1] => Array ( [mystr] => str1 ) [2] => Array ( [mystr] => str2 ) [33] => Array ( [mystr] => str33 ) )

Merge a string in an array

I have this array:
$fields = $_GET['r'];
Which has some ids, for example:
Array (
[0] => 3134
[1] => 3135 )
and then I have this string in $tematiche:
3113,3120
How can I marge this string in the first array? Also, how can I remove the equals id (if any)?
Try,
$fields = array_unique(array_merge($fields,explode(",",$tematiche)));
echo "<pre>";
print_r($fields);
Try this :
$fields = $_GET['r'];
$string = '3113,3120';
$array = explode(",",$string);
$res_array = array_unique(array_merge($fields,$array));
echo "<pre>";
print_r($res_array);
Output :
Array (
[0] => 3134
[1] => 3135
[2] => 3113
[3] => 3120
)
A combination of explode(), array_merge() and array_unique() would be suitable.
// Make $tematiche into an array
$tematiche_array = explode(',', $tematiche);
// Merge the two arrays
$merged_array = array_merge($fields, $tematiche_array);
// Remove all duplicate values in the array
$unique_array = array_unique($merged_array);
Please try like this. This should work for you.
$a = explode(',', '3113,3120');
print_r(array_merge($fields,$a));

Categories