Get subset of array based on values in PHP - php

I have an array like the below one
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 2
[4] => 2
[5] => 3
[6] => 3
[7] => 3
[8] => 4
[9] => 4
[10] => 4
)
Is there any way to get a subset of this array using the values? So that, if need the subset of value 1, then it has to display
Array
(
[0] => 1
[1] => 1
[2] => 1
)
And if subset of 2 then
Array
(
[3] => 2
[4] => 2
)
And so on.. I need to preserve the index too.
I searched so many places. But didn't get an answer for this. I wish to avoid the multiple looping for this.

You can use array_filter to filter an array down to only the elements you're looking for. Set $subset to whatever value you're searching for, and it'll return the matching elements, without changing the keys.
$subset = 2;
$results = array_filter($array, function ($item) use ($subset) {
return $item === $subset;
});
print_r($results);
Array
(
[3] => 2
[4] => 2
)
See https://eval.in/926983

Try array_chunk
array_chunk($array,4,true);
First parameter array
Second parameter size
Third parameter preserve keys

Related

in_array() not working with two dimensional associative array?

I'm trying very simply to use in_array() to check a key is in an array and then echo its value.
$array = Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
And I have value 556729685 which I want to check that this value exists or not? So I am using in_array() function for this.
in_array(556729685, array_keys($array));
in_array(556729685, array_values($array));
in_array(556729685, $array);
All above three i have used but result always showing NULL means blank.
I am really frustrated to find the solution. I don't understand what's happening.
You should use array_column() which will return the values from a single column in the input array as an array.
$product_auto_ids = array_column($array['cart_item'], 'product_auto_id');
In this case, it would return the following:
Array
(
[0] => 556729685
[1] => 951790801
)
Then you can use in_array() like you currently are.
in_array(556729685, $product_auto_ids);

Move the last element in the node (of a multidimensional php array) at the beginning?

Hello guys i would to move the last element in the node (of a multidimensional php array) at the beginning...
This is my array:
$arr= array(
array(2,3,4,5,1),
array(3,4,5,6,2),
array(4,5,6,7,3)
);
and this is the output that i would to have:
Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[1] => Array (
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
[2] => Array (
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
)
I created this code that works fine
$number= count($arr);
for($a= 0; $a < $number; $a++){
$element= $arr[$a][4];
unset($arr[$a][4]);
array_unshift($arr[$a],$element);
}
but i would know if there are other faster and efficient methods...
Thanks a lot and sorry for my english :)
Given the input you provided and the output, this is quicker to type:
array_map('sort', $arr);
Whether it runs faster or gives the desired result - I have no idea.
A correct answer that doesn't depend on the sort order of the values inside the array.
foreach($array as &$element) {
array_unshift($element,array_pop($element));
}

How to remove empty values from the multi dimensional array

I have array structure like this,
Array
(
[1] => Array
(
[1] =>
[2] =>
[3] =>
[4] => Product
[5] => Product Name
..
[59] => Color
)
[2] => Array
(
[1] =>
[2] =>
[3] => 1
[4] => 9155
....
[59] =>
)
[3] => Array
(
[1] =>
[2] =>
[3] => 1
[4] => 9165
...
[59] => Green
)
[4] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
...
[58] =>
[59] =>
)
)
Its reading data from Excel sheets , the issue is when i read data from excel sheet it reads empty rows too, I already tried to ignore empty rows from the excel sheet some how its working (when the excel is created from MSexcel ) but from Google Drive its reading empty rows. So I would like to remove those rows with 1- 59 are blanks. in the above example array with index 4 .
Note that some index have missing values in many sub index but I don't want to remove those, only all sub indexes from 1-59 are blank then that main index (here its 4) needs to remove.
Is there any smart way to remove those array index that have empty values. I not like to iterate all the array and store to another.
if you want to remove the index 4 that is an empty array :
array_filter(array_map('array_filter', $array));
Use array_map
$array = array_map('array_filter', $array);
let try with array_filter
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r(array_filter($entry));
Array
(
[0] => foo
[2] => -1
)
Use array_filter..It will remove all empty values..
array_filter($array);

PHP - count frequency of array values

Is there a way in php to count how often a value exists in a large array?
So if I have an array like this:
$array = "1,2,3,4,joe,1,2,3,joe,joe,4,5,1,6,7,8,9,joe";
is there a way to output a new array that tells me (and sorts) which is used most and how many for each?
$result = array(
[joe] => 4
[1] => 3
[2] =>2
etc...
)
I've seen the php array_count_values, but can this be sorted by most -> least? or is there an easier way?
Thanks everyone!
Sort them after counting them with arsort()
$result = array_count_values(explode(',', $array));
arsort($result);
Array
(
[joe] => 4
[1] => 3
[2] => 2
[4] => 2
[3] => 2
[9] => 1
[8] => 1
[5] => 1
[6] => 1
[7] => 1
)

multi dimensional array in random order

I want to make it so that my multi dimensional array is in a random order. How would you do it?
// This is how the array looks like
print_r($slides);
Array
(
[0] => Array
(
[id] => 7
[status] => 1
[sortorder] => 0
[title] => Pants
)
[1] => Array
(
[id] => 8
[status] => 1
[sortorder] => 0
[title] => Jewels
)
[2] => Array
(
[id] => 9
[status] => 1
[sortorder] => 0
[title] => Birdhouse
)
[3] => Array
(
[id] => 10
[status] => 1
[sortorder] => 0
[title] => Shirt
)
[4] => Array
(
[id] => 11
[status] => 1
[sortorder] => 0
[title] => Phone
)
)
// This how the result is if I use array_rand()
print_r(array_rand($slides, 5));
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
// This how the result is if I use shuffle()
print_r(shuffle($slides));
1
shuffle() is the way to go here. It prints 1 because shuffle changes the array in-place and returns a boolean, as it is written in the documentation:
Returns TRUE on success or FALSE on failure.
I suggest to also read the documentation of array_rand():
Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.
Always read documentation if you use built-in functions. Don't just assume how the work. I bet it took more time to write the question than looking this up.
Instead of
print_r(shuffle($slides));
do
shuffle($slides);
print_r($slides);
You see shuffle() shuffles the array in-place
i am not sure how you want it to display but you can loop the array and use php rand(0,arraylen) function to parse the array.
It works perfect. print_r(shuffle($slides))) gives the output of TRUE, since the return value of shuffle is a boolean and not an array.
See the working example here: http://codepad.org/B5SlcjGf

Categories