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
Related
My array looks like:
$arr =>
(
[0] => 2575
[1] => 2570
[2] => 0
[3] => 2575
)
And I want to be able to use the array_keys method to search for multiple keys, so I'm trying to get it work using an array as the search value but can't get it to work.
$keys = array_keys($arr, ['2575','2570']);
to return
Array
(
[0] => 0
[1] => 1
[2] => 3
)
Is it possible with array_keys to get the above function to work without calling it twice for each value?
No, but you can do the same with two simple function calls:
$arr = [2575,2570,0,2575];
$search = ['2575','2570'];
$result = array_keys(array_intersect($arr, $search));
Output:
array (size=3)
0 => int 0
1 => int 1
2 => int 3
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);
I have 2 arrays which are in the same form as below; for examples sake, lets call them $array1 and $array2
Array (
[Element1] => Array
(
[id] => 11
[morethings] => 145
[somemore] => namehere
)
[Element2] => Array
(
[id] => 11
[morethings] => 145
[somemore] => namehere
)
[Element3] => Array
(
[id] => 11
[morethings] => 145
[somemore] => namehere
)
)
What I need to do is take Element2 from the first array and then insert it into array2 as NewElement2
I have the following below but it keeps returning nothing at all in array2
$searchArray = array_search('Element2', $array1);
array_splice($array2, $searchArray, 1, array('NewElement2'));
Any help would be greatly appreciated.
for assign the value you can simply
$array2['Element2'] = $array1['Element2'];
(this append or repalce the entry for Elemet2 in $array2)
and for remove the value
unset($array1['Element2']);
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 6 years ago.
How to convert array within array to string, that means I am having a one result set having value of country id but the result set will be in array within array.
Something like below code :
Array
(
[0] => Array
(
[country_id] => 7
)
[1] => Array
(
[country_id] => 8
)
[2] => Array
(
[country_id] => 9
)
[3] => Array
(
[country_id] => 10
)
[4] => Array
(
[country_id] => 1
)
[5] => Array
(
[country_id] => 2
)
[6] => Array
(
[country_id] => 3
)
[7] => Array
(
[country_id] => 4
)
[8] => Array
(
[country_id] => 5
)
)
I want that country list into one string like 7,8,9,10,1,2,3,4,5 but without looping..
Anyone have idea please let me know...?
You can use array_column() and implode() function to do this.
Here is how you can do it,
$values=array_column($array,"country_id");
$country_string=implode($values);
array_column() returns the values from a single column of the input,
identified by the column_key(country_id).
implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between
each element.
implode() can have two arguments, first as glue by which you want to join the elements and second as the array of elements. If first argument is not given then default glue is ",".
Use array_column and implode for (PHP 5 >= 5.5.0, PHP 7) as
$data = array_column($records, 'country_id');
echo implode(",",$data);
try this,
$newarray = array();
foreach ($array as $item) {
$newarray[] = $item['country_id'];
}
echo implode(",",$newarray);
i hope it will be helpful.
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.