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 8 years ago.
Improve this question
My foreach loop begins at last element. I have an array $gridRows within object $grid that is currently set up like so:
$grid
gridRows
2
gridCells
1
2
3
3
gridCells
1
2
gridRow 2 has 3 gridCells in it and gridRow 3 has 2 gridCells.
However, when I iterate through it, the first foreach goes to gridRow 3 first. It still iterates over gridRow 2 but it does it second.
$gridRows = $grid->getGridRows();
foreach ($gridRows as $rowKey => $rowValue) {
$gridCells = $rowValue->getGridCells();
foreach ($gridCells as $cellKey => $cellValue) {
// do something
}
}
What gives? I tried using sort($grid, SORT_NUMERIC) but I get the same effect.
Result of print_r($gridRows):
Array (
[3] => GridRow Object (
[gridCells:GridRow:private] => Array (
[2] => GridCell Object ( ... )
[1] => GridCell Object ( ... )
)
)
[2] => GridRow Object (
[gridCells:GridRow:private] => Array (
[1] => GridCell Object ( ... )
[2] => GridCell Object ( ... )
[3] => GridCell Object ( ... )
)
)
)
It seems to being an object not an array. Use a numerically indexed array to maintain the order you want.
Related
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 2 years ago.
Improve this question
I have a multi dimensional output array with following structure :
Array
(
[0] => Array
(
[id] => 5925
[fb_id] => 123
[description] =>
[video] =>
[thum] =>
)
[1] => Array
(
[id] => 7060
[fb_id] => 2344
[description] =>
[video] =>
[thum] =>
)
[2] => Array
(
[id] => 6579
[fb_id] => 123
[description] =>
[video] =>
)
)
Here, I want to limit the sub array count to 5 for all users with same 'fb_id'. Only 5 sub arrays for each fb_id should be in final output array. Any help would be appreciable.
The original array being used here is called $original and this code will reduce it down and produce a new array called $new. Feel free to change the variable names as appropriate.
$new = [];
$count = [];
foreach ($original as $element) {
$fbid = $element['fb_id'];
if (!isset($count[$fbid])) {
$count[$fbid] = 0;
}
if ($count[$fbid] < 5) {
$new[] = $element;
$count[$fbid]++;
}
}
// New array with limited sub-arrays
print_r($new);
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
How to get a number of element in array after every foreach
Like
foreach( array(20 elements in database) {
echo $element[1];
echo $element[2];
echo $element[3];
echo $element[4];
echo $element[5];
}
Don't know what actually you need. As per your comments if you have array with 9 indexes and want to get index 3 by 3, you can use array_chunk():
Example:
$yourArr = array(1,2,3,4,5,6,7,8,9);
$result = array_chunk( $yourArr, 3 );
echo "<pre>";
print_r($result);
Result:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
)
foreach($elements as $currentIndex => $element) {
var_dump($currentIndex); # "number" of element for simple (not assoc) arrays
}
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 7 years ago.
Improve this question
New to php and trying to figure out how to parse API data which is returned in what looks like a weird format. Here is a sample of the data:
[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],...,"zierfischforum.at":["1","0","0","0.00"]}]
Here is an example of how you can parse your JSON as an array:
$json_string = '[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],"zierfischforum.at":["1","0","0","0.00"]}]';
$json_array = json_decode($json_string, true); // true gets us an array
echo '<pre>';
print_r($json_array);
echo $json_array[1]['video2.stack.com'][0];
Provides the following results:
Array
(
[0] => Array
(
[campaign_id] => 9000
[date] => 2016-01-11
[totalcount] => 1838
[page] => 1
[totalpages] => 1
[index] => 1
[count] => 1838
)
[1] => Array
(
[video2.stack.com] => Array
(
[0] => 84254
[1] => 105
[2] => 0
[3] => 83.71
)
[zierfischforum.at] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0.00
)
)
)
84254
First we output the entire array. Based on data there we are able to single out a value for one of the array parts for video2.stack.com. It is relatively easy to traverse and you should be able extract any information you need. You could even build a recursive search function for your JSON.
NOTE: I removed some of your data(the part ,...) as it made your JSON non-valid.
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 have the following array that i want to re-arrange
Array
(
[0] => stdClass Object
(
[feeds_id] => 1338
[flag] => 0
)
[1] => stdClass Object
(
[feeds_id] => 1339
[flag] => 0
)
[2] => stdClass Object
(
[feeds_id] => 1339
[flag] => 1
)
)
I want to arrange it to look like this
[1338] => Array (
[0] => 0
)
[1339] => Array (
[0] => 0
[1] => 1
)
This code should work:
$newArray=array();
foreach($items as $item){
if(!is_array($newArray[$item->feeds_id])){
$newArray[$item->feeds_id]=array();
}
array_push($newArray[$item->feeds_id],$item->flag);
}
You should first create an empty array where the new data will be stored. Then, inside the foreach, you should use array_push, BUT if the sub-array in where you want to put the data is not an array, you should declare it first (that's why the "if" before the array_push)
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 8 years ago.
Improve this question
I have an array of objects, each object consists of an id and an organisation. It looks like this
Array (
[0] => stdClass Object (
[id] => 2 [organisation] => org1
)
[1] => stdClass Object (
[id] => 4 [organisation] => org2
)
[2] => stdClass Object (
[id] => 1 [organisation] => org3
)
)
I need to convert it into a simple associative array ([id]=>organisation,...) so the above example would look like this
Array (
[2] => org1
[4] => org2
[1] => org3
)
Greatful for any thoughts
Loop through it using a foreach statement and append it to another array.
$finished = [];
foreach($array as $arr) {
$finished[$arr->id] = $arr->organisation;
}
$result = array();
foreach($array as $arr) {
$result[$arr->id] = $arr->organisation;
}
echo "<pre>";print_r($result);