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
)
Related
I want to need multiple array combaine in one array
I have array
array(
0=> test 1
)
array(
0=> test 2
)
array(
0=> test 3
)
I need expected output
`array(
0=>Test1
1=>Test2
2=>test3
)`
You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.
Syntax:
array_merge(array ...$arrays): array
Example:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
Result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
You can check more here.
$a = array('test_1');
$b = array('test_2');
$c = array('test_3');
print_r(array_merge($a,$b,$c));
O/P - Array ( [0] => test_1 [1] => test_2 [2] => test_3 )
Hope you are doing well and good.
So, as per your requirement i found solution to get result.
$array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];
print_r(array_merge($array1,$array2,$array3));
In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array ...$array).
What is array_merge()?
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
How do you count items in an array that's part of an array? I'm using Advance Custom Fields plugin in WordPress and my array print_r(get_field('irl_today_entry')) output looks like this:
Array
(
[0] => Array
(
[acf_fc_layout] => irl_today_website_entry
[irl_today_website] => Array
(
[0] => Array
( data removed)
[1] => Array
( data removed )
)
)
[1] => Array
(
[acf_fc_layout] => irl_today_social_entry
[irl_today_social] => Array
(
[0] => Array
( data remove )
[1] => Array
( data remove)
)
)
)
How do you only count items in [irl_today_social]? I've tried a lot of options that do not work.
If you have multiple entries with irl_today_social you might also use array_map and array_column
$res = array_map(function($x) {
return count($x);
}, array_column($arrays, "irl_today_social"));
print_r($res);
Output
Array
(
[0] => 2
)
See a php demo
You can use array_reduce,
array_reduce(get_field('irl_today_entry'),function($a,$b){
return count($a["irl_today_website"]) + count($b["irl_today_website"]);
});
Loop through your array, get the count of your desired index, and add that to a counter.
$count = 0;
foreach(get_field('irl_today_entry') as $entry){
$count = $count + count(entry['irl_today_social']);
}
Simply you can use "Count" Function
Here is the Solution:
Step 1: First get your array items of ['irl_today_entry'].
Step 2 Then Count your ['irl_today_social'] items.
Example:
foreach(get_field('irl_today_entry') as $entry){
$socialData = count(entry['irl_today_social']);
}
Thanks
according to my understaing just simply try this
echo count($your_array[1]['irl_today_social']);
I have lot arrays and i want to only print number of 2 and first arrays
i use array_slice but there is still a problem
Arrays:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Array
(
[0] => 571
[1] => Roods
)
I need to like this:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Basically you seem to only need:
array_slice(array_unique(array_column($Myarrays, 'nidtitle')), 0, 2);
This should be done instead of the entire code you use to generate the arrays.
Short explanation:
array_column will get the element nidtitle from each "row" (array entry) in $Myarrays
After that we run that column through a unique function
Then we get the first 2 elements with an array_slice
This should do the job:
$finalarray = array_slice($Myarray, 0, 2);
print_r($finalarray);
You can use array_slice to get the items you want.
$arr = array(Array(441,"Awesome"),
Array(570,"Noons"),
Array(571,"Roods"));
$two = array_slice($arr, 0,2);
Var_dump($two);
Array_slice second parameter is where the slice should start.
Third parameter is count of values to slice.
https://3v4l.org/NPcJT
I am trying to pull dynamic element in single array but result is not showing properly
Ex:
$array =array();
$element="'abc1','abc2'";
$array=array('abc',$element);
//I want result like that:
array[
[0]=>abc,
[1]=>abc1,
[2]=>ab
]
If you need to parse a string of elements to an array you can use one of the csv functions. You may then merge your arrays.
$array = array('abc');
$string_elements = "'abc1','abc2'";
$array_elements = str_getcsv($string_elements, ',', "'");
$array = array_merge($array, $array_elements);
var_export($array);
Output:
array (
0 => 'abc',
1 => 'abc1',
2 => 'abc2',
)
Alternatively to add each array element to the end of another array you can push them like so using a splat:
array_push($array, ...$array_elements);
According to my understanding, $element is storing string not an array so even if you try to get output it will display in following format
Array
(
[0] => abc
[1] => 'abc1','abc2'
)
Instead of this you can store array in $element variable and use array_merge() function to get required output e.g.
$element = array('abc1','abc2');
$array = array('abc');
$result = array_merge($array,$element);
// Output
Array
(
[0] => abc
[1] => abc1
[2] => abc2
)
I have an array that contains entries that themselves contain two types of entries.
For simplicity sake, let's say that the entries are like this:
a|1
b|4
a|2
c|5
b|3
etc.
In fact they represent categories and subcategories in my database.
I will use explode to break these entries into letters and digits.
The question is: I want to group them by category.
What's the easiest way to create a multilevel array, which could be sorted by letters:
a|1
a|2
b|4
b|3
c|5
?
How about something like this?
$input = array('a|1','b|4','a|2','c|5','b|3');
$output = array();
foreach($input as $i){
list($key,$val) = explode("|",$i);
$output[$key][] = $val;
}
Output:
Array
(
[a] => Array
(
[0] => 1
[1] => 2
)
[b] => Array
(
[0] => 4
[1] => 3
)
[c] => Array
(
[0] => 5
)
)
<?php
$your_array = array();
$your_array[a] = array('1','2','3');
$your_array[b] = array('4','5','6');
print_r($your_array);
?>
I take it that your entries are strings (relying on the fact that you want to use explode() on them).
If so you can simply sort the array by using sort($array), and then iterate on that array and explode the values and put them in another array, which will be sorted by the previous array's order.