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
Related
How to update an array of objects, adding the quantities if you already have the same ID, or if you have not created a new object.
I tried to explain in the code with the arrays and also with the idea of how I would like the result to be.
old Array
$a1 = [
array(
"id" => 1,
"qty" => 1
),
array(
"id" => 2,
"qty" => 1
)
];
$a2 = [
array(
"id" => 1,
"qty" => 1
)
];
$output = array_merge($a1, $a2);
echo '<pre>';
print_r($output);
echo '</pre>';
Result Error:
Array
(
[0] => Array
(
[id] => 1
[qty] => 1
)
[1] => Array
(
[id] => 2
[qty] => 1
)
[2] => Array
(
[id] => 1
[qty] => 1
)
)
What I need, in addition to if the ID does not contain, add.
Array
(
[0] => Array
(
[id] => 1
[qty] => 2
)
[1] => Array
(
[id] => 2
[qty] => 1
)
)
You can take the first array as base, then search for the key (if existing) where the product matches the id. Then either add the quantity and recalculate the price or you just add the reformatted element (id to product conversion).
$result = $a;
foreach($b as $element) {
$matchingProductIndex = array_search($element['id'], array_column($a, 'product'));
if ($matchingProductIndex !== false) {
$pricePerUnit = $result[$matchingProductIndex]['price'] / $result[$matchingProductIndex]['qty'];
$result[$matchingProductIndex]['qty'] += $element['qty'];
$result[$matchingProductIndex]['price'] = $result[$matchingProductIndex]['qty'] * $pricePerUnit;
} else {
$result[] = [
'qty' => $element['qty'],
'product' => $element['id'],
'price' => $element['price'],
];
}
}
print_r($result);
Working example.
Loop through both arrays with foreach and check the ids against each other.
https://paiza.io/projects/lnnl5HeJSFIOz_6KD6HRIw
<?php
$arr1 = [['qty' => 4, 'id' => 4],['qty' => 1,'id' => 30]];
$arr2 = [['id' => 30, 'qty' => 19],['id' => 31, 'qty' => 2]];
$arr3 = [];
foreach($arr1 as $iArr1){
$match = false;
foreach($arr2 as $iArr2){
if($iArr1['id'] === $iArr2['id']){
$arr3[] = ['id' => $iArr1['id'], 'qty' => $iArr1['qty'] + $iArr2['qty']];
$match = true;
}
}
if(!$match){
$arr3[] = $iArr1;
$arr3[] = $iArr2;
}
}
print_r($arr3);
?>
One approach could be one I more often suggested.
First lets merge $a2 with one to simplify looping over one larger collection.
If we then create a small mapping from id to its index in the result array we can update the running total of qty.
$map = [];
$result = [];
// Merge the two and do as per usual, create a mapping
// from id to index and update the qty at the corresponding index.
foreach (array_merge($a1, $a2) as $subarr) {
$id = $subarr['id'];
if (!key_exists($id, $map)) {
$index = array_push($result, $subarr) - 1;
$map[$id] = $index;
continue;
}
$result[$map[$id]]['qty'] += $subarr['qty'];
}
echo '<pre>', print_r($result, true), '</pre>';
Output:
Array
(
[0] => Array
(
[id] => 1
[qty] => 2
)
[1] => Array
(
[id] => 2
[qty] => 1
)
)
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";
}
}
I have an array of arrays set up like so. There are a total of 10 arrays but I will just display the first 2. The second column has a unique id of between 1-10 (each only used once).
Array
(
[0] => Array
(
[0] => User1
[1] => 5
)
[1] => Array
(
[0] => User2
[1] => 3
)
)
I have another array of arrays:
Array
(
[0] => Array
(
[0] => 3
[1] => 10.00
)
[1] => Array
(
[0] => 5
[1] => 47.00
)
)
where the first column is the id and the second column is the value I want to add to the first array.
Each id (1-10) is only used once. How would I go about adding the second column from Array#2 to Array#1 matching the ID#?
There are tons of ways to do this :) This is one of them, optimizing the second array for search and walking the first one:
Live example
<?
$first_array[0][] = 'User1';
$first_array[0][] = 5;
$first_array[1][] = 'User2';
$first_array[1][] = 3;
$secnd_array[0][] = 3;
$secnd_array[0][] = 10.00;
$secnd_array[1][] = 5;
$secnd_array[1][] = 47.00;
// Make the user_id the key of the array
foreach ($secnd_array as $sca) {
$searchable_second_array[ $sca[0] ] = $sca[1];
}
// Modify the original array
array_walk($first_array, function(&$a) use ($searchable_second_array) {
// Here we find the second element of the first array in the modified second array :p
$a[] = $searchable_second_array[ $a[1] ];
});
// print_r($first_array);
Assuming that 0 will always be the key of the array and 1 will always be the value you'd like to add, a simple foreach loop is all you need.
Where $initial is the first array you provided and $add is the second:
<?php
$initial = array(array("User1", 5),
array("User2", 3));
$add = array(
array(0, 10.00),
array(1, 47.00));
foreach ($add as $item) {
if (isset($initial[$item[0]])) {
$initial[$item[0]][] = $item[1];
}
}
printf("<pre>%s</pre>", print_r($arr1[$item[0]], true));
I don't know if I got you right, but I've come up with a solution XD
<?php
$array_1 = array(
0 => array(
0 => 'ID1',
1 => 5
),
1 => array(
0 => 'ID2',
1 => 3
)
);
$array_2 = array(
0 => array(
0 => 3,
1 => 10.00
),
1 => array(
0 => 5,
1 => 47.00
)
);
foreach($array_1 as $key_1 => $arr_1){
foreach($array_2 as $key_2 => $arr_2){
if($arr_2[0] == $arr_1[1]){
$array_1[$key_1][2] = $arr_2[1];
}
}
}
var_dump($array_1);
?>
Demo: https://eval.in/201648
The short version would look like this:
<?php
$array_1 = array(array('ID1',5),array('ID2',3));
$array_2 = array(array(3,10.00),array(5,47.00));
foreach($array_1 as $key => $arr_1){
foreach($array_2 as$arr_2){
if($arr_2[0] == $arr_1[1]){
$array_1[$key][2] = $arr_2[1];
}
}
}
var_dump($array_1);
?>
Demo: https://eval.in/201649
Hope that helps :)
A quick and dirty way just to show you one of the more self-explaining ways to do it :)
$users = array(
0 => array(
0 => 'User1',
1 => 123
),
1 => array(
0 => 'User2',
1 => 456
)
);
$items = array(
0 => array(
0 => 123,
1 => 'Stuff 1'
),
1 => array(
0 => 456,
1 => 'Stuff 2'
)
);
foreach($items as $item){
foreach($users as $key => $user){
if($item[0] == $user[1])
array_push($users[$key], $item[1]);
}
}
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
)
)