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.
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 5 months ago.
Improve this question
how to merge data array of to array in php?
i have 2 arrays like:
$a=[1,2]; $b =[3,4];
and i want to merge data like:
$data = [[1,3]],[2,4]];
how to code in php like array_merge or something in php or laravel?
i have try using array_merge but not my expectation data like this:
$data = array_merge($a, $b);
but data always return
[1,2,3,4]
i have clue about this, can someone help this problem?
Since we concluded in the comments that you actually want a multidimensional array, we need to create new arrays so we can swap the values around between the original arrays.
Here's one possible solution (the comments explain the different parts)
$a = [1,2];
$b = [3,4];
// Initiate a new array
$newArray = [];
// Iterate through one of the arrays
foreach ($a as $index => $value) {
// On each iteration, take the values from both arrays for that specific array index
// and add them as a new sub array.
$newArray[] = [
$a[$index],
$b[$index]
];
}
$newArray will now contain a multidimensional array:
[[1,3],[2,4]]
Here's a demo: https://3v4l.org/okHhQ
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
how would I convert my array to an array of objects with PHP ?
input
[1, 2, 3]
output
[ {id:1}, {id:2}, {id:3} ]
When you have to reformat array, e.g. transorm values of the array to another format (number to object with id->number) and they map 1:1 (the new array have the same number of elements like the original), array_map is the solution
$newArray = array_map(function($item) {
$object = new \StdClass;
$object->id = $item;
return $object;
}, $array);
If you defined your object before, I guess I'd do a loop
class previouslyDefinedObject{
public $id;
}
$myArray = array(1,2,3);
$newArray = array();
foreach($myArray as $id){
$obj = new previouslyDefinedObject();
$obj->id = $id;
array_push($newArray, $obj);
}
print_r($newArray);
That way, your $newArray will contains every object in an 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 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 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);