Find first element in array and place it at the beginning [duplicate] - php

This question already has answers here:
Move an array element to a new index in PHP
(9 answers)
Closed 4 years ago.
Given the following array:
$arr = array ([0] => "apple", [1] => "pineapple", [2] => "fruit");
How can I place "fruit", if exists in the array, to the first index, pushing the others forward?
Expected result:
$arr = array ([0] => "fruit", [1] => "apple", [2] => "pineapple");

Try with array_search() and array_unshift()
<?php
$arr = array ("apple","pineapple","fruit");
$fruit_key = array_search('fruit', $arr);
if($fruit_key){
$fruit_value = $arr[$fruit_key];
unset($arr[$fruit_key]);
array_unshift($arr, $fruit_value);
print_r($arr);
}
?>
Output:
Array (
[0] => fruit
[1] => apple
[2] => pineapple
)
DEMO: https://3v4l.org/bir6i

Related

How to combine multiple arrays into one in PHP [duplicate]

This question already has answers here:
PHP: merge two arrays while keeping keys instead of reindexing?
(6 answers)
Closed 4 years ago.
I have a multiple arrays which I'd like to put into a single array in order to sort it:
$weight = array($weight);
$dev = array_combine($missing, $weight);
echo "<pre>";
print_r($dev);
echo "</pre>";
Output:
Array (
[angular] => 2
)
Array (
[android sdk] => 3
) Array (
[application] => 1
)
Now how do I turn the array above into this?
Array (
[android sdk] => 3
[angular] => 2
[application] => 1 )
I've tried the below from a solution that I've found on this site, but it returns NULL:
$weight = array($weight);
$dev = array_combine($missing, $weight);
$result = call_user_func_array("array_merge", $dev);
echo "<pre>";
print_r($result);
echo "</pre>";
EDIT
Here is my $missing array, some arrays are empty because a match hasn't been found against some keywords:
Array
(
)
Array
(
[0] => angular
)
Array
(
[0] => android sdk
)
Array
(
[0] => application
)
Array
(
)
Here are the value from $weight:
3 2 3 1 3
How can I get this?
Array (
[android sdk] => 3
[angular] => 2
[application] => 1 )
use array_merge:
$array1 = [1,2,3];
$array2 = [4,5,6];
$result = array_merge($array1, $array2);
print_r($result);
results in:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
You can use array_merge function.
Therefore, the code will be
array_merge($array1, $array2);

Multiple arrays merging issue [duplicate]

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 5 years ago.
I have two arrays
First array
array(
[0] => +970
[1] => +971
[2] => +972
)
And Second array
array(
[0] => 465465454
[1] => 321321355
[2] => 987946546
)
I want to merge them like this
array(
[+970] => 465465454
[+971] => 321321355
[+972] => 987946546
)
I try with array_merge but this gives me the result that I didn't want e.g.
$busi_code = $page1_data->business_code; //array
$busi_num = $page1_data->business_number; //array
$business_phone_numbers = array_merge($busi_code, $busi_num);
echo '<pre>';
print_r($business_phone_numbers);
echo '</pre>';
And its result is
[0] => +970
[1] => +971
[2] => +972
[3] => 465465454
[4] => 321321355
[5] => 987946546
So please guide me how can I achieve my required result.
You're looking for array_combine, rather than array_merge:
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
$business_phone_numbers = array_combine($busi_code, $busi_num);
See https://eval.in/954799
This is job for array_combine function:
$business_phone_numbers = array_combine($busi_code, $busi_num);
DOCS: http://php.net/manual/en/function.array-combine.php
You must use array_combine.
Try this:
$a = array(
0 => +970,
1 => +971,
2 => +972);
$b = array(
0 => 465465454,
1 => 321321355,
2 => 987946546);
$r = array_combine($a,$b);
print_r($r);

PHP multi dimensional arrays [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I have two questions.
How can i create an array which i can add two values per index like:
$sample[0] = ("abc",10);
Second is that once i have created this array i would like to sort this array according to the 2nd value at the index.
So if i have an array like:
$sample[0] = ("abc",32);
$sample[1] = ("def",11);
The sorted result should be:
$sample[0] = ("def",11);
$sample[1] = ("abc",32);
Answer to part one:
$sample[0] = array("abc", 10);
Answer to part two:
array_multisort($sample, SORT_NUMERIC);
Testing Environment:
<?php
$sample[0] = array("abc", 32);
$sample[1] = array("def", 11);
print_r($sample);
array_multisort($sample, SORT_NUMERIC);
echo '<br />';
print_r($sample);
?>
Output:
Array ( [0] => Array ( [0] => abc [1] => 32 ) [1] => Array ( [0] => def [1] => 11 ) )
Array ( [0] => Array ( [0] => def [1] => 11 ) [1] => Array ( [0] => abc [1] => 32 ) )
Warning from #Deceze:
Above functionality is coincidental; correct code is:
usort($sample, function ($a, $b) { return $a[1] - $b[1]; })

How to remove value and reassign remaining values to keys in array in PHP [duplicate]

This question already has answers here:
Deleting an element from an array in PHP
(25 answers)
Closed 9 years ago.
The below code,
$test_array = array("a","b","c","d","e");
echo "<fieldset><pre>";
htmlspecialchars(print_r($test_array));
echo "</pre></fieldset>";
which gives output like,
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
)
I want to remove a specific entry say from index 2 and re-index the array as below,
Array
(
[0] => a
[1] => b
[2] => d
[3] => e
)
How to do that?
Use array_splice
array_splice($test_array, 2, 1);
The second argument is your index that you want to nix and the third is how many elements you want gone.
Try this
unset($test_array[2]);
$test_array = array_values($test_array);

Add array to an array with the same indexes not being merged [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Combine Two Arrays with numerical keys without overwriting the old keys
OK guys, was searching about this one with no luck - it always points only to array_merge or array_push or array_combine functions which are useless for my purpose.
Here are two arrays (number indexed):
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
)
Array (
[0] => 25485
[1] => "tyjfhgdfsasdfsdf"
[2] => "mojsbnvgsdfbsdf"
)
and I need to create one "joined" (unioned) array, so it will look like:
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
[3] => 25485
[4] => "tyjfhgdfsasdfsdf"
[5] => "mojsbnvgsdfbsdf"
)
As I found nothing on this problem I tried by myself ($arr1 and $arr2 are the two small arrays):
$result_array = $arr1;
foreach($arr2 as $v) {
$result_array[] = $v;
}
This is, of course, working fine but I don't like this approach - imagine the situation when there will not be just 3 elements in second array...
Question: is there a better approach or at the best some built-in function (I do not know about)???
array_merge will work without any problem as your using numeric keys ... see the explanation below from the docs
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
emphasis mine
Array merge works fine for your numerically indexed arrays:
<?php
$arrayOne = array(
0 => 12345
,1 => "asdvsdfsasdfsdf"
,2 => "sdgvsdfgsdfbsdf"
);
$arrayTwo = array(
0 => 25485
,1 => "tyjfhgdfsasdfsdf"
,2 => "mojsbnvgsdfbsdf"
);
$arrayMerged = array_merge($arrayOne, $arrayTwo);
print_r($arrayMerged);
?>
output:
Array
(
[0] => 12345
[1] => asdvsdfsasdfsdf
[2] => sdgvsdfgsdfbsdf
[3] => 25485
[4] => tyjfhgdfsasdfsdf
[5] => mojsbnvgsdfbsdf
)

Categories