Ok, I need an array to be outputted like this:
$sections = array(
5 => $americanFlag,
6 => $americanFlag,
22 => $russianFlag,
23 => $russianFlag,
24 => $russianFlag,
25 => $russianFlag,
);
Ofcourse it is much longer than this.
So, say I have an array like this:
$russian = array(22, 23, 24, 25);
$american = array(5, 6);
And arrays like this:
$americanFlag = 'http://pathtomyAmericanFlag.png';
$russianFlag = 'http://pathtomyRussianFlag.png';
How can I do this quick and easy??
There are probably a few ways to do it. Here's a simple one:
$russian = array_fill_keys($russian, $russianFlag);
$american = array_fill_keys($american, $americanFlag);
$sections = ksort(array_merge($russian, $american));
Assumes you want them sorted by key. If not, just remove the ksort()
$usFlags = array_combine($american, array_fill(0, count($american), $americanFlag);
$ruFlags = array_combine($russian, array_fill(0, count($russian), $russianFlag);
$sections = array_merge($usFlags, $ruFlags);
Should do it. However, I don't know, what you want to achieve, but it seems, that you want to output a flag depending on (I guess) an ID of something?
flags = array_merge(array_flip($russian), array_flip($american));
$helper = function ($id) use (flags) {
return isset($flags[$id]) ? $flags[$id] : null;
}
echo 'http://pathtomy'. ucfirst($helper($id)) . 'Flag.png'; // returns
You can use a multi-dimensional array:
array (
american => array ( [0] => american, [1] american );
russian => array ( [0] => russian, [1] russian );
Is that what you're wanting?
Something similar to this maybe.
$countries = array($russian, $american);
foreach($countries as $country){
foreach($country as $flag) {
echo $sections[$flag];
}
}
Try this
<?php
$russian = array();
$american = array();
foreach($sections as $key=>$value){
if($value == "http://pathtomyRussianFlag.png"){
array_push($russian, $key);
}
elseif($value == "http://pathtomyAmericanFlag.png"){
array_push($american, $key);
}
}
?>
Related
I have one array and i want to keep only blank values in array in php so how can i achieve that ?
My array is like
$array = array(0=>5,1=>6,2=>7,3=>'',4=>'');
so in result array
$array = array(3=>'',4=>'');
I want like this with existing keys.
You can use array_filter.
function isBlank($arrayValue): bool
{
return '' === $arrayValue;
}
$array = array(0 => 5, 1 => 6, 2 => 7, 3 => '', 4 => '');
var_dump(array_filter($array, 'isBlank'));
use for each loop like this
foreach($array as $x=>$value)
if($value=="")
{
$save=array($x=>$value)
}
if you want print then use print_r in loop
there is likely a fancy built in function but I would:
foreach($arry as $k=>$v){
if($v != ''){
unset($arry[$k]);
}
}
the problem is; you are not using an associative array so I am pretty sure the resulting values would be (from your example) $array = array(0=>'',1=>''); so you would need to:
$newArry = array();
foreach($arry as $k=>$v){
if($v == ''){
$newArry[$k] = $v;
}
}
since im newbie i have a question
well i have $nUserID where store user's id's and is like
int(1)
int(2)
int(1)
and in $nAuctionID i have items id and they are like
int(150022)
int(150022)
int(150031)
i need put it in 1 array and make it like
array (
[1] => 150022
[2] => 120022,150031
)
which user which item id watch
how to do that ?
i though should use foreach , but i cant imagine how will looks
start with
$u[] = $nUserID;
$i[] = $nAuctionID;`
Grouping them by user ID, the following should result in what you're looking for.
$usersWatching = array();
// The following results in:
// array(1 => array(150022, 150023), 2 => array(150022))
// Which should be way more useful.
foreach ($nUserID as $i => $userID)
{
if (!isset($usersWatching[$userID]))
$usersWatching[$userID] = array();
$usersWatching[$userID][] = $nAuctionID[$i];
}
// To get the comma-separated version, do this:
$usersWatching = array_map(function ($value) {
return implode(',', $value);
}, $usersWatching);
this will work :
$arr = array();
foreach( $nUserID as $key=>$value)
{
$arr[$value] = $nAuctionID[$key] ;
}
print_r($arr);
Most confusingly worded question EVA!
$outputArray = array();
for ($i=0; $i< count($nUserID); $i++) {
$outputArray[$nUserID[$i]] = $nAuctionID[$i];
}
echo "<pre>";
print_r($outputArray);
That is what I got from your question..
I have 2 arrays - the first one is output first in full. The 2nd one may have some values that were already used/output with the first array. I want to "clean up" the 2nd array so that I can output its data without worrying about showing duplicates. Just to be sure I have the terminology right & don't have some sort of "array within an array", this is how I access each one:
1st Array
$firstResponse = $sth->fetchAll();
foreach ($firstResponse as $firstResponseItem) {
echo $firstResponseItem['samecolumnname']; // Don't care if it's in 2nd array
}
2nd Array
while( $secondResponseRow = $dbRequest->fetch_assoc() ){
$secondResponseArray = array($secondResponseRow);
foreach ($secondResponseArray as $secondResponseItem){
echo $secondResponseItem['samecolumnname']; //This can't match anything above
}
}
Thanks!
For example:
$response_names = array();
$firstResponse = $sth->fetchAll();
foreach ($firstResponse as $firstResponseItem)
$response_names[] = $firstResponseItem['samecolumnname'];
while( $secondResponseRow = $dbRequest->fetch_assoc() ){
$secondResponseArray = array($secondResponseRow);
foreach ($secondResponseArray as $secondResponseItem) {
if (!in_array($secondResponseItem['samecolumnname'], $response_names))
$response_names[] = $secondResponseItem['samecolumnname'];
}
}
array_walk($response_names, function($value) { echo $value . '<br />' });
If I understand what you're looking to do and the arrays are in the same scope, this should work.
$secondResponseArray = array($secondResponseRow);
$secondResponseArray = array_diff($secondResponseArray, $firstResponse);
//secondResponseArray now contains only unique items
foreach ($secondResponseArray as $secondResponseItem){
echo $secondResponseItem['samecolumnname'];
}
If you know that the keys of duplicate values will be the same you could use array_diff_assoc to get the items of the first array that aren't in the other supplied arrays.
This code
<?php
$a = array('abc', 'def', 'ghi', 'adg');
$b = array('abc', 'hfs', 'toast', 'adgi');
$r = array_diff_assoc($b, $a);
print_r($a);
print_r($r);
produces the following output
[kernel#~]php so_1.php
Array
(
[0] => abc
[1] => def
[2] => ghi
[3] => adg
)
Array
(
[1] => hfs
[2] => toast
[3] => adgi
)
[kernel#~]
I want to change the value of an array without loop-
Example code:
<?php
//current array
$ids = array('1113_1', '1156_6', '1342_16', '1132_3', '1165_2');
//result should be looks like this
$ids = array('1113', '1156', '1342', '1132', '1165');
?>
is it possible to do it without any loop?
Instead of the shown string/array function workarounds, you can also just use a PHP built-in to filter the arrays:
$ids = array('1113_1', '1156_6', '1342_16', '1132_3', '1165_2');
$ids = array_map("intval", $ids);
This converts each entry into an integer, which is sufficient in this case to get:
Array
(
[0] => 1113
[1] => 1156
[2] => 1342
[3] => 1132
[4] => 1165
)
Try this using array_map():
<?php
function remove_end($n)
{
list($front) = explode("_", $n);
return $front;
}
$a = array('1113_1', '1156_6', '1342_16', '1132_3', '1165_2');
$a = array_map("remove_end", $a);
print_r($a);
?>
Demo: http://codepad.org/iGJ3cJW2
Possible? Yes:
$ids[0] = substr($ids[0], 0, -2);
$ids[1] = substr($ids[1], 0, -2);
$ids[2] = substr($ids[2], 0, -3);
$ids[3] = substr($ids[3], 0, -2);
$ids[4] = substr($ids[4], 0, -2);
But why do you want to avoid using a loop in this case?
array_map(), but internally it'd still be using a loop.
function $mycallback($a) {
... process $a
return $fixed_value;
}
$fixed_array = array_map('mycallback', $bad_array);
$data1 = "mathews,luks,john,ethan,neil";
$data2 = "80%,60%,78%,55%,62%";
want to combine both like
mathews = 80%
Luks = 60%
John = 78%
etc.... how to do this?? I want print result exactly like Mathews = 80%
http://us.php.net/manual/en/function.array-combine.php
$result = array_combine ( explode ( ',', $data1 ), explode ( ',', $data2 );
to extract:
if you know key name: echo $result ['john'];
if all: foreach ( $result as $key => $value ) { echo $key . ' : ' . $value; }
to gey list of keys: $list = explode ( ',', $data1 ) or $list = array_keys ( $result );
http://us.php.net/manual/en/function.array-keys.php
How about this...?
print_r(array_combine(explode(',',$data1),explode(',',$data2)));
$arr = array_combine(explode(',', $data1), explode(',', $data2));
Or
$names = explode(',', $data1);
$par = explode(',', $data2);
$arr = array();
foreach ($names as $idx=>$name)
{
$arr[$name] = $par[$idx];
}
Use the built in array_combine
You may consider using preg_split to the string:
$names = preg_split("/[,]+/", "mathews,luks,john,ethan,neil");
$percent = preg_split("/[,]+/", "80%,60%,78%,55%,62%");
Then you can use array_combine like many others suggest, or simply use 2 arrays $names and $percent along with the shared index, for example, $names[2] goes along with $percent[2].
As stated by bensui:
$data1 = "mathews,luks,john,ethan,neil";
$data2 = "80%,60%,78%,55%,62%";
$array = array_combine(explode(',',$data1), explode(',',$data2));
This will result in:
print_r($array);
Array
(
[mathews] => 80%
[luks] => 60%
[john] => 78%
[ethan] => 55%
[neil] => 62%
)
This will give you the ability easily update the assocaited scores simple by doing:
$array['mathews'] = '90%';
You can then use the array_slice I offered you earlier to discard john and ethan:
$newArray = array_slice($array, 2, 2);
This will give you:
print_r($newArray);
Array
(
[mathews] => 80%
[luks] => 60%
[neil] => 62%
)
Note: there is nothing wrong with using loops, the fact of the matter is they are often over used when there are methods built in to the language to solve many common problems.
If you are some kind of anti-loop fanatic, you could create a recursive function.
function no_loop_combine($data1,$data2,$index)
{
if($index == count($data1))
{
return '';
}
else
{
return $data1[$index] . ' = ' . $data2[index]
. '\n' . no_loop_combine($data1,$data2,$index + 1);
}
}
Then you just have to call your function specifying $index = 0, and you will get the result. Using array_combine probably is still using a loop, just masked by the fact that you are calling a function. Using recursion means there is no loop. Just realized that your initial input is a string, and not a loop. So you could call explode before passing it into the function as so:
no_loop_combine(explode(',', $data1 ),explode(',',$data2),0);