Merge arrays. Second array as a "column" - php

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.

Related

Set array keys to incrementing letters

I have a PHP array, which looks like follows:
array(8) {
[0]=>
string(3) "639"
[1]=>
string(2) "33"
[2]=>
string(2) "68"
[3]=>
string(3) "196"
[4]=>
string(3) "275"
[5]=>
string(3) "309"
[6]=>
string(3) "331"
[7]=>
string(3) "378"
}
I would like to change all the keys to these values to incrementing letters (a, b, c, etc.) - how would I do this?
I realise I can increment letters like so:
$x = "a";
$x++;
echo $x;
"b"
But how can I perform this within a loop?
The desired result would be something like this:
"a" => "639"
"b" => "33"
"c" => "68"
etc.
I think the following can help
$newArray = array();
$index = "a";
foreach($oldArray as $value)
{
$newArray[$index] = $value;
$index++;
}
You have provided the answer pretty much yourself already
$array = array('639', '33', '68', '196', '275', '309', '331', '378');
$index = 'a';
$newArray = array();
foreach ($array as $value) {
$newArray[$index++] = $value;
}
Following code will surely help you:
$result = [];
array_walk($data,function($v,$k)use (&$result){
$result[chr(65 + $k)] = $v;
});
print_r($result);
Demo

Php extract array value

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)

Count from two array php

I have two array, arrLevel1 and arrLevel2.
I want to count animal that can walk.
How can I do that array stucture like this?
Thx before. I already tried, but it failed.
arrLevel1:
array(4) {
[0]=>
array(1) {
["Walk"]=>
string(4) "Bird"
}
[1]=>
array(1) {
["Walk"]=>
string(3) "Cat"
}
[2]=>
array(1) {
["Fly"]=>
string(9) "ButterFLy"
}
[3]=>
array(1) {
["Fly"]=>
string(4) "Bird"
}
}
arrLevel2:
array(3) {
[0]=>
array(1) {
["Animal"]=>
string(3) "Fly"
}
[1]=>
array(1) {
["Animal"]=>
string(11) "Walk"
}
[2]=>
array(1) {
["Human"]=>
string(11) "Walk"
}
}
Just check them in a loop
For the first arrLevel1:
<?php
$arrLevel1 = array(array("walk"=>"Bird"),array("walk"=>"Cat"),array("Fly"=>"Butterfly"),array("Fly"=>"Bird"));
$x = 0;
foreach($arrLevel1 as $p){
if($p["walk"]!==null){
$x++;
}
}
var_dump($x);
?>
Hope thsi helps you
One way to do it is using array_reduce():
$array = array(array('Walk' => 'Bird'), array('Walk' => 'Cat'), array('Fly' => 'ButterFly'), array('Fly' => 'Bird'));
$count = array_reduce($array, function($carry, $item) {
return key($item) == 'Walk' ? $carry + 1 : $carry;
}, 0);
var_dump($count);
You can use a similar code for the second version:
$array = array(array('Animal' => 'Fly'), array('Animal' => 'Walk'), array('Human' => 'Walk'));
$count = array_reduce($array, function($carry, $item) {
return reset($item) == 'Walk' ? $carry + 1 : $carry;
}, 0);
var_dump($count);

Combining multidimensional array (ones key with other's values)

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 :)

How to set the member values inside of a foreach loop

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';
}

Categories