I need to enconde just the first column of an array with n rows,
Array
(
[0] => Array
(
[FamilyName] => Dikkartan, Bartu
[Balance] => -2446.91
[Mobile] => 0444497
[HAddress] => 2/6 Tramsway Street
[HSuburb] => Rosebery
[HState] => NSW
[HPCode] =>
)
[1] => Array
(
[FamilyName] => King, Alan & Luka
[Balance] => -1676
[Mobile] => 0433 6090
[HAddress] => 46/12 Hayberry Street
[HSuburb] => Crows Nest
[HState] => NSW
[HPCode] =>
) ...
so I need to make an array just with the first value [familyName],
- I want to have this to use with a jquery autocomplete, so after having the name selected from autocomplete, I use this name to search again in the complete array and fill some textfields with the data from the array
I need to search in the array as data is taken from sql, but I cannot search in the db, just use the array data,
Is this a good path or what other path should I take to do some autocomplete for the name and then have my textfields filled with the data from that array
thanks a lot!
Using array_map you can reduce the array to just that element.
$familyNames = array_map(function($object) {
return $object['FamilyName'];
}, $objects);
json_encode($familyNames);
This uses an anonymous function available in php 5.3 otherwise you need to use create_function or a reference to an existing function you've created. See the PHP callback documentation.
create_function('$object', 'return $object["FamilyName"];')
Alternatively you could use a simple foreach loop:
$familyNames = array();
foreach($objects as $object)
$familyNames[] = $object['FamilyName'];
Related
I have a complex multi-dimensional array that looks something like
[name] => Marko Polo
[description] => New application
[number] => ABCD1234
[loans] => Array
(
[0] => Array
(
[id] => 123
[application_id] => 456
[loan_fees] => Array
(
)
[loan_parts] => Array
(
[0] => Array
(
[id] => 987
[loan_id] => 123
[product_id] => 49788
[product] => Array
(
[id] => 49788
[lender] => MAC
...
I need to create an efficient way of traversing this array and, for example having a set of rules to filter/modify the data.
For example, in the array there is [lender] => MAC, I want to have something like
loans.loan_parts.product.lender.MAC = 'Macquarie'
This would be in a config of sorts such that if the data array changed, it would be simply a matter of changing that dot notation to point to the new location of the lender value.
Using this, I need to filter the lender and modify it to be Macquarie instead of Mac.
I know that a big no-no these days is using too many foreach loops and I've looked into Collections, but because the inner arrays are not named, I don't believe Collections is possible.
As I say, I'd like to avoid the situation of
foreach
foreach
if (is_array())
foreach
eeewww!
How can I execute this in the most efficient manner due to the possible large size of the array and its complexity.
You can use array_walk_recursive with callback that will change behavior according to key of array.
<?php
//can pass variable by reference to change array in function
function handleMAC(&$item, $key)
{
if($key == 'lender'){
$item['MAC'] = 'your value';
}
}
array_walk_recursive($array, 'handleMAC');
?>
I have an array like below
Array
(
[0] => '13-Nov'
[1] => 'PUJA SUNUWAR'
[2] => '13-Nov'
[3] => '...301303'
[4] => 'TT1331600004\DLG'
[5] => '-10000.00'
[6] => '0'
[7] => '90000.00'
)
I need to remove 4th item of array and save it as
Array
(
[0] => '13-Nov'
[1] => 'PUJA SUNUWAR'
[2] => '13-Nov'
[3] => 'TT1331600004\DLG'
[4] => '-10000.00'
[5] => '0'
[6] => '90000.00'
)
i don't want to iterate over each elements of array. Is there any one shot function like array_pop to remove nth element of array?
use array_splice($array, 3, 1);
http://php.net/manual/en/function.array-splice.php
Is this a 2d-array? If so:
No, there is no built in function to do this. You could use ´array_walk´ with a custom callback but I doubt it would be faster than a simple foreach.
Else (if normal array):
unset( $aData[3] );
$aData = array_values( $aData );
Wich is faster then array_splice.
As mentioned above, there is no build in function for that.
You should use unset if you dont need to care about array indexing and array_values if should.
Also you should use value reference while array iterating to prevent internal array copyng while values modification. If dont, it can be one of perfomance break down reasons.
foreach ($yourDataSet as &$value) {
unset($value[2]);
//$value = array_values($value);
}
You shouldt use array_splice becouse of perfomance reasons.
p.s. Also you could check phpbench for perfomance test about different types of array iteration.
I have something like this
Array
(
[0] => stdClass Object
(
[CustomerID] => 14
[Email] => joe.blogs#example.com
[LastName] => Blogs
[BirthDayOfMonth] => 29
[Gender] =>
[Occupation] =>
[SendSpecialOffers] => 1
[SendReminderNotes] => 1
)
[1] => stdClass Object
(
[CustomerID] => 1460
[Email] => example#example.com
[LastName] => Example
[BirthDayOfMonth] => 5
[Gender] => F
[Occupation] =>
[SendSpecialOffers] => 1
[SendReminderNotes] => 1
)
);
I would like get Email address of each separated by commas, something like this
'joe.blogs#example', 'example#example.com'
I know i could iterate it through foreach but i got a really big list, is there anyway to do it faster? thanks
Now, how can i remove the indexes based some email addresses?
You can do this with array map and a function but this will also iterate your array
echo implode(',',array_map('getEmail',$array));
function getEmail($obj)
{
return $obj->Email;
}
The simplest solution would indeed be a foreach() to iterate over all the items of your array ; adding, for each item, the email to a another resulting array.
Maybe you could replace the foreach by a call to array_walk(), but it probably wouldn't change much :
You wouldn't loop in PHP, as array_walk is coded in C (could be a bit faster than foreach -- not sure, though)
But a function would be called for each item, instead of just a couple of PHP instructions.
You'd have to benchmark, to see if there is a significant difference in your specific case -- but I personnaly would go for the foreach, without thinking much more.
array_filter is best..see the examples on manual
i have a form with some checkboxes. if i activate a ceckbox, jquery is sending the data with the .serialize() function to a php file via ajax. The problem is, that jquery send some double parameters. Here is the Query:
area=26-50&area=51-75&area=76-100&area=100&std=1&std=3
How can i create a array like this:
array(
'area' => array(0 => '26-50',1 => '51-75',2 => '76-100'), std => array(0 => 1,1 => 3)
)
PHP overwrites the last variable with a new one...
Thanks for the help!
greetings
[] notation will make it possible to transmit array data in a form.
Name the checkboxes in the form like this:
<input name="area[]" type="checkbox" value="51-75">
this should build an array of all selected check boxes.
PHP can support this if the key name is appended with []:
area[]=26-50&area[]=51-75&area[]=76-100&area[]=100&std[]=1&std[]=3
/*
Array
(
[area] => Array
(
[0] => 26-50
[1] => 51-75
[2] => 76-100
[3] => 100
)
[std] => Array
(
[0] => 1
[1] => 3
)
)
*/
Been kind of stuck on this one for a while now, so any help would be appreciated. I have one array (left) that contains a list of elements, the goal is to sort another arrays (right) keys with the values from the left array.
The left array
Array
(
[0] => ID
[1] => FirstName
[2] => LastName
[3] => Address
)
The right array
Array
(
[0] => Array
(
[FirstName] => Pim
[Address] => Finland
[LastName] => Svensson
[ID] => 3
)
[1] => Array
(
[FirstName] => Emil
[Address] => Sweden
[LastName] => Malm
[ID] => 5
)
)
What I'm trying to accomplish would be similar to this
Array
(
[0] => Array
(
[ID] => 3
[FirstName] => Pim
[LastName] => Svensson
[Address] => Finland
)
Anyone? :)
Oh, I'm running php 5.3, if it helps!
$output = array();
foreach ( $right as $array ) {
foreach ( $left as $field ) {
$temp[$field] = $array[$field];
}
$output[] = $temp;
}
You can use uksort() which lets you sort array keys by a user defined function. E.g.:
function sort_by_array($array) {
// create a sorting function based on the order of the elements in the array
$sorter = function($a, $b) use ($array) {
// if key is not found in array that specifies the order, it is always smaller
if(!isset($array[$a])) return -1;
if($array[$a] > $array[$b]) {
return 1;
}
return ($array[$a] == $array[$b]) ? 0 : -1;
};
return $sorter;
}
// $array contains the records to sort
// $sort_array is the array that specifies the order
foreach($array as &$record) {
uksort($record, sort_by_array(array_flip($sort_array)));
}
I make use of the possibility in 5.3 to define functions dynamically and I use array_flip() to transform:
Array
(
[0] => ID
[1] => FirstName
[2] => LastName
[3] => Address
)
to
Array
(
[ID] => 0
[FirstName] => 1
[LastName] => 2
[Address] => 3
)
This makes it easier to compare the keys later.
You must explode the array
Store the array in a variable like this
$array = Array
(
[0] => Array
(
[ID] => 3
[FirstName] => Pim
[LastName] => Svensson
[Address] => Finland
);
and then explode the array
after exploding the array you will get the parameters of the array seperated then you can use implode function the arrange them in anyorder as you wish
I'd take a step back and look at what you really need to do. The array is associative, so you can access the correct element instantly, right? So you don't really need it to be in order, unless you print output with foreach.
I'd suggest one of the following solutions:
If you need the "right" array to be in key-order, then look at the database query / similar and select the columns in the order you need them.
Foreach person you want to print, look up the order in the "left" array, then print the corresponding value of that key in the "right" array.
Well, your question it's uncommon, usually the associative arrays are used to resolve any problems about "position".
Anyway, there are many way to do what you are looking for what are you looking for.
You can use list() but it's position based:
foreach($oldArray as $o)
{
list($firstName,$lastName,$address,$id)=$oldArray;
$newArray[]=array($id,$firstName,$lastName,$address);
}
But the best way to solve your problem it's fill the array directly in the right order instead of re-sort after :)