Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Inside a foreach loop, I am returning an array ($followerPosts).
foreach($myfollowers['entities'] as $myfollower)
{
$followerPosts=$this->displayPostsAction($myfollower->getFollower());
}
I need to have at the end one one big array containing all the $followerPosts arrays.
$bigArray = array();
foreach($myfollowers['entities'] as $myfollower)
{
$followerPosts=$this->displayPostsAction($myfollower->getFollower());
$bigArray[] = $followerPosts;
}
OR
$bigArray = array();
foreach($myfollowers['entities'] as $myfollower)
{
$bigArray[] =$this->displayPostsAction($myfollower->getFollower());
}
You can declare an array before the loop, then use array_merge on each iteration
or array_push, it depends on what you want to do
Use array_merge to put all of them into one array like this:
$big = array();
foreach($myfollowers['entities'] as $myfollower)
{
$big = array_merge($big, $this->displayPostsAction($myfollower->getFollower()));
}
You have to add them to the array.
$followerPosts = array()
foreach($myfollowers['entities'] as $myfollower)
{
//$followerPosts=$this->displayPostsAction($myfollower->getFollower());
$followerPosts[]=$this->displayPostsAction($myfollower->getFollower());
}
print_r(followerPosts)
For the purpose the best tool, I think, is the array_map function:
$followerPosts = array_map(function($f) {
return $this->displayPostsAction($f->getFollower());
}, $myFollowers['entities']);
var_dump($followerPosts);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need an array like this in PHP?
array = [mani , mani, nithi, nithi, basto]
return in
array = [basto]
not for other elements
Does anyone know how to solve this problem?
<?php
$items = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$counts = array_count_values($items);
// Remove elements that occur more than once.
$filtered = array_filter($items, function ($item) use ($counts) {
return $counts[$item] === 1;
});
var_export($filtered);
Output:
array (
4 => 'basto',
)
You may want to specify your question so users here can provide better assistance.
To your issue:
array_unique($array);
https://www.php.net/manual/en/function.array-unique.php
EDIT: you want to search all items by name, to do that you need this function: https://www.php.net/manual/en/function.array-search.php
with your example:
$array = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$basto = array_search('basto', $array);
Best,
Sebo
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i have this certain php function. But i cannot figured it out how to output this one. This is the question // Given an array, display each item in the array without using a loop. (Do not use built in functions to do this, like PHP's print_r; use a recursive function to implement)
and this is the code
<?php
function print_array(array $input)
{
}
?>
can someone share ideas on this? Any help is muchly appreciated.
you can use implode
$your_array = array("abc", "5", "xyz")
$text = implode(" ", $your_array); // implode with space you can use any other on this place
echo $text;
OUTPUT : abc 5 xyz
FUNCTION :
function print_array($your_array)
{
$text = implode(" ", $your_array); // implode with space you can use any other on this place
echo $text;
}
Recusive Method :
but only used when the key of the array is numeric starting with 0, 1, 2 ....
print_array($your_array);
function print_array($your_array, $index=0)
{
if(isset($your_array[$index]))
{
echo $your_array[$index];
$index+=1;
print_array($your_array, $index)
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array like this :
[[a,b],[c,d]]
How can I delete the outer array so it becomes:
[a,b],[c,d]
I tried using unset but cannot find a solution.
i got this answer by asign arrays in loops to another variable.. so how can i access the loops outside the loop it self?
foreach($arr3 as $key => $value)
{
$newArr[$key] = $value;
echo json_encode($value); // this will answer my question
}
echo json_encode($value); // when i echo outside loop it will not display as inside loop
Simple way to do this is as below.
foreach($array as $key => $value)
{
$newArr[$key] = $value;
}
$newArr contains new array which you are asking.
Comment Response
You can also con cat it.
$concat = "";
foreach($arr3 as $key => $value)
{
$newArr[$key] = $value;
$concat .= json_encode($value).',';
}
echo rtrim($concat,',');
You should not delete the outer array. You can simply access the index of the array and cast it to another array.
As an example;
$arr = array(array('x','y'),array('z'));
You can access this with;
$arr[0];
$array2=$array[0];
$array3=$array[1];
unset($array);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
foreach($items as $value)
{
if($value['item1']=='somestring')
{
// some PHP code...
}
}
Shouldn't $value refer to each value in $items array. What do they mean by $value['item1']? Does it mean $items is a multidimensional array or something?
It means $value is an array and therefore $items is a multidimensional array.
The $foo[] syntax is used by arrays and objects that emulate them (if that's what you mean). The index can be an integer or a string. As you already point out, PHP allows multidimensional arrays.
If you print $key and $value then out show you good result
foreach($items as $key=>$value) // $items contain array mean multidimensional
{
print_r($value);
print($key);//$key is index value
//item1 is array element and $value is array exist in $items
if($value['item1']=='somestring')
{
some PHP code...
}
}
so $items multidimensional array and $value array exist in $items array that's by $items multidimensional array
It means that $items is a multi-dimensional array.
And its looped over.
It has following structure for example:
array(
0 => array('item1' => 'something'),
1 => array('item1' => 'something1')
);
Through this code, we are looping so, in the statement,
foreach($items as $value)
We are getting the inner arrays 0, 1, .. and so on.
And in the line, if($value['item1']=='somestring'), we are getting array elements of 0, 1, and so on.
Hope you understand.
'item1' is the key to one of the elements inside the array $value.
So yes, this makes $items a multidimensional array.
All above are true if no errors are found running the code.