Merge multiple arrays by index - php

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

Related

how to get only array values into another array

need to get only array values into an array.
i have array like.
and want to convert into another array like.
array('pic','picc','topic');
i have tried this but it gives me same result
$new_array = array();
foreach($tags as $val)
{
array_push($new_array, $val);
}
print_r($new_array);
You have to use implode() for your query and change the way you implement it:
$tags = implode("','", $array);
$this->db->query("UPDATE discuss_tags SET TotalPosts=TotalPosts-1 WHERE Name in ('".$tags."')");
The query will look like this:
UPDATE discuss_tags SET TotalPosts=TotalPosts-1 WHERE Name in ('pic','picc','topic')
PHP Manual: Implode
array('pic','picc','topic'); such an array only exists in pseudocode, not in PHP.
Arrays in PHP will always have numeric indexes unless you force string indexes.

Transfer keys from a 2D array to fill a 1D array

PHP has plenty of useful functions and Im wondering if Im overlooking one that has already been built.
Lets say you have an array such as:
$first_array = array("Name"=>"Angela", "Age"=>24);
and you wanted to grab the keys from the first array to create a second array (which could then be pushed into a third array). So you need to create:
$second_array = array("Name", "Age");
Is there a way to achieve this result without this loop?:
foreach($first_array as $k=>$v){
array_push($second_array, $k);
}
This should do it:
array_keys($first_array);
Use array_keys($first_array) to get the array of all the keys in the $first_array

How to remap array keys (based on values) with PHP?

<?php
// get unordered array of Group objects
$groups = $this->getDoctrine()->getManager()->getRepository('AcmeDemoBundle:Group');
$array = array();
// map array to $groupName => $groupObject
foreach ($groups as $group) {
$array[$group->getName()] = $group;
}
Is there a shorter way to do that in PHP?
Note: Group names are unique, not empty strings.
I was thinking about array_walk, but I'm not sure if can replace the key somehow?
It seems there is no shorter/better way than the one posted in the question.

Storing array within an array using PHP

foreach ($topicarray as $key=>$value){
$files = mysql_query("mysqlquery");
while($file = mysql_fetch_array($files)){ extract($file);
$topicarray[$value] = array( array($id=>$title)
);
}
}
The first foreach loop is providing me with an array of unique values which forms a 1-dimensional array.
The while loop is intended to store another array of values inside the 1-dimensional array.
When the while loop returns to the beginning, it is overwriting it. So I only ever get the last returned set of values in the array.
My array ends up being a two dimensional array with only one value in each of the inner arrays.
Feels like I'm missing something very basic here - like a function or syntax which prevents the array from overwriting itself but instead, adds to the array.
Any ideas?
Step 1. Replace $topicarray[$value] with $topicarray[$value][]
Step 2. ???
Step 3. Profit
Make $topicarray[$value] an array of rows, instead of one row. Also, don't use extract here.
foreach ($topicarray as $key => $value) {
$rows = array();
$files = mysql_query("mysqlquery");
while($file = mysql_fetch_array($files)) {
$rows[] = array($file['id'] => $file['title']);
}
$topicarray[$value] = $rows;
}
Also, you should switch to PDO or MySQLi.

convert numeric array to associative using another array for keys

I have a csv table, with the first row as the header. I would like to iterate through all the rows, using the name of the column to refer to the column instead of its numerical value. To do this, I think as I iterate I need to convert each numerically indexed row into an associative one, but I can't figure out the best way to do this.
$headerrow = str_getcsv($table[0]); //gives me an array like 0=>foo,1=>bar,2=>bat
foreach ($table as $rownumber=>$row){
if($rownumber!=0){
$rowarray=str_getcsv($row);//gives me an array like 0=>blah,1=>blah,2=>blah
//how do I get $rowarray['foo'] or $rowarray['bar'] most efficiently?
}
}
Guess I should have looked more closely at the array functions. I found array_combine.
$rowarray = array_combine($headerrow,str_getcsv($row));
print($rowarray['foo']);

Categories