Shuffle inside array php - php

how to shuffle arrays in array ? I tried lots of way but I can't achieve that. I think it's very simple but I'm stuck on that.
Array
(
[2] => Array
(
[0] => 12011190
[1] => 12011158
[2] => 12011583
[3] => 12012107
[4] => 12011222
[5] => 12010638
[6] => 12013836
[7] => 12012232
[8] => 12011256
[9] => 12010007
[10] => 12012531
[11] => 12012182
[12] => 12013253
)
[6] => Array
(
[0] => 12011565
[1] => 12010020
[2] => 12011352
[3] => 12014366
[4] => 12011879
[5] => 12011449
)
)
I want to shuffle within arrays. I hope explain...

As far as I know, you can do it like this (assuming you want to shuffle every sub-array independently):
foreach($array AS &$element) {
shuffle($element);
}
Or maybe like this:
array_walk($array, function(&$value, $key) {
shuffle($value);
});

Here is a recursive multi-level function you can use.
function shuffle_array($arr) {
if (!is_array($arr)) return $arr;
shuffle($arr);
foreach ($arr as $key => $a) {
if (is_array($a)) {
$arr[$key] = shuffle_array($a);
}
}
return $arr;
}
print_r(shuffle_array($array));

Related

get count of entries in multi-dimensional array with comma separated values- php

How to get count of entries in an array below?
I tried Count arrays comma separated values and didnot get desired solution.Please suggest a method.
For below sample count is 11.
$sample= Array (
[0] => Array (
[0] => Array ( [attendance] => 2012SD71,2010SD94 )
[1] => Array ( [attendance] => 2003SD18,2003SD19 )
[2] => Array ( [attendance] => 2003SD23,2003SD28 ))
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] => Array (
[0] => Array ( [attendance] => 2012SD81,2010SD84 )
[1] => Array ( [attendance] => 2003SD18,2003SD19,2004SD14 ) ) [14] =>
[15] =>
);
A simple recursive function should work that uses explode() and array_merge():
function recurse($array,&$new)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurse($value,$new);
}
else {
if(!empty($value)) {
$exp = array_filter(explode(',',$value));
$new = array_merge($new,$exp);
}
}
}
}
# Create a storage array
$new = array();
# Run the recursive function
recurse($sample,$new);
# Show count
print_r(count($new));

list and count elements in array php

I have the following array and am trying to loop through it with php, generate a list of the "Type" and count how many. The Type is the key, but there will be unique values such as Call, To-do, Meeting, Proposal, etc. My goal is to have the following out put:
Call 2
To-do 1
Meeting 3
Proposal 4
The above are not the values that will be output from the following array, but I wanted you to have an idea of what I am trying to accomplish. Please help!
Array (
[0] => Array (
[0] => Call
[Type] => Call
[1] => fxxxx#xxxxentllc.com
[EmailAddress] => xxxxr#xxxxentllc.com
[2] => 3xxxx00
[Phone] => 31xxxx00
[3] => 31xxxx871
[MobilePhone] => 31xxxx871
[4] => 102795
[CustomerID] => 102795
[5] => Nortxxxxal
[Company] => Noxxxxal
[6] => Frank
[FirstName] => Frank
[7] => Syxxxxer
[LastName] => Sxxxxter
[8] => 3
[Priority] => 3
[9] => invite to Haxxxxales for lunch
[Details] => invite to Hafxxxxales for lunch
[10] => 4503
[ActivityID] => 4503
[11] => 05/23/13
[DueDate] => 05/23/13
)
[1] => Array (
[0] => To-do
[Type] => To-do
[1] => fsxxxxer#summxxxxntllc.com
[EmailAddress] => fsxxxxer#summixxxxtllc.com
[2] => 315xxxx000
[Phone] => 3154xxxx0
[3] => 315xxxx1
[MobilePhone] => 315xxxx1
[4] => 102795
[CustomerID] => 102795
[5] => Norxxxxl
[Company] => Norxxxxcal
[6] => Frxxxxk
[FirstName] => Fxxxxk
[7] => Sxxxxr
[LastName] => Syxxxxer
[8] => 3
[Priority] => 3
[9] => find out who contact is for xxxxdical center
[Details] => find out who contact is foxxxxcal center
[10] => 4504
[ActivityID] => 4504
[11] => 05/23/13
[DueDate] => 05/23/13
)
)
This should do it:
$type_counts = array_count_values(array_map(function($x) {return $x['Type'];}, $array));
The array_map will return an array containing all the Type elements, then array_count_values will count the number of each of these and return this as an associative array.
something along the lines of:
foreach ($array as $key => $value) {
if (isset($result[$key])) {
$result[$key]++;
} else {
$result[$key] = 1;
}
}
if it is a muldi-dimensional array, just put another for each in there but that should give you an idea
$types = array();
foreach($array as $item) {
isset(${$item['Type']}) ? ${$item['Type']}++ : ${$item['Type']} = 1;
if(!in_array($item['Type'], $types) {
$types[] = $item['Type'];
}
foreach($types as $type) {
echo "$type ${$type}\n";
}
I think you can loop in your array and count each occorrence
$Call = 0;
$To_do = 0;
$Meeting = 0;
$Proposal = 0;
foreach($array as $data)
{
if($data['Type'] == 'Call')
{
$Call++;
} else if($data['Type'] == 'To-do')
{
$To_do++;
} else if($data['Type'] == 'Meeting')
{
$Meeting++;
} else if($data['Type'] == 'Proposal')
{
$Proposal++;
}
}
In this way you will have stored in each variable $Call $To_do $Meeting $Proposal its relative count
you can use (array_count_values($array));
http://php.net/manual/en/function.array-count-values.php
This gives count of all the keys in an array.
If you want only for the specific keys then use foreach loop
The one Petros mentioned is the best way to do it.
I would do something along the lines of:
$your_array = /* Your Array */
$types_count = array();
foreach ($your_array as $type_array){
$types_count[$type_array['Type']]++;
}

Remove first levels of identifier in array [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 10 months ago.
I think this has been up before, but could'nt find any answer to it. If it's already answered please point me in the right direction with a link.
I have an array that I wan't to remove the first levels of identifier. I think there is a function for this?
Example of how it is:
[0] => Array
(
[8] => Röd
)
[1] => Array
(
[8] => Blå
)
[2] => Array
(
[6] => Bobo
)
[3] => Array
(
[8] => Grön
)
[4] => Array
(
[7] => Sten
)
[5] => Array
(
[8] => Vit
)
[6] => Array
(
[7] => Guld
)
[7] => Array
(
[6] => Lyxig
)
What I wan't
[8] => Röd
[8] => Blå
[6] => Bobo
[8] => Grön
[7] => Sten
[8] => Vit
[7] => Guld
[6] => Lyxig
Try to merge array with splat operator:
print_r(array_merge(...$array));
The problem here is preserving the keys for the identifier you want. You have some names strings that have the same key (like Blå and Röd). You either need to store these in an array or be willing to lose the key.
Example with php5.3:
$processed = array_map(function($a) { return array_pop($a); }, $arr);
This will give you:
[0] => Röd
[1] => Blå
[2] => Bobo
[3] => Grön
[4] => Sten
[5] => Vit
[6] => Guld
[7] => Lyxig
It has become clear the keys on the inner array need to be preserved because they are some kind of id. With that said you must change the end structure you're going for because you can have 2 of the same key in a single array. The simplest structure then becomes:
[8] => Array
(
[0] => Röd,
[1] => Blå,
[2] => Vit,
[3] => Grön
)
[6] => Array
(
[0] => Bobo,
[1] => Lyxig
)
[7] => Array
(
[0] => Sten,
[1] => Guld
)
To get this structure a simple loop will work:
$processed = array();
foreach($arr as $subarr) {
foreach($subarr as $id => $value) {
if(!isset($processed[$id])) {
$processed[$id] = array();
}
$processed[$id][] = $value;
}
}
PHP array_column
$new_array = array_column($old_array,0);
This will retrieve the value at index 0 for each array within $old_array. Can also be using with associative arrays.
use :
public function remove_level($array) {
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $value);
}
}
return $result;
}
which will return second level array values in the same order of the original array.
or you can use array_walk
$results = array();
array_walk($array, function($v, $k) use($key, &$val){
array_merge($results, $v);
});
Below code will also achieve the same result.
$resultArray = array_map('current',$inputArray);
OR
$resultArray = array_map('array_pop',$inputArray);
Note: I have ignored OP's expected result keys. Because it is not possible to have the same keys in the array. The last key will replace the previous one if the same.
foreach($array as $key=>$val) {
$newarr[$val] = $array[$key][$val];
}
untested!
Check this out this is what expected result
<?php
$arrData = array(
"5" => array
(
"8" => "Vit"
),
"6" => array
(
"7" => "Guld"
)
);
foreach($arrData as $key=>$value):
foreach($value as $k=>$v):
$data[$k] = implode(',',$arrData[$key]);
endforeach;
endforeach;
print_r($data);
?>

Convert associative array into indexed

Ive seen a few examples by using array_values, but cant quite make out how to get it to work...
I have an associative array thats passed via POST, I need to convert it into a indexed array...
My print_r($_POST) gives me this... I need all of this put into an indexed array :)
Array (
[fieldnames] => 36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
[36771X21X198] => 3434343
[display36771X21X198] => on
[36771X21X199] => 5656565
[display36771X21X199] => on
[36771X21X200] => 89898989
[display36771X21X200] => on
[36771X21X201] => 90909090
[display36771X21X201] => on
[36771X21X202] => 12121212
[display36771X21X202] => on
[move] => movesubmit
[move2] => ONLINE Submit
[thisstep] => 1
[sid] => 36771
[token] => 1234567890
)
Observe this amazing way to convert your $_POST into a numerically indexed array:
$numerical = array_values($_POST);
but what if you want to preserve your keys? Perhaps you want something like this?
$numerical = array();
$sep = ':';
foreach($_POST as $k=>$v)
{
$numerical[] = $k.$sep.$v;
}
$numerical will then have:
Array
(
[0] => fieldnames:36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
[1] => 36771X21X198:3434343
[2] => display36771X21X198:on
[3] => 36771X21X199:5656565
[4] => display36771X21X199:on
[5] => 36771X21X200:89898989
[6] => display36771X21X200:on
[7] => 36771X21X201:90909090
[8] => display36771X21X201:on
[9] => 36771X21X202:12121212
[10] => display36771X21X202:on
[11] => move:movesubmit
[12] => move2:ONLINE Submit
[13] => thisstep:1
[14] => sid:36771
[15] => token:1234567890
)
or, for my final example:
$fieldnames_original = explode('|', $_POST['fieldnames']);
$fieldnames_actual = array();
$values = array();
foreach($_POST as $k=>$v)
{
if($k!='fieldnames')
{
$fieldnames_actual[] = $k;
$values[] = $v;
}
}
which will set 3 arrays:
$fieldnames_original:
Array
(
[0] => 36771X21X198
[1] => 36771X21X199
[2] => 36771X21X200
[3] => 36771X21X201
[4] => 36771X21X202
)
$fieldnames_actual:
Array
(
[0] => 36771X21X198
[1] => display36771X21X198
[2] => 36771X21X199
[3] => display36771X21X199
[4] => 36771X21X200
[5] => display36771X21X200
[6] => 36771X21X201
[7] => display36771X21X201
[8] => 36771X21X202
[9] => display36771X21X202
[10] => move
[11] => move2
[12] => thisstep
[13] => sid
[14] => token
)
and $values:
Array
(
[0] => 3434343
[1] => on
[2] => 5656565
[3] => on
[4] => 89898989
[5] => on
[6] => 90909090
[7] => on
[8] => 12121212
[9] => on
[10] => movesubmit
[11] => ONLINE Submit
[12] => 1
[13] => 36771
[14] => 1234567890
)
function
function array_default_key($array) {
$arrayTemp = array();
$i = 0;
foreach ($array as $key => $val) {
$arrayTemp[$i] = $val;
$i++;
}
return $arrayTemp;
}
Pass the associative array as a parameter and it will convert into the default index of the array. For example: we have Array('2014-04-30'=>43,'2014-04-29'=>41) after the call to the function the array will be Array(0=>43,1=>41).
Recursive assoc to indexed converter tested on a small array.
function assoc2indexedMulti($arr) {
// initialize destination indexed array
$indArr = array();
// loop through source
foreach($arr as $val) {
// if the element is array call the recursion
if(is_array($val)) {
$indArr[] = assoc2indexedMulti($val);
// else add the value to destination array
} else {
$indArr[] = $val;
}
}
return $indArr;
}

array_intersect() in php with special purpose

Hi need to intersect two arrays in a special function. the two arrays are:
Array A (
[0] => 104-20_140.1 [1] => 104-10_136.1 [2] => 104-40_121.1 [3] => 104-41_122.1
[4] => 200-42_951.1 [5] => 200-43_952.1 [6] => 200-44_123.1 [7] => 200-45_124.1
[8] => 300-46_125.1 [9] => 300-47_126.1 [10] => 300-48_127.1 [11] => 300-49_128.1
[9] => 380-56_125.1 [10] => 380-57_126.1 [11] => 380-58_127.1 [12] => 380-59_128.1
)
Array B (
[0] => 200 [1] => 300
)
I need two look at Array A's beginning of the value. Ex. [0] => 104-20_140 and see if the beginning '104' it excists in Array B. If not Array A shall remove it from the result array C.
the output with Array A and B shall have:
Array C (
[0] => 200-42_951.1 [1] => 200-43_952.1 [2] => 200-44_123.1 [3] => 200-45_124.1
[4] => 300-46_125.1 [5] => 300-47_126.1 [6] => 300-48_127.1 [7] => 300-49_128.1
)
All shall be calculated in Php
thx for all the help!
Try this:
function startsWith($haystack, $needle) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
$C = array();
foreach ($A as $ka => $va) {
foreach ($B as $kb => $vb) {
if (startsWith($va, $vb)) {
$C[] = $va;
}
}
}
example on codepad
Chances are, what you really need is array_uintersect. This will give the option to provide a custom callback which contains the logic on how to check if values intersect.
http://uk3.php.net/manual/en/function.array-uintersect.php
In the callback, you will need to parse out first section before the first "-" using substr or one of the preg functions.

Categories