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);
Related
This question already has answers here:
PHP array replace numbers with keys
(2 answers)
Closed 6 years ago.
When doing a print_r on my array, I get the following output;
Array
(
[0] => Array
(
[id] => 178
[name] => Briar Price
)
[1] => Array
(
[id] => 90
[name] => Bradley Kramer
)
[2] => Array
(
[id] => 508
[name] => Calvin Yang
)
[3] => Array
(
[id] => 457
[name] => Charles Valenzuela
)
... and so on
How can I modify the array to look like this;
Array
(
[178] => Briar Price
[90] => Bradley Kramer
[508] => Calvin Yang
[457] => Charles Valenzuela
... and so on
)
I just want to make the ID the key for the value name. I always have issues when it comes to arrays reordering.
Pass third parameter to array_column to make its key as
$array = array_column($users, 'name', 'id');
print_r($array);
Use foreach() -
$newArr = array();
foreach ($your_array as $key => $val) {
$newArr[$val['id']] = $val['name'];
}
print_r($newArr) // desired output
I'd use array_combine which attaches new keys to values
$results = [
['id'=>1,'name'=>'John'],['id'=>2,'name'=>'Jane'],
];
$results = array_combine(
array_column($results,'id'), //use 'id' column as keys
array_column($results,'name') //use 'name' column as values
);
//now $results is [1=>'John', 2=>'Jane']
You can do it using array_column and array_combine PHP function without applying custom logic.
Here is how you do it,
<?php
$keys=array_column($mainarray,'id');
$values=array_column($mainarray,'name');
$finalarray=array_combine($keys,$values);
$finalarray will be your desired result.
array_combine creates an array by using one array for keys and another for its values.
array_column returns the values from a single column in the input array.
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 years ago.
I have an multidimensional array and want to sort in according to date and also want to get only 5 data from an array.
Array :
Array
(
[0] => Array
(
[ID] => 1
[TITLE] => example1
[DATE] => 2016-05-17
[PST_BY] => 0
[IMG_NM] =>
[SLUG] =>
[NAME] => Web Design & Development
)
[1] => Array
(
[ID] => 2
[TITLE] => example2
[DATE] => 2016-05-20
[PST_BY] => 0
[IMG_NM] =>
[SLUG] =>
[NAME] => Mobile OS
)
)
I am doing this but not working :
$list = array_sort($blog, 'DATE', SORT_ASC);
print_r($list);
Example to sort on a specific key (in this case name):
// Function to compare two items in the array
function CompareName($left, $right) {
return $left['name'] > $right['name'];
}
// Example array/data
$myarray = [
["id"=>1, "name"=>"foo"],
["id"=>2, "name"=>"bar"],
["id"=>3, "name"=>"ah"],
["id"=>4, "name"=>"zoo"]
];
echo 'Unsorted:';
var_dump($myarray);
usort($myarray , 'CompareName');
echo 'Sorted:';
var_dump($myarray);
want to get only 5 data from an array
$top5 = array_slice($myarray, 0, 5);
or:
$top5 = array_splice($myarray, 0, 5);
This question already has answers here:
Print the keys of an array
(8 answers)
Closed 10 months ago.
Here is the array:
$campaigns =
Array (
[252] =>Array (
[campaign_type_id] => 9
[company] => Array (
[company_name] => facebook
[company_type] => 2
)
[campaign] => Array(
[pitch_id] => 27
[pitch_campaign_title] => facebook mandate
[pitch_campaign_description] => desc face
[pitch_campaign_image] => db.png
)
[title] => Accelarator
[selection] => Array (
[0] => Array(
[ca_mandate_id] => 96
[ca_id] => 252
[ca_company_id] => 1
[ca_updated] => 2015-12-31 12:37:50
)
)
[campaign_created_by] => 3
[userinfo] => Array (
[0] => Array (
[user_id] => 3
[user_first_name] => CoLabs
[user_last_name] => Accelerator
[user_img] => index2.jpg
[user_designation_name] => Investor
[user_company_id] => 123
)
)
)
)
how can I get the value '252'? Its a dynamic value.
I want to get whatever value is stored in place of 252.
Please help?
Thanks in advance.
Use the array_keys function: http://php.net/array_keys
array_keys — Return all the keys or a subset of the keys of an array
Description array array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] )
For your code:
$keys = array_keys($campaigns);
Try array_keys
array_keys() returns the keys, numeric and string, from the array.
$req_key = array_keys($campaigns);
You can use array_keys() (as other suggested) and key() functions for getting this result.
Difference is that:
If you want to use array_keys() function it will returns you an array that consist of all keys.
If you want to use key() function it will returns you the first index of array. (you can get all keys by using loop);
Example 1 (With array_keys):
$arry = array(1,2,3);
echo "<pre>";
print_r(array_keys($arry));
Result:
Array
(
[0] => 0
[1] => 1
[2] => 2
)
Example 2 (With key):
$arry = array(1,2,3);
echo key($arry);
Result:
0 // index
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Merging PHP array, one as Keys the other as Values?
I have the following two arrays in PHP, which represent atmospheric gases and their compositions:
Array
(
[0] => nitrogen
[1] => argon
[2] => oxygen
[3] => carbon dioxide
)
Array
(
[0] => 78
[1] => 1
[2] => 21
[3] => 0
)
Is there an elegant way of obtaining the following array, a combination of keys and values:
Array
(
"nitrogen" => 78
"argon" => 1
"oxygen" => 21
"carbon dioxide" => 0
)
The methods I can think of involve loops, I don't know if I'm complicating it. Is there a simple way?
array_combine(), simply:
$new_arr = array_combine($keys,$values);
use array_combine
$key = array('nitrogen', 'argon', 'oxygen', 'carbon dioxide');
$values = array(78, 1, 21, 0 );
$return = array_combine($key, $values);
print_r($return);
output:
Array
(
[nitrogen] => 78
[argon] => 1
[oxygen] => 21
[carbon dioxide] => 0
)
array_combine() Example:
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
[a] => 2
[b] => 3
)
If two keys are the same, the second one prevails.
Believe it or not - array_combine() exists and is exactly what you are looking for!
http://php.net/manual/en/function.array-combine.php
array_combine — Creates an array by using one array for keys and
another for its values
Example adapted from the above link -
$a = array('Stack', 'Server', 'Super');
$b = array('Overflow', 'Fault', 'User');
$c = array_combine($a, $b);
print_r($c);
Array
(
[Stack] => Overflow
[Server] => Fault
[Super] => User
)
And there you have it - an array fit for a trilogy.
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
)