Fill an array in php with two array - php

I have two numeric arrays (these arrays will always have the same number of keys and values).
$array1 = array(0 => "key1", 1 => "key2");
$array2 = array(0 => "value1", 1 => "value2");
$array_final = array(value of the $array1 => $value of the array2);
If I write a while and fill the $array_final, it only fills with the last key and value
So it's like:
for ($i = 0; $i < count($array(1))
{
$array_final = array($array1[$i] => $array2[$i]);
}
$array_final = array("key2" => "value2");
But I want:
$array_final = array("key1" => "value1", "key2" => "value2");

You want array_combine
It does exatcly what you need
So basically
$array1 = array(0 => "key1", 1 => "key2");
$array2 = array(0 => "value1", 1 => "value2");
$array_final = array_combine($array1, $array2);

for ($i=0; $i< sizeof($array1) && $i< sizeof($array2) ; $i++)
{
$array_final[$array1[$i]] =$array2[$i];
}

You may try below code.
$array1 = array(0 => "key1", 1 => "key2");
$array2 = array(0 => "value1", 1 => "value2");
$array_final = combine_if_same_keys($array1,$array2);
print_r($array_final);
function combine_if_same_keys( $array_one, $array_two ) {
$expected = false;
ksort($array_one);
ksort($array_two);
$diff = array_diff_key($array_one, $array_two);
if( empty($diff) && count($array_one) == count($array_two) ) {
$expected = array_combine( $array_one, $array_two );
}
return $expected;
}

Related

Compare 2 arrays after removing the keys that does not exist in both using array functions

I have 2 arrays a and b which may or may not have similar values.
$a = array('id' => 1, 'name' => 'John Doe', 'age' => 35);
$b = array('name' => 'John Doe', 'age' => 35);
I need to check whether the keys that are there in both the arrays contains same values or not using the array functions itself. Please help me.
NB: Array $a is always the parent array. If any key has to be popped, it would be only from $a.
You can use the following comparison based on array_intersect_assoc:
$b == array_intersect_assoc($a, $b)
This will be true when all of the $b key/value pairs occur in $a, false otherwise.
Use this code:
Use: array_diff_key
Remove duplicate key:
<?php
$a = array('id' => 1, 'name' => 'John Doe', 'age' => 35);
$b = array('name' => 'John Doe', 'age' => 35);
$c = array_diff_key($a, $b);
print_r($c); //Array ( [id] => 1 )
?>
Get duplicate key:
Use: array_intersect_key
<?php
function array_duplicate_keys() {
$arrays = func_get_args();
$count = count($arrays);
$dupes = array();
// Stick all your arrays in $arrays first, then:
for ($i = 0; $i < $count; $i++) {
for ($j = $i+1; $j < $count; $j++) {
$dupes += array_intersect_key($arrays[$i], $arrays[$j]);
}
}
return array_keys($dupes);
}
print_r(array_duplicate_keys($a, $b)); //Array ( [0] => name [1] => age )
?>
Get duplicate key and value:
<?php
function get_keys_for_duplicate_values($my_arr, $clean = false) {
if ($clean) {
return array_unique($my_arr);
}
$dups = $new_arr = array();
foreach ($my_arr as $key => $val) {
if (!isset($new_arr[$val])) {
$new_arr[$val] = $key;
} else {
if (isset($dups[$val])) {
$dups[$val][] = $key;
} else {
$dups[$val] = array($key);
}
}
}
return $dups;
}
print_r(get_keys_for_duplicate_values($a, $b));
//Array ( [id] => 1 [name] => John Doe [age] => 35 )
?>
If $b can have keys not present in $a, you can use array_intersect_key two times, with arguments in reversed order and check if both results are the same:
$ab = array_intersect_key($a, $b);
$ba = array_intersect_key($b, $a);
$allValuesEqual = ($ab == $ba);
use this:"it compare both key and value"
$result=array_diff_assoc($a1,$a2); //<-Compare both key and value, gives new array
example:
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","b"=>"pink");
$result=array_diff_assoc($a1,$a2);
print_r($result);
//result will be
Array ( [b] => green )

PHP - How to check two arrays and search for matching keys and merge the values

How to check two arrays and search for matching keys and merge the values of the 1st array with the matching keys of the second array.Please help me as I'm new to this.
example :
1st array = {id => 11,name => 'name',age => 18 }
2nd array = {id,name,age,school}
I want to get the result by adding the matching values to the 2nd array
2nd array = {id => 11,name => 'name',age => 18,school => }
try this
$a = ['id' => 11,'name' => 'name','age' => 18];
$b = array_flip(['id','name','age','school']);
foreach($b as $key => &$value){
$value = '';
}
$result = array_merge($b, $a);
One of the simple way is looping
$first= array('id' => 11,'name' => 'name','age' => 18 );
$second = array('id','name','age','school');
foreach ($second as $value) {
if(isset($first[$value])){
$final[$value] = $first[$value];
}
};
print_r($final);
Second Array flip and array merge
$first = ['id' => 11,'name' => 'name','age' => 18];
$second= array_flip(['id','name','age','school']);
foreach($second as $key => s$value){
$value = '';
}
$result = array_merge($second, $first);
print_r($result);
Use array_merge
<?php
$array1 = array('id' => '11', 'name' => 'name', 'age' => 18);
$array2 = array('id','name','age','school');
$array3 = array_merge(array_fill_keys($array2, null), $array1);
print_r($array3);
?>

Array Difference for multi dimentional array

I have two array
$array1 = array
(
array('A',0),
array('B',0),
array('C',0),
array('D',0),
array('E',0),
array('F',0),
)
$array2 = array
(
array('A',5),
array('B',6),
array('C',10),
array('F',23),
)
$array2 will be changing sometimes A keys is there or its not there. It is applied for all keys.
I want to create a new array or replace the array values in $array1 to
$array1 = array
(
array('A',5),
array('B',6),
array('C',10),
array('D',0),
array('E',0),
array('F',23),
)
Try something like below
if(count($array1) > count($array2)){
$tempArr1 = $array1;
$tempArr2 = $array2;
}else{
$tempArr1 = $array2;
$tempArr2 = $array1;
}
$newArr = array();
foreach($tempArr1 as $values){
$a = $values[0]; $n = $values[1];
foreach($tempArr2 as $key=>$val){
if($val[0] == $a){
$n = ($val[1] > $n) ? $val[1] : $n;
unset($tempArr2[$key]);
}
}
$newArr[] = array($a, $n);
}
print_r($newArr);
$array1 = array (
array('A',0),
array('B',0),
array('C',0),
array('D',0),
array('E',0),
array('F',0),
);
$array2 = array (
array('A',5),
array('B',6),
array('C',10),
array('F',23),
);
foreach( $array2 as $itemKey2 => $itemVal2 ) {
$found = false;
foreach( $array1 as $itemKey1 => $itemVal1 ) {
if( $itemVal1[0] == $itemVal2[0] ) {
$found = true;
$array1[$itemKey1][1] = $itemVal2[1];
break;
}
}
if( !$found )
$array1[] = $item2;
}
echo var_export( $array1, true );
In retrospect, this scenario seems needlessly complicated. Unless something else truly requires this structure, if possible use something like:
$array1 = array (
'A' => 0,
'B' => 0,
'C' => 0,
'D' => 0,
'E' => 0,
'F' => 0
);
$array2 = array (
'A' => 5,
'B' => 6,
'C' => 10,
'F' => 23
);
foreach( $array2 as $key => $val ) {
$array1[$key] = $val;
}

php array merge and convert to json

I have two arrays (below). Is it possible to convert them into json string?
Array
(
[0] => size
[1] => color
)
Array
(
[0] => L
[1] => Black
)
Output structure should be:
[
{"name":"size","value":"L"},
{"name":"color","value":"Black"}
]
Thanks!
Sure:
$array1 = array('size', 'color');
$array2 = array('L', 'Black');
$jsonArray = array();
foreach (array_combine( $array1, $array2 ) as $name => $value) {
$jsonArray[] = array('name' => $name, 'value' => $value);
}
echo $json = json_encode($jsonArray);
This gives you
[{"name":"size","value":"L"},{"name":"color","value":"Black"}]
this here should work:
$json = json_encode( array_combine( $array1, $array2 ) );
Something like this should work just how you want:
<?php
$keys = array("size", "color");
$values = array("L", "Black");
$array = array();
foreach ($keys as $i => $key) {
$array[] = array(
"name" => $key,
"value" => $values[$i]
);
}
$json = json_encode($array);
var_dump($json);
//string(62) "[{"name":"size","value":"L"},{"name":"color","value":"Black"}]"
?>
$array1 = array('size', 'color');
$array2 = array('L', 'Black');
$result = array_combine($array1 , $array2);
$json = array();
foreach($result as $key => $val){
$json[] = array('name' => $key, 'value' => $value);
}
$json = json_encode($json);
I think you are looking for this:
$array1 = array('size', 'color');
$array2 = array('L', 'Black');
for($i=0;$i<sizeof($array1);$i++)
{
$array3[]=array($array1[$i]=>$array2[$i]);
}
echo json_encode($array3);
?>
Output:
[{"size":"L"},{"color":"Black"}]

I try to sort an array using sort() but it fails

if ( $_GET['_value'] == 'moto' )
{
$array[] = array('1' => 'Yamaha');
$array[] = array('2' => 'Suzuki');
$array[] = array('3' => 'Triumph');
$array[] = array('4' => 'KTM');
$array[] = array('5' => 'Honda');
$array[] = array('6' => 'Harley Davidson');
$array[] = array('7' => 'Buell');
$array[] = array('8' => 'MV Agusta');
$array[] = array('9' => 'Ducati');
$array[] = array('10' => 'Other');
}
$array = sort($array);
echo json_encode( $array );
that is the code i have and its pulled by a chained dropdown.
I want it to return the values sorted alphabetically but based on the code you see it returns an empty array. what could be the mistake I am making /
Your code fails because you have an array of arrays here.
You should either search for "sort php array by sub-array key"
Or you can try something like:
$array[1] = 'Yamaha';
$array[2] = 'Suzuki';
// ...
sort($array);
echo json_encode($array);
Your array contains arrays, hence cannot be sorted, try:
$array[1] = 'Yamaha';
$array[2] = 'Suzuki';
then sort($array)
You can use uasort() function
like:
function cmp($a, $b) {
$a = reset($a);
$b = reset($b);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
uasort($array, 'cmp')
It's not at all pretty but this does the job.
Unless you're constrained otherwise you should really use some of the other suggestions.
<?
if ( $_GET['_value'] == 'moto' ) {
$array[] = array('1' => 'Yamaha');
$array[] = array('2' => 'Suzuki');
$array[] = array('3' => 'Triumph');
$array[] = array('4' => 'KTM');
$array[] = array('5' => 'Honda');
$array[] = array('6' => 'Harley Davidson');
$array[] = array('7' => 'Buell');
$array[] = array('8' => 'MV Agusta');
$array[] = array('9' => 'Ducati');
$array[] = array('10' => 'Other');
foreach($array as $i => $v)
{
$v = array_values($v);
$sort[] = $v[0];
}
sort($sort);
$c = 0;
foreach($sort as $i => $v)
{
$c++;
$sorted[] = array($c=>$v);
}
echo json_encode($sorted);
}
?>

Categories