How to create an index array for an associative array? - php

I have this multidimensional array:
$arr = [
2 => ["a", "b", "c"],
5 => ["j", "k", "l"],
9 => ["w", "x", "y", "z"]
];
For which I'd like to create a new index array like this one:
$index = [
"a" => 2,
"b" => 2,
"c" => 2,
"j" => 5,
"k" => 5,
"l" => 5,
"w" => 9,
"x" => 9,
"y" => 9,
"z" => 9
]
I couldn't find any PHP function that appears to do that, but I'm sure there is one. Or maybe there's some known code that does this efficiently?

$index = array();
foreach ($arr as $k => $a) {
foreach ($a as $v) {
$index[$v] = $k;
}
}

Related

Array Keys Comparison Without In-Build Function

I am having two arrays of key-value pairs.
Arrays as follows:
$array1 = ["a" => 2, "b" => 3, "c" => 1, "d" => 2];
$array2 = ["c" => 1, "d" => 1, "a" => 2, "b" => 3, "x" => 4, "z" => 1];
Problem Statement
I need to find the count of the number of keys of array1 present in array2 having the same value.
Example
Count will be 3 as keys a, b, and c of array1 are present in array2 and having same values 2, 3, and 1 respectivily.
Tried method
foreach($array1 as $key => $value){
if($array2.$key === $array1.$key){
if($array2[$value] === $array1[$value]){
$matchCount++;
}
}
}
NOTE: I am not sure about accessing the keys from the array object, so using dot(.), somehow I am getting the count, but not correct count.
<?php
$array1 = ["a" => 2, "b" => 3, "c" => 1, "d" => 2];
$array2 = ["c" => 1, "d" => 1, "a" => 2, "b" => 3, "x" => 4, "z" => 1];
$count = 0;
foreach($array1 as $key => $value){
$array_2_value = $array2[$key] ?? null;
if($array_2_value !== null && $array_2_value === $value) $count++;
}
echo $count;
Demo: https://3v4l.org/BREbg
You can just loop over $array1 and check if the key exists in $array2 with the same value. If yes, we increment count. You can make use of null coalescing operator ?? to check if the $key exists in $array2 or not.
function countSameKeyAndValues(array $array1, array $array2): int
{
$sameKeys = array_keys(array_intersect($array1, $array2));
$count = 0;
foreach ($sameKeys as $key) {
if ($array1[$key] === $array2[$key]) {
$count++;
}
}
return $count;
}
You can loop over the $array1 using foreach loop.
Then you can check for the values of $array2 using the key for $array1.
Then if value exist for that key, you can check if value is same at that of $array1
<?php
$array1 = ["a" => 2, "b" => 3, "c" => 1, "d" => 2];
$array2 = ["c" => 1, "d" => 1, "a" => 2, "b" => 3, "x" => 4, "z" => 1];
$count = 0;
foreach($array1 as $key => $value){
$array2Value = $array2[$key] ?? null;
if($array2Value !== null && $array2Value === $value)
$count++;
}
echo $count;

Array slice like hash slice in perl

Is there built-in function, or shorter way to extract elements into new array, as described here?
<?php
function arr_slice ($arr, $keys) {
$ret = array();
foreach ($keys as $k) { $ret[$k] = $arr[$k]; }
return $ret;
}
$array = array(
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
);
var_export(
arr_slice($array, array("x","d","b"))
);
output (key order matters)
array (
'x' => NULL,
'd' => 4,
'b' => 2,
)

PHP delete elements in associate array by value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove integers in array less than X with PHP
$array = array(
"a" => 10,
"b" => 9,
"c" => 8,
"d" => 7,
"e" => 6,
"f" => 5,
"g" => 4,
"h" => 3
);
How can I dele the item if the value is smaller than 6? i.e., how can I get the following array?
$array = array(
"a" => 10,
"b" => 9,
"c" => 8,
"d" => 7,
"e" => 6
);
array_filter is perfect for this:
$new = array_filter($old,function($a) {return $a >= 6;});
A simple iteration would be:
$new_array = array();
foreach($arras as $key => $value){
if($value >= 6)new_array[$key] = $value;
}
foreach($array as $index=>$value) {
if ( $value < 6) {
unset($array[$index]);
}
}
or
foreach($array as $index=>$arr_value) {
if ( $value >= 6) {
$new_array[$index] = $arr_value;
}
}

How to build a hierarchical associative array out of two other arrays like "group by"

I need help with an array problem.
First I have an array like you would receive from a sql query:
$aRows=array(
array("one" => 1, "two" => 2, "three" => 3, "a" => "4", "b" => "lala"),
array("one" => 1, "two" => 2, "three" => 3, "a" => "5", "b" => "lolo"),
array("one" => 1, "two" => 2, "three" => 3, "a" => "6", "b" => "lili")
)
Then I have another array which defines the hierarchy - lets say
$aArray=array("one", "two", "three");
Now I want to build a hierarchical associative array:
$newArray =
array(
"one" => array(
"two" => array(
"three" => array(
array("a" => "4", "b" => "lala"),
array("a" => "5", "b" => "lolo"),
array("a" => "6", "b" => "lili"),
)
)
)
);
Imagine a "grouped by" sql and I want the hierarchical representation.
EDIT: In the sample one,two,three contains the same values, this would not be the case in real life - assume:
select one,two,three,a,b from a_table group by one,two,three;
Of course SQL will group equals values in one,two,three together and this redundant information is useless. But sql can not display hierarchic data, so the fields are redundant.
EDIT 2: Thanks to Waygood the complete solution is:
$aRows=array(
array("one" => 1, "two" => 2, "three" => 3, "a" => "4", "b" => "lala"),
array("one" => 1, "two" => 2, "three" => 4, "a" => "5", "b" => "lolo"),
array("one" => 1, "two" => 2, "three" => 4, "a" => "6", "b" => "lili")
);
$aArray=array("one", "two", "three");
$output=array();
foreach($aRows as $row)
{
$newKey = "";
foreach($aArray as $key) // loop through the hierarchy to get the fields names
{
eval('$exist = array_key_exists(\'' . $row[$key] .
'\', $output' . $newKey . ');');
if (!$exist)
{
$newKey .= "['" . $row[$key] . "']";
eval('$output' . $newKey . '=array();');
}
}
eval('$output' . $newKey . '[]=$row;');
}
echo(var_export($output, true));
If you wanted to use the VALUES of the fields specified in the hierarchy you can:
$output=array();
foreach($aRows as $row)
{
reset($aArray);
$eval='$output';
foreach($aArray as $key) // loop through the hierarchy to get the fields names
{
if(is_numeric($row[$key]))
{
$eval.='['.$row[$key].']'; // build the key based on the number $row value of each field named
}
else
{
$eval.='[\''.$row[$key].'\']'; // build the key based on the string $row value
}
unset($row[$key]); // remove the field from the $row
}
$eval.='=$row;';
eval($eval); // evaluate the built up index and set it to the remaining data
}

Change array keys according to another one

I have 2 arrays:
$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc", 8 => "ddd", 9 => "eee", 11 => "fff");
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "", 9 => "", 13 => "");
I want to change the keys of $array1 according to $array2. I'm given the information that an element of the second array has to correspond to another of the first one. For example I know that $array2[6] has to correspond to $array1[4].
So I should have all the keys of $array1 changed according to this rule:
$array1 = array(3 => "aaa", 6 => "bbb", 7 => "ccc", 9 => "ddd", 13 => "eee", 2 => "fff");
I don't know how to solve this. I've tried to split the first array where the element given is but I'm stuck.
You can define a function that maps and constructs a new array.
function transfer_keys($key_array,
$value_array) {
$a = array_map(null, array_keys($key_array),
$value_array);
$result = array();
foreach($a as $kv) {
$result[$kv[0]] = $kv[1];
}
return $result;
}
$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc",
8 => "ddd", 9 => "eee", 11 => "fff");
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "",
9 => "", 13 => "");
print_r(transfer_keys($array2, $array1));
if you wanna update all elements of your first array with the values in the array2 in the same order :
$i = 0;
foreach ($array1 as $v) {
while (!isset($array2[$v]))
$i++;
$array2[$i] = $v;
}
But if you want to update according to an order you pre-defined, you have to make something else, like a table defining the rule :
$assoc_array = array(
2 => 4,
3 => 7,
4 => 11,
6 => 5,
5 => 9,
8 => 1);
foreach ($assoc_array as $k => $v) {
$array1[$k] = $array2[$v];
}
Hope this helps.
Aw, you know the difference is always the same !
Then this should be something like :
function updateArray ($array1, $array2, $key_of_array1, $key_associated_of_array2) {
$diff = $key_associated_of_array2 - $key_of_array1;
foreach ($array1 as $k => $v) {
$array2[$k + $diff] = $array1[$k];
}
}
you can get first keys of both arrays using function array_keys();
suppose, $keys1 contains keys of $array1 and $keys2 cotains keys of $array2
Then move into for loop as follows:
for($i=0 ; $i<count($array1) ; $i++)
{
$result[$keys2[$i+1]] = $array1[$i];
}
print_r($result);
Hope this will be helpful to you
foreach($array1 as $item => $value)
if(isset($array2[($item + 2)]) && item != 11)
$temp[$item + 2] = $value;
elseif($item == 11)
$temp[2] = $value;
$array1 = #$temp;
Example code.. but again, you need to tell us the pattern that determines where the first array elements are being inserted into the second array elements. I think it's.. +2? Maybe?

Categories