Splitting an array equally: - php

I have the following array:
$array = array(1,0,0,0,1,1,1,1,0,1,1,1,1,0,1,1,0,1,0,0,1,0,1);
I want split it up into individual arrays so that each array contains seven or less values.
So for example the first array will become:
$one = array(1,0,0,0,1,1,1)
$two = array(1,0,1,1,1,1,0)
$three = array(1,1,0,1,0,0,1);
$four = array(0,1);
Also how would you count the number of times 1 occurs in array one?

array_chunk() is what you are looking for.
$splitted = array_chunk($array, 7);
For counting the occurences I would be lazy. If your arrays only contain 1s or 0s, then a simple array_sum() would do:
print array_sum($splitted[0]); // for the first chunk

I want split it up into individual arrays so that each array contains seven or less values.
Use array_chunk(), which is made expressly for this purpose.
Also how would you count the number of times 1 occurs in array one?
Use array_count_values().
$one = array(1,0,0,0,1,1,1);
$one_counts = array_count_values($one);
print_r($one_counts);
// prints
Array
(
[0] => 3
[1] => 4
)

Assuming you want to preserve the contents of the array, I'd use array_slice() to extract the needed number of elements from the array, incrementing the '$offset' by the required count each time until the array was exhausted.
And as to your second question, try:
$num_ones=count(preg_grep(/^1$/,$array));

Related

Compare the difference of two array index by index

I want to know how can I compare two arrays if there's a differences between their values in each indexes. I have this two arrays for example.
$arr1 = ["0"=>"A", "1"=>"B", "2"=>"C", "3"=>"A"]..
$arr2 = ["0"=>"A", "1"=>"C", "2"=>"C", "3"=>"A"]..
The result that I want to get would be 1 because only index 1 is not equal with the index 1 of the second array.
I tried using array_diff but the result is always 0. I want to compare each array by indexes and values and return the number of differences on each.
Thank you
$arr1 = ["0"=>"A", "1"=>"B", "2"=>"C", "3"=>"A"];
$arr2 = ["0"=>"A", "1"=>"C", "2"=>"C", "3"=>"A"];
print_r(array_diff_assoc($arr1, $arr2)); // output: [1 => "B"]
Is this what you want? If you only need an index, you can do this
print_r(array_keys(array_diff_assoc($arr1, $arr2))); // output: [1]

How to Combine two array rand from two array data?

I m so confusing here with this problem, please help me to solve this.
I have 2 array data,
$one = array ("sinta","jojo","wawan","silvie");
$two = array ("eat","sleep","breakfast","sport");
I want to create a random output array from this 2 array, and i want this only pick 2 random array from each array data, so maybe the result would be like this :
$three = array ("sinta","silvie","breakfast","eat");
or
$three = array("jojo","silvie","eat","sleep");
and etc..
Normally I don't like code only answers, but:
$three = array_merge(array_rand($one, 2), array_rand($two, 2));
shuffle($three);
You can read up on array_rand and array_merge and shuffle in the linked manuals. This code picks 2 elements each from $one and $two at random, and then randomizes the order of the result.
Small warning: If you have string keys in your arrays they will be destroyed.
$three[0] = $one[rand(0,3)];
$three[1] = $one[rand(0,3)];
$three[2] = $two[rand(0,3)];
$three[3] = $two[rand(0,3)];
thats the logic - I did not php for a while so check the syntax, please.
Now you have to assure that rand is not picking the same value twice or youhave to eat two breakfasts :-)

How to split array into two different array

How to split array into two different array and return both. here my array is $input , it may contain any number of element.
for example:
$input = array ('onex','twox','threex','fourx','fivex','sixx','sevenx','eightx','ninex');
I want to split my '$input' array into two different array '$number1' and '$number2' .
1)if $input array with even element then split into 2 equal element arrays.
2)if $input array with odd element then '$number1' is always 1 element greater than '$number2' .
You can use array_chunk for it.
$new_arrays = array_chunk($input, ceil(count($input)/2));
$number1 = $new_arrays[0];
$number2 = $new_arrays[1];

change starting index when assigning one array to another in php

I am using str_split() to split a long strings into an array of length 16 each. And I'm assigning the returned array to one in my function. Like this:
$myarray = str_split($string, 16);
The problem is that I want the indexing of $myarray to start from a number other than 0, say 50. Currently I'm doing this:
foreach($myarray as $id => $value)
{
$myarray[$id + 50] = $value;
unset($myarray[$id]);
}
Is there a better solution? Because the arrays and strings I'm dealing with are very long. Thanks
You can use array_pad().
$myarray = str_split($string, 16);
$myarray = array_pad($myarray, -(size($myarray)+50), null);
It will fill the first 50 elements with nulls and push the rest of the array forward by 50 elements.

How to get the last n items in a PHP array as another array?

How to I get an array of the last n items of another array in PHP?
$n is equal to the number of items you want off the end.
$arr = array_slice($old_arr, -$n);
You can use array_slice:
$arr = array_slice($old_arr, -$n, $n, true);
If the array indices are meaningful to you, remember that array_slice will reset and reorder the numeric array indices. You need the preserve_keys flag (4th parameter) set to true to avoid this.

Categories