in_array with single and multidimension - php

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]) {
}

Related

Why cant slice or something like that of my arrays with php

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

multidimensional array to indexed array in php

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.

php remove duplicates based on first value of multidimensional array

Given
[0] => Array
(
[0] => ask.com
[1] => 2320476
)
[1] => Array
(
[0] => amazon.com
[1] => 1834593
)
[2] => Array
(
[0] => ask.com
[1] => 1127456
)
I need to remove duplicate values solely based on first value, regardless of what any other subsequent values may be. Notice [0][1] differs from [2][1] yet I consider this as a duplicate because there are two matching first values. The other data is irrelevant and shouldn't be considered in comparison.
Try this, assuming that $mainArray is the array you have.
$outputArray = array(); // The results will be loaded into this array.
$keysArray = array(); // The list of keys will be added here.
foreach ($mainArray as $innerArray) { // Iterate through your array.
if (!in_array($innerArray[0], $keysArray)) { // Check to see if this is a key that's already been used before.
$keysArray[] = $innerArray[0]; // If the key hasn't been used before, add it into the list of keys.
$outputArray[] = $innerArray; // Add the inner array into the output.
}
}
print_r($outputArray);

php - multidimensional array regex search and print out partial array to look at structure

I have this array structure, it is stored in a variable $xxx
Array
(
[xyz] => Array
(
[1] => 3
[0] => s
)
[d2s] => Array
(
[a] => 96
[d] => 4
)
...
)
It is a long array, and I don't want to out put the whole thing, how do print only the first 5 (1st dimension) values along with the 2nd dimension values?
Secondly, if I want this array to contain only alphabets in the FIRST dimension, how do I either delete values that don't match that requirement or retain values that match the requirement? so that my final array would be
Array
(
[xyz] => Array
(
[1] => 3
[0] => s
)
...
)
TIA
To output only the first 5 elements, use array_slice:
array_slice($arr, 0, 5)
To remove any elements whose index contains non-alpha characters.
foreach ($arr AS $index => $value) {
// Remove the element if the index contains non-alpha characters
if (preg_match('/[^A-Za-z]/', $index))
unset($arr[$index]);
}
Check it out in action.

checking to see if a vaule is in a particular key of an array

I'm new to working with arrays so I need some help. With getting just one vaule from an array. I have an original array that looks like this:
$array1= Array(
[0] => 1_31
[1] => 1_65
[2] => 29_885...)
What I'm trying to do is seach for and return just the value after the underscore. I've figured out how to get that data into a second array and return the vaules as a new array.
foreach($array1 as $key => $value){
$id = explode('_',$value);
}
which gives me:
Array ( [0] => 1 [1] => 31 )
Array ( [0] => 1 [1] => 65 )
Array ( [0] => 29 [1] => 885 )
I can also get a list of the id's or part after the underscore by using $id[1] I'm just not sure if this is the best way and if it is how to do a search. I've tried using in_array() but that searches the whole array and I couldn't make it just search one key of the array.
Any help would be great.
If the part after underscore is unique, make it a key for new array:
$newArray = array();
foreach($array1 as $key => $value){
list($v,$k) = explode('_',$value);
$newArray[$k] = $v;
}
So you can check for key existence with isset($newArray[$mykey]), which will be more efficient.
You can use preg_grep() to grep an array:
$array1= array("1_31", "1_65", "29_885");
$num = 65;
print_r(preg_grep("/^\d+_$num$/", $array1));
Outputs:
Array
(
[1] => 1_65
)
See http://ideone.com/3Fgr8
I would say you're doing it just about as well as anyone else would.
EDIT
Alternate method:
$array1 = array_map(create_function('$a','$_ = explode("_",$a); return $_[1];'),$array1);
echo in_array(3,$array1) ? "yes" : "no"; // 3 being the example
I would have to agree. If you wish to see is a value exists in an array however just use the 'array_key_exists' function, if it returns true use the value for whatever.

Categories