I want to clear same values in array.
For example so be my array:
array(0=>"1",1=>"1",2=>"3",3=>"1",4=>"6");
I want ot get as:
array(0=>"1",1=>"3",2=>"6");
How?
<?php
$input = array(0=>"1",1=>"1",2=>"3",3=>"1",4=>"6");
$result = array_values(array_unique($input));
print_r($result);
?>
array_unique
Array
(
[0] => 1
[2] => 3
[4] => 6
)
array_values with array_unique
Array
(
[0] => 1
[1] => 3
[2] => 6
)
With a combination of array_unique [docs] and array_values [docs]:
$array = array_values(array_unique($array));
I believe you want the array_unique() function ( http://php.net/manual/en/function.array-unique.php ):
$arr = array_unique(array(0 => '1', 1 => '1', 2 => '2'));
will return:
array(0=> '1', 2 => '2')
I believe you can use array_slice for this purpose. Then edit the keys' values manually
Edit: To remove a key/value pair, call the unset() function on it. from here under Creating/modifying with square bracket syntax section.
Related
I have lot arrays and i want to only print number of 2 and first arrays
i use array_slice but there is still a problem
Arrays:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Array
(
[0] => 571
[1] => Roods
)
I need to like this:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Basically you seem to only need:
array_slice(array_unique(array_column($Myarrays, 'nidtitle')), 0, 2);
This should be done instead of the entire code you use to generate the arrays.
Short explanation:
array_column will get the element nidtitle from each "row" (array entry) in $Myarrays
After that we run that column through a unique function
Then we get the first 2 elements with an array_slice
This should do the job:
$finalarray = array_slice($Myarray, 0, 2);
print_r($finalarray);
You can use array_slice to get the items you want.
$arr = array(Array(441,"Awesome"),
Array(570,"Noons"),
Array(571,"Roods"));
$two = array_slice($arr, 0,2);
Var_dump($two);
Array_slice second parameter is where the slice should start.
Third parameter is count of values to slice.
https://3v4l.org/NPcJT
I've following array called $user_id_arr
Array
(
[0] => 92ecd33db4ddcdc28e025cae80f00208
[1] => 9def02e6337b888d6dbe5617a172c18d
[2] => a6d22e4cc3f65778a60b359842bcec82
)
Now I want a string containing above array values but each array value should be enclosed in single quotes. For it I tried following code but not succeed.
$user_ids = implode(",", $user_id_arr);
I'm getting following array:
92ecd33db4ddcdc28e025cae80f00208,9def02e6337b888d6dbe5617a172c18d,a6d22e4cc3f65778a60b359842bcec82
Can anyone please help me in this regard please? I want the desired array in following format:
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
Thanks in advance.
You can use the implode as the above answers suggested. If you directly want to modify the array then you could make use of array_walk().
<?php
$arr=Array
(
0 => '92ecd33db4ddcdc28e025cae80f00208',
1 => '9def02e6337b888d6dbe5617a172c18d',
2 => 'a6d22e4cc3f65778a60b359842bcec82',
);
array_walk($arr,function (&$val){ $val="'".$val."'";});
echo implode(',',$arr); //<----- This is the one you wanted by the way...
print_r($arr);
OUTPUT :
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
Array
(
[0] => '92ecd33db4ddcdc28e025cae80f00208'
[1] => '9def02e6337b888d6dbe5617a172c18d'
[2] => 'a6d22e4cc3f65778a60b359842bcec82'
)
$user_id_arr = array
(
'0' => '92ecd33db4ddcdc28e025cae80f00208',
'1' => '9def02e6337b888d6dbe5617a172c18d',
'2' => 'a6d22e4cc3f65778a60b359842bcec82'
);
$user_ids = "'". implode("','",$user_id_arr)."'";
echo $user_ids;
Try
$user_ids = "'".implode("','", $arr)."'";
See demo
foreach($user_id_arr as $r){
$user_ids = $user_ids."'".$r."',";
}
I have an array in PHP that looks like
Array ( [123654] => Array ( [0] => 123456789123456789 [1] => 1 [2] => 06/24/2011 [3] => 06/24/2012 [4] => 12355.44 [5] => 55321.55 ) )
I know in javascript I could access the data I need by doing array[0][0], how would I go about doing this in PHP. It is the 123456789123456789 value that I'm looking at getting.
Try this
array_slice($array, 0, 1);
http://php.net/array_slice
If you don't know the exact keys, you could do something like this:
$a = array_values($my_array);
$b = array_values($a[0]);
echo $b[0];
array_values replaces the keys by simple numbers from 0 to n-1 (where n is the count of values), by that you can access your desired value with the indexes [0][0]. See more here
http://codepad.org/YXu6884R
Here you go. See above for proof. The methodology from #azat is not explicit enough and is prone to risk if the elements of the array or sub array are re-arranged or if the key value for the super array changes.
$my_array = array( 123654 => array( 0 => '123456789123456789', 1 => '1', 2 => '06/24/2011', 3 => '06/24/2012', 4 => '12355.44', 5 => '55321.55' ) );
echo $my_array['123654'][0];
Try
$first = array_shift(array_values($array));
http://php.net/manual/en/function.array-shift.php
I have an array like this:
$categories_array = array(
[0] => 'category_1',
[1] => 'category_2',
[2] => 'category_3',
[3] => 'category_4'
)
I'd like to "filter" the array to get a new one. For example, I'd like to have a new array with only 'category_2' and 'category_3' like this:
$new_categories_array = array(
[1] => 'category_2',
[2] => 'category_3',
)
How can I accomplish this result?
unset($new_categories_array[0]);
unset($new_categories_array[3]);
..might do the trick
See
array_diff — Computes the difference of arrays
array_intersect — Computes the intersection of arrays
Example:
$original = array('category_1','category_2','category_3','category_4');
$new = array_diff($original, array('category_1', 'category_4'));
print_r($new);
Output:
Array
(
[1] => category_2
[2] => category_3
)
When using array_intersect the returned array would contain cat 1 and 4 obviously.
Use preg_grep:
$new_categories_array = preg_grep('/category_[23]/', $categories_array);
While I agree preg_grep is a good solution in your example, if you want a more general case function, look at array_filter - http://ca.php.net/manual/en/function.array-filter.php
I have an array with some value like this:
[Organization_id] => Array
(
[0] => 4
[1] => 4
[2] => 4
)
but i want some thing like this:
[Organization_id] => Array
(
[0] => 4
)
Thanks in advance..
If you don't care about the key to value association possibly messing up, you can use this:
$array = array_unique($array);
Although array_unique was mentioned twice now, I feel the answers failed to point out that you have to use the function on the nested array and not the array itself, so here is a usage example
$array = array( 'Organization_id' => array(4,4,4) );
$array['Organization_id'] = array_unique( $array['Organization_id'] );
print_r($array);
which will do what you want.