This loop
$demo = array();
for($i=0;$i<count($big_array);$i++){
echo 'Page['.$i.'][0]: '.$big_array[$i][0].'<br>';
for($j=1;$j<count($big_array[$i]);$j++){
echo 'Email['.$i.']['.$j.']: '.$big_array[$i][$j].'<br>';
$demo[$big_array[$i][$j]][] = $big_array[$i][$j-1]; //something is not ok with this
}
}
gives me this:
Page[0][0]: http://www.example.com/impressum
Email[0][1]: sales#example.com
Email[0][2]: support#example.com
Page[1][0]: http://www.example.com/termsofuse
Email[1][1]: support#example.com
Email[1][2]: terms1#example.com
Email[1][3]: terms2#example.com
Email[1][4]: ad2#example.com
Page[2][0]: http://www.example.com/adpolicy
Email[2][1]: support#example.com
Email[2][2]: ad1#example.com
Email[2][3]: ad2#example.com
Email[2][4]: ad1#example.com
How can I transform it to get this result:
sales#example.com
http://www.example.com/impressum
support#example.com
http://www.example.com/impressum
http://www.example.com/termsofuse
http://www.example.com/adpolicy
terms1#example.com
http://www.example.com/termsofuse
terms2#example.com
http://www.example.com/termsofuse
ad2#example.com
http://www.example.com/termsofuse
http://www.example.com/adpolicy
ad1#example.com
http://www.example.com/adpolicy
var_dump($big_array):
array ( 0 => array ( 0 => 'http://www.example.com/impressum', 1 => 'sales#example.com', 2 => 'support#example.com', ), 1 => array ( 0 => 'http://www.example.com/termsofuse', 1 => 'support#example.com', 2 => 'terms1#example.com', 3 => 'terms2#example.com', 4 => 'ad2#example.com', ), 2 => array ( 0 => 'http://www.example.com/adpolicy', 1 => 'support#example.com', 2 => 'ad1#example.com', 3 => 'ad2#example.com', 4 => 'ad1#example.com', ), )
$array = array ( 0 => array ( 0 => 'http://www.example.com/impressum', 1 => 'sales#example.com', 2 => 'support#example.com', ), 1 => array ( 0 => 'http://www.example.com/termsofuse', 1 => 'support#example.com', 2 => 'terms1#example.com', 3 => 'terms2#example.com', 4 => 'ad2#example.com', ), 2 => array ( 0 => 'http://www.example.com/adpolicy', 1 => 'support#example.com', 2 => 'ad1#example.com', 3 => 'ad2#example.com', 4 => 'ad1#example.com', ), );
print_r($array);
$final = array();
foreach ( $array as $group )
{
for ( $i=1; $i<count($group); $i++ )
{
$final[$group[$i]][] = $group[0];
}
}
print_r($final);
Here is the PHP Playground result.
To format it like your example:
foreach ( $final as $email => $links )
{
echo $email . "\n";
foreach ( $links as $link )
{
echo " " . $link . "\n";
}
}
Related
I have an array with some repeating string values. How to replace these string values (as a whole, because some words are repeated in others strings) with corresponding specific numeric values, as bellow?
deloc = 1
foarte puţin = 2
mediu = 3
mult = 4
foarte mult = 5
This is the array (example):
array = (
"tensionat" => "mediu",
"trist" => "mult",
"melancolic" => "deloc",
"fara_speranta" => "foarte puțin",
"nefolositor"] => "deloc",
"ingrijorat" => "foarte mult",
"amarat" => "deloc",
"anxios" => "mediu"
);
How can this
Try this
$data = array (
"tensionat" => "mediu",
"trist" => "mult",
"melancolic" => "deloc",
"fara_speranta" => "foarte puțin",
"nefolositor" => "deloc",
"ingrijorat" => "foarte mult",
"amarat" => "deloc",
"anxios" => "mediu"
);
$repl = array (
'deloc' => 1,
'foarte puţin' => 2,
'mediu' => 3,
'mult' => 4,
'foarte mult' => 5,
);
$result = array ();
foreach ($data as $key => $value) {
$result[$key] = !empty($repl[$value]) ? $repl[$value] : $value;
}
print_r($result);
Output:
Array
(
[tensionat] => 3
[trist] => 4
[melancolic] => 1
[fara_speranta] => foarte puțin
[nefolositor] => 1
[ingrijorat] => 5
[amarat] => 1
[anxios] => 3
)
I have the following code that I can't figure out how to echo each value randomly..
<?php
$c1 = array(
0 => '#d24726',
1 => '#bf3317'
);
$c2 = array(
0 => '#14826d',
1 => '#0d6856'
);
$c3 = array(
0 => '#624f87',
1 => '#534373'
);
$c4 = array(
0 => '#008198',
1 => '#006e87'
);
$c5 = array(
0 => '#08893e',
1 => '#067038'
);
$randArray = array($c1,$c2,$c3,$c4,$c5);
echo '<pre>'; print_r($randArray); echo '</pre>';
?>
Which gives the following output..
Array
(
[0] => Array
(
[0] => #d24726
[1] => #bf3317
)
[1] => Array
(
[0] => #14826d
[1] => #0d6856
)
[2] => Array
(
[0] => #624f87
[1] => #534373
)
[3] => Array
(
[0] => #008198
[1] => #006e87
)
[4] => Array
(
[0] => #08893e
[1] => #067038
)
)
I want $c1, $c2, $c3, $c4 or $c5 to be chosen randomly and then be able to use their values which are colors..
I tried rand_array which didn't work..
$r = array_rand($randArray);
echo $r[][0];
echo $r[][1];
It works you just have to use it like this:
(array_rand() returns the key, so you just have to use it for the first dimension of the array as key)
$r = array_rand($randArray);
echo $randArray[$r][0];
echo $randArray[$r][1];
For more information about array_rand() see the manual: http://php.net/manual/en/function.array-rand.php
And a quote from there:
When picking only one entry, array_rand() returns the key for a random entry
Your code is right but you will have to specify the key returned by the array_rand().Use the code below
<?php
$c1 = array(
0 => '#d24726',
1 => '#bf3317'
);
$c2 = array(
0 => '#14826d',
1 => '#0d6856'
);
$c3 = array(
0 => '#624f87',
1 => '#534373'
);
$c4 = array(
0 => '#008198',
1 => '#006e87'
);
$c5 = array(
0 => '#08893e',
1 => '#067038'
);
$randArray = array($c1,$c2,$c3,$c4,$c5);
$r = array_rand($randArray);
echo '<pre>'; print_r($randArray[$r]); echo '</pre>';
?>
Hope this helps you
How can I get [jobNo] using loop from array below?
Array
(
[date] => 2014-01-13
[totcomdraft] => 400
[comdraft] => 0
[0] => Array
(
[jobNo] => 1401018618
[dateType] => 1
[comdraft] => 200
)
[1] => Array
(
[jobNo] => 1401018615
[dateType] => 1
[comdraft] => 100
)
[2] => Array
(
[jobNo] => 1401018617
[dateType] => 1
[comdraft] => 100
)
)
Try this
foreach($array as $key=>$val){
if(is_array($val)){
echo $val["jobNo"];
echo "<br />";
}
}
for( $i = 0; $ < count($array); $i++ )
{
print $array[$i]['jobNo'] . "<br>";
}
try with array in-built function :-
$result_array=array_map(function($input_array)
{
return $input_array['desired_column'];
},$input_array_original
);
Use This
foreach($array as $key=>$val){
if(is_array($val)){ // check this value in array
echo $val["jobNo"];
echo "<br />";
}
}
This code should work. I have tested.
$array = array
(
'date' => '2014-01-13',
'totcomdraft' => 400,
'comdraft' => 0,
'0' => array
(
'jobNo' => 1401018618,
'dateType' => 1,
'comdraft' => 200
),
'1' => array
(
'jobNo' => 1401018615,
'dateType' => 1,
'comdraft' => 100
),
'2' => array
(
'jobNo' => 1401018617,
'dateType' => 1,
'comdraft' => 100
)
);
for($i=0; $i<3; $i++){
echo 'Job no:' . $array[$i]['jobNo']."<br>";
}
Output:
Job no:1401018618
Job no:1401018615
Job no:1401018617
I have an array which contains following values.
array(
'dates' => array(
(int) 0 => '2013-04-22',
(int) 1 => '2013-04-23',
),
'publisherName' => array(
(int) 0 => 'Comp1',
(int) 1 => 'Comp2',
),
'loaded' => array(
(int) 0 => (int) 2189,
(int) 1 => (int) 37,
),
'clicks' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
),
'ctr' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
)
)
What I want to produce is getting company based data on different dates like the array below.
How am I able to create an array which is like;
array (
'2013-04-22'=>array(
'publisherName'=>'Comp1',
'loaded'=>2189,
'clicks'=>0,
'ctr'=>0),
'2013-04-23'=>array(
'publisherName'=>'Comp2',
'loaded'=>37,
'clicks'=>0,
'ctr'=>0)
...
)
Which contains daily stats but which comes from publishername field.
Any ideas?
Here's an example that doesn't rely on any keys, the only requirement is that date is the first array in your original array:
// $array is your original array
$dates = array_shift($array);
$output = array();
foreach ($array as $k => $a) {
foreach ($a as $i => $v) {
$output[$i][$k] = $v;
}
}
$output = array_combine($dates, $output);
Let the initial array be $a and the desired array $b;
Code:
$b = array();
$count = 0;
foreach($a['dates'] as $date) {
$b[$date] = array(
'name' => $a['publisherName'][$count],
'loaded'=> $a['loaded'][$count],
'clicks'=> $a['clicks'][$count],
'ctr'=> $a['ctr'][$count]
);
$count++;
}
Result:
Array
(
[2013-04-22] => Array
(
[name] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[name] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
<?php
$data = $yourarray;
$new = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($new);
?>
$newArr = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($newArr);
$new_arr = your array;
$finalArray = array();
foreach($new_arr['dates'] as $key => $value){
$finalArray[$value] = array(
'publisherName'=>$new_arr['publisherName'][$key],
'loaded'=>$new_arr['loaded'][$key],
'clicks'=>$new_arr['clicks'][$key],
'ctr'=>$new_arr['ctr'][$key]
);
}
Output :
Array
(
[2013-04-22] => Array
(
[publisherName] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[publisherName] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
I have an array of objects.
What I need is to take each [name] of each object in put into another array, but I don't want duplicates.
How can I do it?
Array (
[0] => ADOFetchObj Object
(
[name] => Team 1
[att] => None
[idGrupo] => 1
[idModulo] => 4
[ler] => 1
[escrever] => 1
[excluir] => 1
)
[1] => ADOFetchObj Object
(
[name] => Team 1
[nomeModulo] => Aplicar Juros
[idGrupo] => 1
[idModulo] => 1006
[ler] => 1
[escrever] => 1
[excluir] => 1
)
[2] => ADOFetchObj Object
(
[name] => Team 2
[att] => None
[idGrupo] => 1
[idModulo] => 10
[ler] => 1
[escrever] => 1
[excluir] => 1
)
[3] => ADOFetchObj Object
(
[name] => Team 2
[att] => None
[idGrupo] => 1
[idModulo] => 1012
[ler] => 1
[escrever] => 1
[excluir] => 1
)
)
Thanks!
You can do this:
$names = array();
foreach($arr as $list) {
$names[$list->name] = true; // can be *any* arbitrary value
}
$names = array_keys($names);
This will work because by definition array keys have to be unique.
array_unique(array_map(function($element) {
return $element->name;
}, $my_array));
There you go
$res = array();
foreach($arr as $var)
{
if(!in_array($var->name, $res))
{
$res[] = $var->name;
}
}
First, copy the names to a new array:
$arrNames = array();
foreach($arrOriginal as $objObject) {
array_push(
$arrNames,
$objObject->name
);
}
Then remove the duplicate names:
$arrNames = array_unique($arrNames);
$n = array();
foreach($array as $d) $n[] = $d->name;
$n = array_unique($n);