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
Related
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:
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
I know this won't take much time for experts here. But still please help me out
My Array output is like this
Array ( [0] => 1 [1] => 37 [2] => 1035 ) 1
Array ( [0] => 1 [1] => 37 [2] => 1035 ) mystatusmessage1
Array ( [0] => 4 [1] => 37 [2] => 2925 ) 2
Array ( [0] => 4 [1] => 37 [2] => 2925 ) mystatusmessage2
What I would like to get it is in a single string value like this so that I can insert into database.
1,37,1035,1,mystatusmessage1
4,37,2925,2,mystatusmessage2
How can I achieve that. I'm trying to do with foreach but still I'm not able to do it.
Thanks,
Kimz
use implode function to make string from array for example
if you have array like Array('a','b','c');
implode(',',array('a','b','c') )
will return a,b,c as string
here first argument is your glue by which you want to join string
Here you go.
// Original array
$array = array(0 => 1, 1 => 37, 2 => 1035);
// $_POST array
$_POST = array(1,'mystatusmessage1');
// Jump to the end of array
end($array);
// Merge the post with original array
$newArr = array_merge($array,$_POST);
// Impode
echo implode(",",$newArr);
Repeat with other array.
my array is
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
)
start value is = 3
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 0
[5] => 1
[6] => 2
)
any idea friends..
try
$arr = array(0,1,2,3,4,5,6);
foreach($arr as $k=>$v) {
if($v >= 3)
$a[] = $v;
else
$b[] = $v;
}
$c = array_merge($a, $b);
print_r($c); //Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 0 [5] => 1 [6] => 2 )
Step 0 (optional if needed):
order the array (heap or some other sort algorithm)
Step 1:
create an output array
Step 2:
loop trough the array and find your start point move the value and the ones after to new array and set the value in the original array to null
Step 3:
loop trough the array and move the remaining values over
DONE
Just do this:
Call asort on the array.
Call array_search to find the index you want to slice.
Call array_slice to cut off the part you want.
Call array_merge to merge them in the right order.
I don't know PHP that well but a quick look at php.net and I suspect this will work.
Seeing how your array is already sorted and 0-indexed, you can just slice and reorganise it:
array_values(array_slice($arr, 3, null, true) + array_slice($arr, 0, 3, true));
I have an multidimensional array:
Array
(
[0] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Other Cereals
[IName] =>
[Date] => 2013-02-05
)
[1] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Porridge
[IName] => Oats,Milk,Sugar
[Date] => 2013-02-06
)
[2] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Porridge
[IName] => Oats,Milk,Sugar,Oats,Milk,Sugar
[Date] => 2013-02-05
)
)
And I am trying to use array unique to filter this
[IName] => Oats,Milk,Sugar,Oats,Milk,Sugar
I am having no luck. How can I filter the duplicates?
Cheers.
If you filter input and therefore don't have extra spaces in IName field, you can use something as simple as this for filtering:
$array[2]['IName'] = implode(',', array_unique(explode(',', $array[2]['IName'])));
The problem is that you habe in array two Oats,Milk,Sugar as element of IName, in array three you have Oats,Milk,Sugar,Oats,Milk,Sugar. This is not the same!
"Oats,Milk,Sugar"=="Oats,Milk,Sugar,Oats,Milk,Sugar" (or "Oats,Milk,Sugar".equals("Oats,Milk,Sugar,Oats,Milk,Sugar")) is false.
If you want to have it unique you have to explode the single results and then do a unique on it or you have to store the single values in seperate fields...
BTW: Here is a link how to remove duplicates from a multi dimensional array How to remove duplicate values from a multi-dimensional array in PHP
I am not sure if a function exists for that, here is a simple solution,
you can loop the array, and get the result of each value, then explode result, and insert it into an array.
then use the array_unique function.
try this:
$result = array();
foreach($arrays as $array)
{
$tmp = $array['IName'];
$tmp2 = explode(',',$tmp);
foreach ($tmp2 as $t)
{
$result[]=$t;
}
}
$array_unique = array_unique($result);