I am learning more about for loops and would like to see how do you merge arrays using only for loops and not using built-in PHP functions such as array_merge().
I know you can use foreach to do this, but how would this work using only for loops and not foreach?
foreach example:
$array1 = ['judas', 'john', 'michael'];
$array2 = ['fernando', 'jsamine', 'sam', 'walter'];
$array3 = ['judas', 'john', 'mike', 'steve'];
foreach ([$array1, $array2, $array3] as $arr) {
foreach ($arr as $values) {
...
}
}
Yes, you can do this using just for loops.
$array1 = ['judas', 'john', 'michael'];
$array2 = ['fernando', 'jsamine', 'sam', 'walter'];
$array3 = ['judas', 'john', 'mike', 'steve'];
$all_arrays = [$array1, $array2, $array3];
$merged = [];
for ($i = 0; $i < 3; $i++) {
$arr = $all_arrays[$i];
$x = count($arr);
for ($j=0; $j < $x; $j++) {
// Using the value as the key in the merged array ensures
// that you will end up with distinct values.
$merged[$arr[$j]] = 1;
}
}
// You could use array_keys() to do get the final result, but if you
// want to use just loops then it would work like this:
$final = [];
$x = count($merged);
for ($i=0; $i < $x; $i++) {
$final[] = key($merged);
next($merged);
}
var_dump($final);
key() and next() ARE php functions. But I really do not know of a way to get to the keys without using either foreach or some php function.
Iterate each array on its own:
foreach($array2 as $v)
if(!in_array($v, $array1))
$array1[] = $v;
foreach($array3 as $v)
if(!in_array($v, $array1))
$array1[] = $v;
Or simply use array_merge() - there is no reason for do not do it.
$a=array('1','2','3','4','5');
$b=array('a','b','c','d','e');
$c=count($b);
for($i=0; $i<$c; $i++)
{
$a[]=$b[$i]; // each element 1 by 1 store inside the array $a[]
}
print_r($a);
function arrayMerge(array $arrays) {
$mergeArray = [];
foreach ($arrays as $key => $array) {
foreach($array as $finalArray) {
$mergeArray[] = $finalArray;
}
}
return $mergeArray;
}
$array1 = [
'Laravel', 'Codeigniter', 'Zend'
];
$array2 = [
'Node js', 'Vue js', 'Angular js'
];
$array3 = [
'Python', 'Django', 'Ruby'
];
print_r(arrayMerge([$array1, $array2, $array3]));
$arr1 = array(1,2,3,4,5);
$arr2 = array(2,5,6,7,8);
$allval = array();
foreach($arr2 as $key=>$val){
$arr1[] = $val;
}
foreach($arr1 as $k=>$v){
if(!in_array($v,$allval)){
$allval[] = $v;
}
}
echo "<pre>"; print_R($allval);
Related
I want to combine three arrays into one
I am expecting like output are using array_combine or array_merge
Likes
friends,
usa,
usa2,
..,
..,
so.on..,
$array1 = array('likes', 'friends', 'USA');
$array2 = array('USA2', 'lools', 'tools');
$array3 = array('USA3', 'Awesome', 'lop');
$output = $array1+$array2+$array3;
echo "<pre>";
print_r($output);
echo "</pre>";
But here i am getting output as
likes,
friends,
USA
In PHP 5.6+ You can also use ... when calling functions to unpack an array or
Traversable variable or literal into the argument list:
$array1 = array('likes', 'friends', 'USA');
$array2 = array('USA2', 'lools', 'tools');
$array3 = array('USA3', 'Awesome', 'lop');
array_push($array1, ...$array2, ...$array3);
echo "<pre>";
print_r($array1);
echo "</pre>";
demo
check this out custom function for array push
<?php
$array1 = array('likes', 'friends', 'USA');
$array2 = array('USA2', 'lools', 'tools');
$array3 = array('USA3', 'Awesome', 'lop');
function push($array1,$array2){
$return = array();
foreach($array1 as $key => $value){
$return[] = $value;
}
foreach($array2 as $key => $value){
$return[] = $value;
}
return $return;
}
$array = push($array1,$array2);
$array = push($array,$array3);
print_r($array);
If you want to have an array of arrays, each element at a given index having the values of the three arrays at the respecting index, then:
$length = count($array1);
$result = array();
for ($index = 0; $index < $length; $index++) {
$result[]=array($array1[$index], $array2[$index], $array3[$index]);
}
If you simply want to put all items into a new array, then:
$result = array();
for ($index = 0; $index < count($array1); $index++) {
$result[]=$array1[$index];
}
for ($index = 0; $index < count($array2); $index++) {
$result[]=$array2[$index];
}
for ($index = 0; $index < count($array3); $index++) {
$result[]=$array3[$index];
}
Use Array Merge
$array1 = array('likes', 'friends', 'USA');
$array2 = array('USA2', 'lools', 'tools');
$array3 = array('USA3', 'Awesome', 'lop');
$output = array_merge($array1,$array2,$array3);
echo "<pre>";
print_r($output);
echo "</pre>";
I have 2 arrays as follows:
$arr = array('one.jpg', 'two.jpg', 'three.jpg');
$arr1 = array('', 'five.jpg', '');
Now I want to make these two arrays to one array with following value:
$newArray = array('one.jpg', 'five.jpg', 'three.jpg');
How can I do this using PHP?
Use array_filter to remove the empty values.
Use array_replace to replace the values from the first array with the remaining values of the 2nd array.
$arr1=array_filter($arr1);
var_dump(array_replace($arr,$arr1));
Assuming you want to overwrite entries in the first array only with truthy values from the second:
$newArray = array_map(function ($a, $b) { return $b ?: $a; }, $arr, $arr1);
You can iterate through array and check value for second array :
$arr = array('one.jpg', 'two.jpg', 'three.jpg');
$arr1 = array('', 'five.jpg', '');
$newArray =array();
foreach ($arr as $key => $value) {
if(isset($arr1[$key]) && $arr1[$key] != "")
$newArray[$key] = $arr1[$key];
else
$newArray[$key] = $value;
}
var_dump($newArray);
Simple solution using a for loop, not sure whether there is a more elegant one:
$arr = array('one.jpg', 'two.jpg', 'three.jpg');
$arr1 = array('', 'five.jpg', '');
$newArray = array();
$size = count($arr);
for($i = 0; $i < $size; ++$i) {
if(!empty($arr1[$i])){
$newArray[$i] = $arr1[$i];
} else {
$newArray[$i] = $arr[$i];
}
}
I have 2 arrays in php
$array1[] = a,b,c,d,e;
$array2[] = 1,2,3,4,5;
$data = array('letter'=>$array1,'num'=>$array2);
return json_encode($data);
This will return:
[[a,b,c,d,e],[1,2,3,4,5]]
I'd like to return it in json_encode like this:
$data = [[1a,1],[b,2],[c,3],[d,4],[e,5]];
Can someone help me with this?
this is the simplest solution
$result = array();
foreach ($array1 as $k1 => $v1) {
$result[] = array($v1, $array2[$k1]);
}
echo json_encode($result)
but arrays must have the same length and same keys
Try below code, it is flexible and don't have to care about the length of the arrays.
<?php
$letters = array('a','b','c','d','e');
$numbers = array('1','2','3','4','5');
$counter = (sizeof($letters) > sizeof($numbers)) ? sizeof($letters) : sizeof($numbers);
$arr = array();
for($i=0; $i<$counter; $i++)
{
if(array_key_exists($i, $letters))
$arr[$i][] = $letters[$i];
if(array_key_exists($i, $numbers))
$arr[$i][] = $numbers[$i];
}
$json = json_encode($arr);
echo $json;
Output:
[["a","1"],["b","2"],["c","3"],["d","4"],["e","5"]]
Demo:
http://3v4l.org/7v7X4
What you are looking for is the function array_combine().
Here is an example:
$array1 = array("a","b","c","d","e");
$array2 = array(1,2,3,4,5);
$data = array_combine($array1, $array2);
$new_data = array();
foreach($data AS $key => $value) {
$new_data[] = array($key, $value);
}
print_r(json_encode($new_data));
Which should return something like:
[["a",1],["b",2],["c",3],["d",4],["e",5]]
UPDATE Changed the code to give the result wanted...
There is the following array:
arr1 = array(array('xxx',3),array('yyy',2));
I need to transform it into the array arr2, where the number of occurrence of each entry is equal to the 2nd column value in array arr1. For instance, for the above given arr1, arr2 should be the following:
arr2 = array(array('xxx'),array('xxx'),array('xxx'),array('yyy'),array('yyy'));
I wrote the following code, but my question is: Is it possible to do the same in a simpler way?
for ($i=0; $i<count($arr1); $i++) {
for ($j=0; $j<$arr1[i][1]; $j++) {
$arr2[] = array($arr1[0]);
}
}
I think a foreach is simpler and easier to read.
$arr1 = array(array('xxx', 3), array('yyy', 2));
$arr2 = array();
foreach ($arr1 as $arr)
{
for ($i = 0; $i < $arr[1]; $i++)
{
$arr2[] = array($arr[0]);
}
}
foreach ($arr1 as $entry) {
$arr2[] = array_fill(0, $entry[1], array($entry[0]));
}
$arr2 = call_user_func_array('array_merge', $arr2);
I wouldn't use it though. It's much less readable.
I have an array like this (one dimension only):
$arr = array('one', 'two', 'three', 'foo', 'bar', 'etc');
Now I need a for() loop that creates a new array from $arr, like that:
$newArr = array('one', 'onetwo', 'onetwothree', 'onetwothreefoo', 'onetwothreefoobar', 'onetwothreefoobaretc');
Seems to be simple but I can't figure it out.
Thanks in advance!
$mash = "";
$res = array();
foreach ($arr as $el) {
$mash .= $el;
array_push($res, $mash);
}
$newArr = array();
$finish = count($arr);
$start = 0;
foreach($arr as $key => $value) {
for ($i = $start; $i < $finish; $i++) {
if (isset($newArray[$i])) {
$newArray[$i] .= $value;
} else {
$newArray[$i] = $value;
}
}
$start++;
}