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
Right now, this block of code displays VLANs. I'm trying to display only the names(TLSoIP_JB1, TLSoIP_JB2, TLSoIP_JB3)
Array (
[0] => Array ( [0] => TLSoIP_JB1 [1] => 1 )
[1] => Array ( [0] => TLSoIP_JB2 [1] => 2 )
[2] => Array ( [0] => TLSoIP_JB3 [1] => 3 )
)
Regards Philipp
You can try:
foreach ($array as $key => $inner_array) {
echo $inner_array[0];
}
Or you can store the desired values in a separate array and print it afterwards:
$new_array = array();
foreach ($array as $key => $inner_array) {
$new_array[] = $inner_array[0];
}
print_r($new_array);
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 8 years ago.
Improve this question
Is there any simple solution to convert php mysql result change array structure with foreach or any method ?
Current result
Array
(
[1033] => stdClass Object
(
[id] => 1033
[plugin] => kooyke
[name] => kooyduration
[value] => 13
)
[1029] => stdClass Object
(
[id] => 1029
[plugin] => kooyke
[name] => kooyendpoint
[value] => http://localhost/public/data/
)
[1030] => stdClass Object
(
[id] => 1030
[plugin] => kooyke
[name] => kooylogin
[value] => ryrtrtr68fds876fdsf876fsd87fd
)
)
Expecting result
Array
(
[kooyduration] => 13
[kooyendpoint] => http://localhost/public/data/
[kooylogin] => ryrtrtr68fds876fdsf876fsd87fd
)
the code trying to convert
foreach($result as $value){
$expresult[] = $value['value'];
}
print_r($expresult);
Bellow code blocks may help you.
$i = 0;
$last_arr = array();
$new_arr = array();
foreach($my_array as $key=>$val)
{
$new_arr[$val->name] = $val->value;
}
array_push($last_arr,$new_arr);
You can not use like $value['value'], should be $value->value because of object. And push new array your name index. Try following:
$expresult = array();
foreach($result as $value)
{
$expresult[$value->name] = $value->value;
}
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How foreach this array to < a href="path-to-jpg-file">name< /a>
Array
(
[0] => Array
(
[0] => 01-siena-rosso-new.jpg
[1] => Siena Rosso
)
[1] => Array
(
[0] => 02-siena-noce-new.jpg
[1] => Siena Noce
)
[2] => Array
(
[0] => 03-zloty-dab.jpg
[1] => Złoty Dąb
)
)
Try loop like this:-
foreach($array as $value) {
echo ''.$value[1].'';
}
Working Demo
Expanding on Roopendras, you just need preg_replace:
foreach($array as $value) {
echo ''.$value[1].'';
}