Create an array-of-arrays from an array's elements - php

I'm looking for a function to take a simple array and create an array of arrays, without iterating through the original array (i.e., foreach)
For example, give this array:
['a','b','c','d']
I want the result to be:
[['a'], ['b'], ['c'], ['d']]
Does such a function exist?

You can use array_chunk() Documentation:
http://php.net/manual/en/function.array-chunk.php
It will split an array into a multi-demential array of "chunks." For you, you want every chunk to be 1 variable in size.
$Alphabet = array('a','b','c','d');
$Chunked = array_chunk($Alphabet, 1); // Chunk the Alphabet array in 1 size chunks
print_r($Chunked);
This will produce:
Array
(
[0] => Array
(
[0] => a
)
[1] => Array
(
[0] => b
)
[2] => Array
(
[0] => c
)
[3] => Array
(
[0] => d
)
)

Related

Can I split an array of this format to this?

How can I do the below change in PHP?
Input:
[hiddenAllPrefered] => Array
(
[0] => 14477,14478,14479,14485,14486,14487
)
Output should be like this:
[hiddenAllPrefered] => Array
(
[0] => 14477,14478,14479
[1] => 14485,14486,14487
)
A possible solution:
$input = array('14477,14478,14479,14485,14486,14487');
$output = array_map(
function (array $a){
return implode(',', $a);
},
array_chunk(
explode(',', $input[0]),
3
)
);
Read it from inside out:
explode() splits the string $input[0] using comma (,) as delimiter and returns an array;
array_chunk() splits the array into chunks of size 3; it returns an array of arrays, each inner array contains 3 elements (apart from the last one that can contain less);
array_map() applies the function it receives as its first argument to each value of the array it gets as its second argument (the array of arrays returned by array_chunk()); it returns an array whose values are the values returned by the function;
the anonymous function passed to array_map() gets an array (of size 3 or less) and uses implode() to join its elements into a string, using comma (,) to separate the values and returns the string;
array_map() puts together all the values returned by the anonymous function (one for each chunk of 3 elements of the array) into a new array it returns.
The output (print_r($output)) looks like this:
Array
(
[0] => 14477,14478,14479
[1] => 14485,14486,14487
)
try this as a boilerplate
function chunker($arr, $l) {
return array_chunk($arr, $l);
}
print_r(chunker($hap, 3));
/*
Array
(
[0] => Array
(
[0] => 14477
[1] => 14478
[2] => 14479
)
[1] => Array
(
[0] => 14485
[1] => 14486
[2] => 14487
)
)
*/
UPDATE
php > $h = [ "14477,14478,14479,14485,14486,14487" ];
php > $hap = explode(",", $h[0]);
php > print_r($hap);
Array
(
[0] => 14477
[1] => 14478
[2] => 14479
[3] => 14485
[4] => 14486
[5] => 14487
)
php > print_r(chunker($hap, 3));
Array
(
[0] => Array
(
[0] => 14477
[1] => 14478
[2] => 14479
)
[1] => Array
(
[0] => 14485
[1] => 14486
[2] => 14487
)
)
php >

Return the difference of multi-dimensional arrays

how can I compare these two arrays?
I want the result be elements that are not available in one array. For example:
Array #1
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
)
Array #2
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
)
Result
Array
(
[0] => Array
(
[0] => c
[1] => 3
)
)
In set theory this is call symmetrical difference.
First you need to remove any possible duplicates, then get the individual differences for both arrays and the merge those two individual differences.
$a = array('John', 'Bob', 'Mary', 'Serena');
$b = array('Jim', 'Mary', 'John', 'Bob');
// Remove any duplicates
$a = array_unique($a);
$b = array_unique($b);
// Get the individual differences, using array_diff()
$a_minus_b = array_diff($a, $b);
$b_minus_a = array_diff($b, $a);
// Simply merge them together to get the symmetric difference
$symmetric_difference = array_merge($a_minus_b, $b_minus_a); // produces ['Serena', 'Jim']
You may need to make the necessary changes for a 2D array if you need to.
I hope this help.

Check same word on each array

I want to check that a string contains the same word when comparing 2 different array's. If there is the same word on each array it will show how many on each inside multidimensional array
Array one is common array type and array two is multidimensional array
Array 1:
Array
(
[0] => royalty
[1] => free
[2] => picture
)
Array 2:
Array
(
[0] => Array
(
[0] => Affordable and search from millions of royalty free picture
)
[1] => Array
(
[0] => from millions of royalty picture
)
[2] => Array
(
[0] => Provides free picture upload and hosting
)
[3] => Array
(
[0] => Post your picture here Get permanent links
)
[4] => Array
(
[0] => Choose your own unique username to access image
)
)
Result:
Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 2
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 0
)
)
From the above example, array contain each word of royalty free picture will show how many word the same in each inside multidimensional array
I was trying it using strcasecmp() but it just give me 0 results if two string contains same word and I think it can't give right result for string with so many words.
Split string and find intersection with the array of seached words
foreach($array2 as &$item)
$item[0] = count(array_intersect($array1, explode(' ', $item[0])));
The easiest way is probably to loop through Array 2 and compare each word to the string with strstr() or preg_match
Something like this (incomplete):
foreach($array2 as $sentence){
foreach($array1 as $word){
if(strstr($word, $sentence) !== false){
$wordsFound++;
}
}
}

Deleting a number from an array in PHP

I want to remove a number from an array, for example this array:
Array ( [0] => [1] => 2 [2] => 3 )
I want to remove the number 2 so i will get this:
Array ( [0] => [1] => 3 )
but I get this:
Array ( [0] => [2] => 3 )
What I did is to check if the number 2 is in the array and removing it with unset. Is there a better way to do this?
You can reset the numeric indices of your array after you removed the element with array_values.
unset($myArray[1]);
$myArray = array_values($myArray);

Using array_flip() to return an array

I have an array:
$input = array(1,2,3,4,6,5,3,6)
and I want the key/value pairs to be flipped.
This can be done by using the array_flip() function.
$flipped = array_flip($input)
If in the original array has 2 or more same values (in this case number 6)how can I return it in an array?
array= ([1]=0,[2]=>1,[4]=>2,[6]=>array(3,6),[5]=>4,[3]=>5)
I tried to use array_count_values() but can't figure out how to do it?
You cannot do that using the array_flip() function. Probably you look for something like that:
<?php
function array_flip_and_collect($input) {
$output = [];
foreach ($input as $key=>$val) {
$output[$val][] = $key;
}
return $output;
}
$input = array(1,2,3,4,6,5,3,6);
print_r(array_flip_and_collect($input));
The output:
Array
(
[1] => Array
(
[0] => 0
)
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 2
[1] => 6
)
[4] => Array
(
[0] => 3
)
[6] => Array
(
[0] => 4
[1] => 7
)
[5] => Array
(
[0] => 5
)
)
Note that the output differs slightly from what you suggested in your question. That is by purpose because this appears more logical to me. If you really want that keys with only one element really are scalars and not arrays with one element, then you have to add an additional conversion step to the code.

Categories