I have an array structure like below.
//var_dump($data):
array(5) {
[0]=> string(1) "1"
[1]=> string(1) "2"
[2]=> string(4) "4=13"
[3]=> string(1) "4"
[4]=> string(3) "1=4"
}
Here value 1 and 4 has extension. So I need to get those values.
i.e Final output should be
$data = array(1,4);
$array = array("1", "2", "4=13", "4", "1=4");
$keys = array();
foreach ($array as $value) {
if (strpos($value, "=") !== false) {
list($key, $_) = explode("=", $value, 2);
$keys[] = (int) $key;
}
}
sort($keys);
var_dump($keys); // array(1, 4)
Related
I am receiving data that is an array of elements that contains an array of tags by language like this
[
{
"1": "tag_es1;tag_es2;tag_es3",
"2": "tag_en1;tag_en2;tag_en3"
},
{
"1": "tag_es1;tag_es2",
"2": "tag_en1;tag_en2"
}
]
I need to separate each tag by language, so i usearray_map to transform it like this
[
{
"1": [
"tag_es1",
"tag_es2",
"tag_es3"
],
"2": [
"tag_en1",
"tag_en2",
"tag_en3"
]
},
{
"1": [
"tag_es1",
"tag_es2"
],
"2": [
"tag_en1",
"tag_en2"
]
}
]
Bu what i need is the response to be like this
[
{
{
"1" : "tag_es1",
"2" : "tag_en1"
},
{
"1" : "tag_es2",
"2" : "tag_en2"
},
{
"1" : "tag_es3",
"2" : "tag_en3"
}
},
{
{
"1" : "tag_es4",
"2" : "tag_en4"
},
{
"1" : "tag_es5",
"2" : "tag_en5"
}
}
]
I tried using array_combine, array_walk, and manually doing it inside array_map, but with no success, what could i do?
Solution with special trick with null as callback of array_map:
$arr = json_decode($s, true);
$new_arr = [];
foreach ($arr as $item) {
$parts1 = explode(';', $item[1]);
$parts2 = explode(';', $item[2]);
// $new_arr[] = array_map(null, $parts1, $parts2);
$tmp_arr = array_map(null, $parts1, $parts2);
$new_arr[] = array_map(
function($v) { return array_combine(["1","2"], $v); },
$tmp_arr
);
}
You can loop the array and build a temporary array.
This array can then be looped and used array_column on to get the corresponding values to the new array.
$arr = json_decode($json, true);
foreach($arr as $key1 => $sub){
foreach($sub as $item){
$temp[] = explode(";", $item);
}
foreach($temp[0] as $key2 => $val){
$new[$key1][]= array_combine([1,2],array_column($temp, $key2));
}
$temp =[]; // empty array
}
var_dump($new);
Output:
array(2) {
[0]=>
array(3) {
[0]=>
array(2) {
[1]=>
string(7) "tag_es1"
[2]=>
string(7) "tag_en1"
}
[1]=>
array(2) {
[1]=>
string(7) "tag_es2"
[2]=>
string(7) "tag_en2"
}
[2]=>
array(2) {
[1]=>
string(7) "tag_es3"
[2]=>
string(7) "tag_en3"
}
}
[1]=>
array(2) {
[0]=>
array(2) {
[1]=>
string(7) "tag_es1"
[2]=>
string(7) "tag_en1"
}
[1]=>
array(2) {
[1]=>
string(7) "tag_es2"
[2]=>
string(7) "tag_en2"
}
}
}
https://3v4l.org/qgCA1
Added "1","2" as keys
The leanest, cleanest approach is three nested foreach loops.
Assign the dynamic keys when pushing data in the result array.
Code: (Demo)
$result = [];
foreach (json_decode($json, true) as $i => $row) {
foreach ($row as $id => $delimited) {
foreach (explode(';', $delimited) as $key => $value) {
$result[$i][$key][$id] = $value;
}
}
}
var_export($result);
Output of var_dump($arr)
array(4) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(2) "4
"
}
[1]=>
array(4) {
[0]=>
string(1) "5"
[1]=>
string(1) "6"
[2]=>
string(1) "7"
[3]=>
string(2) "8
"
}
[2]=>
array(4) {
[0]=>
string(1) "9"
[1]=>
string(2) "10"
[2]=>
string(2) "11"
[3]=>
string(3) "12
"
}
[3]=>
array(4) {
[0]=>
string(2) "13"
[1]=>
string(2) "14"
[2]=>
string(2) "15"
[3]=>
string(2) "16"
}
}
i Want to remove \n in $arr array.
I tried to use array_walk($arr,'intval'); but it did not work, because it is multiple dimensional array. What's the solution?
Is there any inbuilt PHP function? Or I need to use loops and remove it?
P.S: I am a newbie, try not to get too technical.
You can just do
array_walk_recursive($arr, function(&$v) { $v = trim($v); });
You cannot use trim directly as the callback, because it doesn't accept arguments by reference, so you have to wrap it in a callback that does.
Demo https://eval.in/904410
You can achieve this via loop and array_map()
$arr = array(
array("1", "2", "3", "4\n"),
array("5", "6", "7", "8\n"),
array("9", "10", "11", "12\n"),
array("13", "14", "15", "16\n"),
);
// result array
$result = [];
// Loop thru array
foreach ($arr as $value) {
// Map thru $value with trim to remove \n then push to result
$result[] = array_map('trim', $value);
}
// Output
echo('<pre>');
print_r($result);
There are alternatives, but recursively looping through your array gives you flexibility about what to remove, not only newlines:
function removeNewline($array) {
$result = array();
foreach ($array as $key => $value) {
// If the array value is an array itself, call the function recursively
if (is_array($value)) {
$result[$key] = removeNewline($value);
} else {
// Only remove newlines from strings
if (is_string($value)) {
$result[$key] = preg_replace('/\s+/', '', $value);
} else {
$result[$key] = $value;
}
}
return $result;
}
Played for hours, but couldn't do this. Task looks very simple, though..I need recursively combine 2 arrays into one. Using first array's values as keys, and second array's leave values as they are. This is what I have:
array(2) {
[0]=>
array(4) {
[0]=>
string(9) "First"
[1]=>
string(6) "Something"
}
[1]=>
array(4) {
[0]=>
string(3) "More"
[1]=>
string(6) "Nomore"
}
}
Second array
array(2) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
[1]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
What I'm trying to achive:
array(2) {
[0]=>
array(4) {
["First"]=>
string(1) "1"
["Something"]=>
string(1) "2"
}
[1]=>
array(4) {
["More"]=>
string(1) "1"
["Nomore"]=>
string(1) "2"
}
}
Another solution using array_combine
$first_array = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$second_array = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$new_array = array();
foreach($first_array AS $k => $v) {
$new_array[$k] = array_combine($v,$second_array[$k]);
}
$firstArray = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$secondArray = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$newArray = array();
for ($i=0; $i<count($firstArray); ++$i) {
$subArray1 = $firstArray[$i];
$subArray2 = $secondArray[$i];
$newArray[$i] = array();
for ($j=0; $j<count($subArray1); ++$j) {
$key = $subArray1[$j];
$value = $subArray2[$j];
$newArray[$i][$key] = $value;
}
}
var_dump($newArray);
Wouldn't be more elegant to do something like this ?
$newArray = array();
foreach ($firstArray as $key => $firstVal)
foreach ($secondArray as $key => $secondVal)
array_push($newArray, array_combine($firstVal, $secondVal));
This way you'll have the same result you wanted inside $newArray
with a bit simpler code.
I haven't tested that though, let me know if it works or breaks :)
Here is an example...
I have the following code:
$a=array("a","b","c");
$b=array("1","2","3");
$c = array_merge($a,$b);
echo "<pre>";
var_dump($c);
echo "</pre>";
Gives me an output:
array(6) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "1"
[4]=>
string(1) "2"
[5]=>
string(1) "3"
}
How could I change the code so that it gives me this output instead:
array(3) {
[0]=>
string(5) "a','1"
[1]=>
string(5) "b','2"
[2]=>
string(5) "c','3"
Any ideas?
$c = array_map(function ($a, $b) { return "$a','$b"; }, $a, $b);
For whatever that's good for...
Using SPL's MultipleIterator:
$a = array("a","b","c");
$b = array("1","2","3");
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($a));
$mi->attachIterator(new ArrayIterator($b));
$c = array();
foreach($mi as $row) {
$c[] = $row[0] . "','" . $row[1];
}
var_dump($c);
If the keys to both arrays are always in parity, you could do something like
foreach ($a as $key => $value) {
$newArray[] = "$value','{$b[$key]}";
}
var_dump($newArray);
// would output the below
array(3) {
[0]=>
string(5) "a','1"
[1]=>
string(5) "b','2"
[2]=>
string(5) "c','3"
However, the result looks a little weird, are you sure this is what you are trying to achieve?
$a=array("a","b","c");
$b=array("1","2","3");
if(count($a) > count($b)){
foreach($a as $key => $value)
$c[$key] = isset($b[$key])? $value.",".$b[$key] : $value;
} else {
foreach($b as $key => $value)
$c[$key] = isset($a[$key])? $a[$key].",".$value : $value;
}
Use above code it will for your array.
Is there a way to set the value of members of an array with foreach?
<?
$arr = array(0=>'a',1=>'b',2=>'c',3=>'d');
foreach($arr as $key => $value){
$value = 'a';
}
var_dump($arr);
?>
returns:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
}
Where what I am trying to get it to return is:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "a"
[2]=>
string(1) "a"
[3]=>
string(1) "a"
}
Here is a link to the codepad I was using.
http://codepad.org/FQpPYFtz
$arr = array(0=>'a',1=>'b',2=>'c',3=>'d');
foreach($arr as $key => &$value) { // <-- use reference to $value
$value = 'a';
}
var_dump($arr);
It is quite simple:
foreach ($data as $key => $value) {
$data[$key] = 'new value';
}