PHP get both array value and array key
This should do it
foreach($yourArray as $key => $value) {
//do something with your $key and $value;
echo '<a href="' . $value . '">' . $key . '</a>';
}
Edit: As per Capsule's comment - changed to single quotes.
PHP: Set two arrays into key and value
Do they need to be separate?
You could use array_combine()
to assign a key to the value and then input is just $array[$input]
- e.g.
$arr1 = ['A', 'B', 'C'];
$arr2 = [1, 2, 3];
$arr = array_combine($arr2, $arr1);
echo $arr[$_POST['input']]; # will display 2
Comparing two arrays using both keys and values
array_diff() is there for this purpose. And its ok if the arrays are small, but for optimization, check out this post. It involves flipping the array value and key for faster comparison. And also this other stack comment for a hash table approach.
PHP: How to compare keys in one array with values in another, and return matches?
Update
Check out the answer from Michel: https://stackoverflow.com/a/30841097/2879722. It's a better and easier solution.
Original Answer
If I am understanding this correctly:
Returning a new array:
$array_new = [];
foreach($array_two as $key)
{
if(array_key_exists($key, $array_one))
{
$array_new[$key] = $array_one[$key];
}
}
Stripping from $array_one:
foreach($array_one as $key => $val)
{
if(array_search($key, $array_two) === false)
{
unset($array_one[$key]);
}
}
How to get both array key from value in 2 dimensional array (PHP)
You could probably do something like
function find_index($value) {
foreach ($arr as $index => $index2) {
$exists = array_search($value, $index2);
if ($exists !== false) {
echo "The result is {$index}, {$exists}";
return true;
}
}
return false;
}
how to get difference array using one an array index and another array key value?
Like that
$arr_diff = array_diff_key($first, array_flip($second));
the trick is to array_flip the second array and use array_diff_key
Working example
$first = array(
51581481 => array(
'title' => 'Nike - L',
'price' => '300.00',
'vendor' => 'Vicky Fashion Point',
'quantity' => -23,
),
45747894 => array(
'title' => 'Honor Band A (Black) - Default Title',
'price' => '2249.00',
'vendor' => 'Honor',
'quantity' => 8,
),
);
$second = array(
0 => 45747894,
1 => 713776113,
);
var_dump(array_diff_key($first, array_flip($second)));
php combine two array's subarrays where value of certain key is equal
Try this:
foreach($arr_b as $b_item) {
foreach($arr_a as $key => &$a_item) {
if ($b_item['usrsID'] == $a_item['usrsID']) {
$a_item['id'] = $b_item['usrsID'];
$a_item['avatarID'] = $b_item['avatarID'];
}
}
}
Your output of $_arr_a
will be:
Array
(
[0] => Array
(
[ID] => 5
[usrsID] => 3
[tid] => 19
[txtid] => 22
[id] => 3
[avatarID] => 22
)
[1] => Array
(
[ID] => 6
[usrsID] => 1
[tid] => 19
[txtid] => 23
[id] => 1
[avatarID] => 1
)
[2] => Array
(
[ID] => 7
[usrsID] => 2
[tid] => 19
[txtid] => 24
[id] => 2
[avatarID] => 3
)
[3] => Array
(
[ID] => 8
[usrsID] => 1
[tid] => 19
[txtid] => 25
[id] => 1
[avatarID] => 1
)
)
how to combine two arrays with the same array key then if the value is the same then it is not displayed and if it is different it will be displayed
If I got your description correctly, then all you need to do is loop over your first array, check if an item with the same index exists in your second array (not clear from your explanation, whether those will always have the exact same number of items or not), and if so, add it to the result array using the value from the first array as key:
$result = [];
foreach($naflr as $index => $value) {
if(isset($fuzifikasi[$index])) {
$result[$value][] = $fuzifikasi[$index];
}
}
var_dump($result);
https://3v4l.org/L34El
Related Topics
Regex/ Code to Fix Corrupt Serialized PHP Data
Get Last Modified File in a Directory
Iterable Objects and Array Type Hinting
PHP Exec - Check If Enabled or Disabled
Symfony 2 - How to Pass Data to Formbuilder
Session Unset, or Session_Destroy
How to Rotate Image and Save the Image
In PHP Can Someone Explain Cloning VS Pointer Reference
Get Variable from PHP File Using Jquery/Ajax
Explode() into $Key=>$Value Pair
How to Replace Double Quotes with Single Quotes
Get Timestamp of Today and Yesterday in PHP
Twig Iterate Over Object Properties
Bind Param with Array of Parameters
How to Send a Image to a PHP File Using Swift