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;
}
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 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);
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 9 years ago.
Improve this question
I have this kind of array.
Array
(
[0] => Array
(
[supplierID] => 1
[parkTypeID] => 1
[cost] => 17
)
[1] => Array
(
[supplierID] => 2
[parkTypeID] => 1
[cost] => 65
)
[2] => Array
(
[supplierID] => 2
[parkTypeID] => 2
[cost] => 30
)
)
I want to combine elements which has same value for supplierID like this. Each key name's value has set in new array's keys.
Array
(
[1] => Array
(
[1] => 17
)
[2] => Array
(
[1] => 65
[2] => 30
)
)
Try
$r = array();
foreach ($array1 as $x) {
if (array_key_exists($x['supplierID'], $r)
$r[$x['supplierID']][$x['parkTypeID']] = $x['cost'];
else
$r[$x['supplierID']] = array($x['parkTypeID'] => $x['cost']);
}
This will iterate over the initial array $array1 and create a new array $r with the desired information.
For each element in the original array we check whether supplierID exists in $r.
If so, we just append the new correlation between the parkTypeID and the cost to the existing values.
If not, we add the supplierID to $r and assign it an array with only this pair of parkTypeID and cost.
A simplified version of the above code that relies on php to initialize an unset array index to an empty array when necessary is
$r = array();
foreach ($array1 as $x)
$r[$x['supplierID']][$x['parkTypeID']] = $x['cost'];
$arr_res = array_reduce($arr, function($t, $v) {
if (!isset($t[$v['supplierID']]))
$t[$v['supplierID']] = array();
$t[$v['supplierID']][] = $v['cost'];
return $t;
} , array());
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);