I have an array like below --
input array
I want the array to be like ----
output
How I can loop through this array to get desired output ?
Kindly guide.
Thanks.
Since you won't put any research effort into the question yourself, I'll just give you the half answer ...
foreach($inputArray as $i) {
$newArray[$i[7]] = $i[8];
}
Now you just have to add them into the specific array. Tip: You need another foreach loop.
Do something like this --
foreach($new_array as $item) //// for looping outer array
{
foreach($item as $n)
{
$newArray[$n[7]] = $n[8]; /// for looping inner array
}
}
Related
This question is about how to produce a better code in PHP.
I've multiple arrays (5 to be exact). I've to apply the same logic to all of them.
How can I avoid to duplicate the code for all of them ?
$cpus = getCPUs(); // Get array 1 dimension with key
$rams = getRAMs(); // Get array 1 dimension with key
I mean, the code inside, I just have to create a function. That's OK.
But I still have to declare one foreach loop for each array...
Is there a way to avoid that ? It's like to have my foreach loop parameters from variables.
foreach ($cpus as $key_cpu => &$cpu) {
// FUNCTION XXX
}
foreach ($rams as $key_ram => &$ram) {
// FUNCTION XXX
}
Regards
You could simply use two foreachs:
foreach ([&$cpus, &$rams] as &$components) {
foreach ($components as $key => &$component) {
// FUNCTION XXX
}
}
Note that all those references are needed to be able to assign another value to $component and have it also modify the original values (like the question suggested). Ideally you'd want to avoid doing that if you can. If these components are arrays, explore using objects instead.
Thanks to #AterLux for the helpful comments below.
write common function/method for loop all array and return whatever your want...
function loop_array($array){
$return_data=array()
foreach ($array as $key => &$data) {
// FUNCTION XXX
}
return $return_data;
}
I have a multidimensional array returned by drupal render function in that array i want to choose some last values
for example
$array['static_name'][number]['changes_every_time']['some_name']['value_i_need'];
is there ant way we can skip the "changes_every_time" level while printing array
is there a way we can use wild character in there
like if i want to print
echo $array['static_name'][number][*]['some_name'][value_i_need];
some thing like this
Store answer "no".
But what you can do is do a foreach over the keys of $array['static_name'][number]
foreach($array['static_name'][number] as $val) {
echo $val['some_name'][value_i_need];
}
This way you don't have to care.
well, you cold iterate the array at that "level" with a foreach loop
Untested code:
foreach ($array['static_name'][number] as $key => $value) {
echo $value['some_name'][value_i_need];
}
I have this object containing one row and many keys/variables with numbered names.
I need to pass each of them one at a time to another function. How do I loop through the keys instead of the rows?
The code would look like this:
foreach ($object['id'] as $row):
$i++;
$data['myInfo'][$i] = $this->get_data->getInfo('data1', 'id', $row->{'info'.$i.'_id'});`
but this obviously won't work since it's looping through the rows/instances of an object, and I have only one row in my $object['id'] object (with info1_id, info2_id, info3_id, info4_id... etc keys), so the loop stops after just one cycle. And I really don't feel like typing all of that extra code by hand, there's gotta be solution for this. :)
You can just iterate through your object like an array :
foreach ($object['id'] as $row) {
foreach ($row as $k => $v) {
$id = substr($k, 4, strpos($k, '_')-4);
$data['myInfo'][$id] = $this->get_data->getInfo('data1', 'id', $v);`
}
}
Thanks for the directions Alfwed, I didn't know you could use foreach loop for anything other than instances of an object or arrays (only started learning php a week or so ago), but now it looks pretty straight forward. that's how I did it:
foreach ($object['id'] as $row):
foreach ($row as $k=>$v):
$i++;
if ($k == print_r ('info'.$i.'_id',true)){
$data['myinfo'][$i] = $this->get_db->getRow('products', 'id','info'.$i.'_id');
<...>
in my case, I knew how many and where were those values, so I didn't have to worry about index values too much.
foreach ( $this->parent->get_sections(null, $this->parent->author) as $section)
{
//...
}
I'm trying to do is force the loop to output each $section in the order I want. Each $section's name can be retrieved by $section->name. Let's say that I want to output $section "Section 2" first and then "Section 1" (and not in the order of the foreach). How can I force it do that? I presume the proper way would be a for loop with an if checking section names each time.
The proper way would be sorting the results when you call parent->get_sections(). How you would do this is entirely up to the implementation of that class and method. Changing this foreach to for for the sake of sorting seems like a code smell to me.
For the sake of answering the question as technical as possible.
$sections = $this->parent->get_sections(null, $this->parent->author);
$num_sections = count($sections);
for ($i = 0; $i < $num_sections; $i++) {
// what you do here is up to you $sections[$i]
}
Especially if you are not aware of the specific number of sections, you could use usort() to do a dynamic custom sort on the get_sections()-returned array or object and then utilize the existing code. (This is a little more elegant, imo, than doing the same in a for/foreach loop).
Not knowing the structure of your code, I would do something like.
// Get Org Sections
$sections = $this->parent->get_sections(null, $this->parent->author);
// Loop thru sections to get an array of names
foreach ( $sections as $key=>$section)
{
$sorted_sections[$section->name] = $key;
}
// Sort Array
//ksort — Sort an array by key
//krsort — Sort an array by key in reverse order
krsort($sorted_sections);
foreach ( $sorted_sections as $section)
{
// Orig Code
}
$section = $this->parent->get_sections(null, $this->parent->author);
echo $section[2]->name;
echo $section[1]->name;//just output the indexes the way you want
if you want it sorted, in say descending order, you can sort it that way and then use a for loop to display.
Im some array that's being returned by some query.. and the result is something like this:
array(array('balance_1'=> '-5', 'balance_2'=>'-21'), array('balance_1'=> '-21', 'balance_2'=>'21'), array('balance_1'=> '-50', 'balance_2'=>'40'))
i want to transform this into an array that looks something like this:
array(array(-5,11,-50), array(-21, 21, 40));
basicly i want to join all balance_1, all balance_2, all balance_3 into separated arrays.
any ideas? thanks
You'll just loop over the list, then collect the values. It's most simple if you reuse the existing keys to group:
foreach ($list as $row) {
foreach ($row as $key=>$value) {
$out[$key][] = $value;
}
}
This way you'll get an $out array, with [balance_1] or [balance_2] holding the value lists.
Loop though the array and use "array_key_exists" if the key exists add to the array, if it doesn't build a new array with your index.
For more can be found here:
http://www.php.net/manual/en/function.array-key-exists.php