Shuffle array losing data - php

I have an array and when I check this with print_r the output is:
Array ( [0] => metaalboutique.jpg [1] => asc.jpg [2] => thure.jpg [3] => stegge.jpg [4] => aws.jpg [5] => rsw.jpg [6] => pmm.jpg )
I want the export to be shuffled so I use shuffle() but when I check the output with print_r now I only see 1 as output.
$portfolio = array
(
'thure.jpg',
'rsw.jpg',
'pmm.jpg',
'asc.jpg',
'stegge.jpg',
'metaalboutique.jpg',
'aws.jpg'
);
$shuffled_portfolio = shuffle($portfolio);
print_r($portfolio);
print_r($shuffled_portfolio);

shuffle shuffles an array in place and returns a boolean to indicate if the shuffling succeeded (TRUE) or not (FALSE):
$portfolio = array
(
'thure.jpg',
'rsw.jpg',
'pmm.jpg',
'asc.jpg',
'stegge.jpg',
'metaalboutique.jpg',
'aws.jpg'
);
print_r($portfolio);
$success = shuffle($portfolio);
if ($success) {
# $portfolio is now shuffled
print_r($portfolio);
}

PHP shuffle function returns boolean value.
shuffle — Shuffle an array
bool shuffle ( array &$array )
&$array - the & sign means you are passing a reference of an array in that function.
Return Values
Returns TRUE (1) on success or FALSE(0) on failure.

Related

Slice an array in php

I have an array
print_r($myarray);
Array ( [0] => text one ) Array ( [0] => text two ) Array ( [0] => text three ) ...
When i use slice
print_r(array_slice($myarray,1,2));
Array ( ) Array ( ) Array ( ) Array ( ) ......
I get an empty array, how can i slice this array in half?
Thanks
You actually have an array that contains arrays which probably might be what is causing the issue for you. Though I do not see how you get the result you posted... It mighty be that you actually apply the slice function to each element of the output array. Then certainly you will get an empty array for each iteration. As to be expected, since each element you iterate over contains only a single element. So slicing from position 1 will result in an empty array each time...
Consider this simple example using a plain array:
<?php
$input = ["one", "two", "three", "four", "five", "six"];
$output = array_slice($input, 1, 2);
print_r($output);
The output is:
Array
(
[0] => two
[1] => three
)
So php's array_slice() function works just as expected...
Same with an array of arrays as you suggest in your post:
<?php
$input = [["one"], ["two"], ["three"], ["four"], ["five"], ["six"]];
$output = array_slice($input, 1, 2);
print_r($output);
The output of that is, as expected:
Array
(
[0] => Array
(
[0] => two
)
[1] => Array
(
[0] => three
)
)
Also considering your second comment below, that you have some words in a single string (which mighty be what you describe) I get a meaningful result myself:
<?php
$input = explode(' ', "one two three four five six");
$output = array_slice($input, 1, 2);
print_r($output);
The output, as expected, is:
Array
(
[0] => two
[1] => three
)

PHP Array: array filter with argument

I have this simple array in PHP that I need to filter based on an array of tags matching those in the array.
Array
(
[0] => stdClass Object
(
[name] => Introduction
[id] => 798162f0-d779-46b6-96cb-ede246bf4f3f
[tags] => Array
(
[0] => client corp
[1] => version 2
)
)
[1] => stdClass Object
(
[name] => Chapter one
[id] => 761e1909-34b3-4733-aab6-ebef26d3fcb9
[tags] => Array
(
[0] => pro feature
)
)
)
When supplied with 'client corp', the output should be the above array with only the first item.
So far I have this:
$selectedTree = array_filter($tree,"checkForTags");
function checkForTags($var){
$arr = $var->tags;
$test = in_array("client corp", $arr, true);
return ($test);
}
However, the result is that it's not filtering. When I echo $test, I get 1 all the time. What am I doing wrong?
Something like this should do the trick:
$selectedTree = array_filter(array_map("checkForTags", $tree ,array_fill(0, count($tree), 'client corp')));
function checkForTags($var, $exclude){
$arr = $var->tags;
$test = in_array($exclude, $arr, true);
return ($test ? $var : false);
}
array_map() makes sure you can pass arguments to the array. It returns each value altered. So in the returning array, some values are present, others are set to false. array_filter() with no callback filters all falsey values from that array and you are left with the desired result
The in_array() function returns TRUE if needle is found in the array and FALSE otherwise. So by getting 1 as a result that means that "client corp" is found.
Check PHP in_array() manual
You can user array_search() to return the array key instead of using in_array().

in_array with single and multidimension

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

How to shift 2D array in PHP

Hi this is my 2D array format. I want to remove 1st inside array.
Array
(
[0] => Array
(
[0] => Array
(
[type] => section-open
)
)
[1] => Array
(
[0] => Array
(
[type] => section-close
)
)
)
I want to remove all inside array and return it like this
Array
(
[0] => Array
(
[type] => section-open
)
[1] => Array
(
[type] => section-close
)
)
I tried array_shift function it's not working...
Update: This was based on the example the user gave, but he expected it to work for arrays with more than one element.
array_shift() removes the first element of an array, but that's not what you want.
You have to build something yourself.
Something like:
$result = array();
foreach($my_array as $element)
{
$result[]=$element[0];
}
Since you probably want a real 2d shift I made a function which does that, removing the first level in the array, but keeping ALL the items in the second level.
Here is a working example:
http://codepad.org/H7iaTI1E
And the function:
/**
* Removes first level in an array, returning the 2nd level elements as an array
* #param array Array to process
* #return 2nd level items from the given array
*/
function array2dshift(array $array) {
$res = array();
foreach($array as $lvl1) {
foreach($lvl1 as $item) {
$res[] = $item;
}
}
return $res;
}

Remove items from array with value above/below threshold

What's the most efficient way to remove items from an array in php where the value is greater than a pre-determined threshold, e.g. given an array
Array
(
[0] => 1.639
[1] => 2.168
[4] => 1.897
[6] => 4.129
)
I would like to remove all the items with a value greater than e.g. 2, preserving key associations, to give
Array
(
[0] => 1.639
[4] => 1.897
)
I know I can do this using a foreach() loop but it seems that there should be a more elegant way.
No matter what you use, the array has to be looped through but you can hide it by using array_filter:
function test($var) { return $var < 2; }
$data = array_filter($data, 'test');

Categories