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);
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 6 years ago.
Improve this question
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo $value; // Result: 14
the result inside loop is: 10,11,12,13,14
and outside loop is : 14
I want to use all value outside of loop
Use below code to get your array values comma seperated
implode(",", $reciveValue)
Try this :
$value = '';
foreach($reciveValue as $val){
$value .= $val.",";// Result: based on user input like:10,11,12,13,14
}
echo rtrim($value, ',');
#Support Techcherry if you want to use all values of an array outside
the loop then you can use implode() function, below is an example
understand
<?php
$reciveValue = array(10,11,12,13,14); // suppose this is your array
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo "<br>";
echo implode(',', $reciveValue); // here implode() function convert the array value in the string form
?>
try it, it will help you
thanks to everyone for posting answer
every answer is very helpful for me
but finally i got my solution
$result="";
foreach($reciveValue as $value)
{
$result=$value.",".$result;
}
echo $result;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem is actually, there is an array containing a number of arrays. I have to print the elements of one of the sub arrays named 'list'. But I have a problem to fetch the subarray while using foreach loop.
The following code is:
foreach($arr as $key => $value)
{
$arr1=$value[$list];
echo $arr1;
}
Your code should be:-
// declare second array outside loop
$arr1 = [];
foreach($arr as $key => $value)
{
// list is a key in $value array then you can access via below way
$arr1[] = $value['list'];
}
// print Array using print_r() function outside loop
print_r($arr1);
Hope it will help you :)
If your array like in below format
<?php
$arr = array(
'key' => array('array elements here'),
'list' => array('array elements here'),
'key' => ........
);
?>
you can use below to get only list array
<?php
foreach($arr as $key => $value)
{
if($key == 'list') {
$arr1=$value['list'];
break;
}
}
print_r($arr1);
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to iterate through this array http://2of1.com/zee/ZEES%20SMS%20SERVICE.html and extract the data.
I was using:
foreach ($graphObject['data'] as $key => $value){
$string = $value->message;
$link = $value->actions[0]->link;
$pic = $value->picture;
$post_id = $value->id;
}
But it is no longer working after i added a second source to the array.
When i try:
foreach ($graphObject as $key => $value){
$string = $value->data[0]->message;
$link = $value->data[0]->actions[0]->link;
$pic = $value->data[0]->picture;
$post_id = $value->data[0]->id;
I get only the first entry values from data[0] and it does not iterate through all the data. What i need is the data from data[0] data[1] data[2] data[3]... etc etc... Please help. Thank you!
Probably something like
foreach ($graphObject as $keyEntry => $entry){
foreach( $entry->data as $data ) {
echo $data->picture, "<br />\r\n";
foreach( $data->actions as $action ) {
echo $action->link, "<br />\r\n";
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.
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);