i have this multidimensional array.
Array
(
[0] => Array
(
[car] => Toyota
)
[1] => Array
(
[car] => Ford
)
[2] => Array
(
[car] => Isuzu
)
[3] => Array
(
[car] => Chevrolet
)
)
i want to put them into indexed array like this..
Array = ("Toyota", "Ford", "Isuzu", "Chevrolet");
$result = array_map(function($item)
{ return $item['car']; }, $array);
Some functional approach. array_map
P.S. PHP has no indexed arrays
foreach($yourArray as $carArray)
{
$result[]=$carArray["car"];
}
And your understanding of indexed arrays is wrong. That example output you've shown contains all those values, not indexes, and since you didn't specify an index it will start from 0 and so on.
Option using array_map(), assuming your initial array is $arr
$new_arr = array_map(function($x){return $x['car'];}, $arr);
See demo
try using foreach and array_values, then save it into empty array. Hope it helps.
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
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
Array
(
[0] => 5
[1] => 4
[2] => 1
[3] => 2
)
Expecting result
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
$results = [];
foreach($array as $key=>$value){
$results[$key] = arsort($value);
}
echo "<pre>";
print_r($results);
Please suggest how do we can sort associative array i did try but does not work for me please guide
As per your "expected results' it seems like you don't wish to maintain the keys. If that's the case then you can just use sort.
Something like this..
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
sort($array);
print_r($array);
Just do
sort($array);
Also check the PHP documentation if you need further customization: http://php.net/manual/en/function.sort.php
var_dump( array_reverse($array,false));
you don't need to use foreach or sort ,you can just use array_reverseinstead ,avery simple way
You don't need to iterate using foreach for sorting
Just use sort for sorting array
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
sort($array);
print_r($array);
This will not maintain array keys and if you want arrays keys to be same just replace sort with asort in above code
I have two arrays a single dimension and multidim array ,the multidimension its comma
separated
$singledim =Array
(
[0] => 333
[1] => 673
[2] => 434
[3] => 67
)
$multidim = Array
(
[0] => Array
(
[0] => 22
[1] => 3336,673,34,342,432,23,323,434,765675765,7657567
)
[1] => Array
(
[0] => 24
[1] => 2424,10
)
[2] => Array
(
[0] => 28
[1] => 23,12,13,14,15,16
)
............
}
I want to use in_array to check the single dimension array value exists .Belwo is the one i tried..
<?
foreach($multidim as $multi)
{
if(in_array($singledim,$multi[1])
{
}
$i++;
}
?>
foreach($multidim as $multi){
foreach($singledim as $single){
$temp_array = explode(',',$mutli[1]);
if(in_array($single, $temp_array)){
// do stuff
}
}
}
If you pass an array, that same array group must exist exactly in the same manner in the haystack in order to match.
You don't want in_array(). you want strpos(...) !== false. and note you WILL be subject to false positives. e.g. if you're searching for 1, then your 12, 21, etc... will false match. Your structure needs to be normalized, and each value individual value in the [1] sub-element should be its OWN array element.
$multi[1] is not an array. It is a comma separated string.
You can use explode to create an array from the string:
$vals = explode(',' ,$multi[1]);
if(in_array($singledim, $vals)
{
}
However, that will only work if $singledim is a string.
As noted in the comments, you are checking if a whole array is the same as the string in the second array. You could convert the first array to a string and then check they are equal:
$singleDimStr = implode(',' ,$singledim);
if($singleDimStr == $multi[1]) {
}
I have two arrays, one is generated by using explode() on a comma separated string and the other is generated from result_array() in Codeigniter.
The results when doing print_r are:
From explode():
Array
(
[0] => keyword
[1] => test
)
From database:
Array
(
[0] => Array
(
[name] => keyword
)
[1] => Array
(
[name] => test
)
)
I need them to match up so I can use array_diff(), what's the best way to get them to match? Is there something other than result_array() in CI to get a compatible array?
You could create a new array like this:
foreach($fromDatabase as $x)
{
$arr[] = $x['name'];
}
Now, you will have two one dim arrays and you can run array_dif.
$new_array = array();
foreach ($array1 as $line) {
$new_array[] = array('name' => $line);
}
print_r($new_array);
That should work for you.
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