php - how to use multiple array in foreach [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
foreach with three variables add
i have following code i want to use multiple array in foreach. please note that i need $yourSiteContent[] as it is.
foreach ($pieces as $id and $pieces1 as $id_date) {
$yourSiteContent[] = array('permalink' => $id, 'updated' => $id_date);
}
as you can see i have two arrays ($pieces and $pieces1), please check a loop and let me know where i am doing mistake.
$pieces1 contains dates and $pieces contains urls.
thanks for your help.

Assuming both $pieces and $pieces1 have the same keys you can do the following:
for (array_keys($pieces) as $key) {
$yourSiteContent[] =
array('permalink' => $pieces[$key], 'updated' => $pieces2[$key]);
}

You should use a for loop and an indexer, then access each array.

To keep to the foreach format you can also do it like (Assuming both $pieces and $pieces1 have the same keys):
$yourSiteContent = array();
foreach ($pieces as $key=>$value) {
$yourSiteContent[] = array('permalink'=>$value,
'updated'=>$pieces1[$key]);
}

You can use array_combine() to combine the two arrays into a single array, with one being used for keys and the other for values.
foreach (array_combine($pieces, $pieces1) as $id => $id_date) {
$yourSiteContent[] = array('permalink' => $id, 'updated' => $id_date);
}
See array_combine() for more information.

Related

Using array_diff for broad matches? [duplicate]

This question already has answers here:
Multiple array_filter and strpos
(4 answers)
Closed 3 years ago.
What I'm trying to do is take items from one array, such as:
$array1 = array(
"google-com",
"youtube-com",
);
And remove items from a second array if the above items are included (but BROAD match, not exact).
$array2 = array(
"www-google-com",
"www-youtube-com",
"www-facebook-com",
"www-twitter-com",
);
Expected output:
www-facebook-com
www-twitter-com
Note: The first array would be with "example.com" style URLs and the second with "https://www.example.com/" URLs.
It seems array_diff only works with exact matches, and after much searching, I can't seem to find a way to make it work for broad matches.
Thanks for your help!
The simplest way to do this is to iterate over each of the arrays, using a function such as strpos to see if the short URLs are contained in the longer ones:
$output = array();
foreach ($array2 as $url) {
$found = false;
foreach ($array1 as $short_url) {
$found = $found || (strpos($url, $short_url) !== false);
}
if (!$found) {
$output[] = $url;
}
}
print_r($output);
Output:
Array
(
[0] => www-facebook-com
[1] => www-twitter-com
)
Without knowing exactly what you mean by a BROAD match, strpos is probably close. You can always write a custom function to do the matching and replace strpos in the code above with it.
When making iterated searches, always provide an early exit.
Nested loops with early break conditions will be most performant.
Code: (Demo)
$array1 = array(
"google-com",
"youtube-com",
);
$array2 = array(
"www-google-com",
"www-youtube-com",
"www-facebook-com",
"www-twitter-com",
);
foreach ($array2 as $index => $haystack) {
foreach ($array1 as $needle) {
if (strpos($haystack, $needle) !== false) {
unset($array2[$index]);
break;
}
}
}
var_export(array_values($array2));
That said, if your data is somewhat predictable and you can prepare just one of the arrays, you can spare much of this iterated work.
You're right, array_diff is not a solution here. One of the solutions is using a preg_grep to find records and then unset keys in $array2:
$array1 = array(
"google-com",
"youtube-com",
);
$array2 = array(
"www-google-com",
"www-youtube-com",
"www-facebook-com",
"www-twitter-com",
);
foreach ($array1 as $search) {
foreach (preg_grep('/' . $search . '/', $array2) as $index => $value) {
unset($array2[$index]);
}
}
print_r($array2);
Hope this resolves your answer to the question :)
$array1 = array(
"google-com",
"youtube-com",
);
$array1 = preg_filter('/^/', 'www-', $array1);
$array2 = array(
"www-google-com",
"www-youtube-com",
"www-facebook-com",
"www-twitter-com",
);
print_r(array_diff($array2, $array1));

How to allow same keys in one array? [duplicate]

This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed 4 years ago.
I need to add the same keys to the array, but with different values,
foreach ($selections as $selection) {
$array += [$selection['option_id']=>$selection['product_id']];
}
// example output
$array = [30=>12,14=>10],
but really it should be
[30=>7,30=>12,14=>10];
When the key repeats, it merges.
You just can't.
But you can make the value of this key an array.
So you'll have
$array = [30=>[7,12],14=>10];
You can use any array functions on $array[30]
What you should do is to return the products ids as an array:
$array = array_reduce($selections, function ($carry, $selection) {
if (!isset($carry[$selection['option_id']])) {
$carry[$selection['option_id']] = [];
}
$carry[$selection['option_id']][] = $selection['product_id'];
return $carry;
}, []);
Now the result would be:
[30 => [7, 12], 14 => [10]];
Keys in array are, as the word itself says, keys to access the value they contain and each key must be unique, else you won't have a way to . If you could have two time or more the same value, how could you tell which will access one value and which one will access the other one? To solve your problem you have a way: generate a multidimensional array such that you can have multiple value stored "behind" a single key. E.g. [30 => [7,12], 14 => 10]
Based on your code you can just create a double loop with a nested foreach to navigate through all the value, something like:
foreach ($selections as $selection) {
if(!is_array($selection['product_id']) $array += [$selection['option_id']=>$selection['product_id']];
else {
foreach ($selection['product_id'] as $product) {
$array += [$selection['option_id']=> product];
}
}
}

check inside array if the value sperated by comman [duplicate]

This question already has answers here:
How to explode an array into another array and not subarray using PHP?
(3 answers)
Closed 9 months ago.
Results in array as below,
How can I convert the above array as below:
array:3[
0=>"11856"
1=>"12235"
2=>"11843"
So if any value is separated by comma, remove from that index array and add another index with removed value.
I have tried logic as below:
foreach($domains as $row){
$domain = explode(',',$row);
$row = $domain;
}
No Luck, Any better approach?
Hope make sense. Thanks in advance for all.
Just loop through your source array and explode it with , character. And after explode it return Array, just loop this array push it add to your return array
function convert_array($data) {
$ret = array();
foreach($data as $d) {
$tmp = explode(',', $d);
foreach($tmp as $t) {
$ret[] = $t;
}
}
return $ret;
}
$data = array("11856,12235,113", "11843");
var_dump(convert_array($data));
and output is
array(3) {
[0] => 11856
[1] => 12235
[2] => 113
[3] => 11843
}
Thanks for all your answers,
I manage to achieve the requirement just in one line of code as below.
$domain = explode(",",implode(",",$domains));
First Implode by comma, Then explode the result. Done the magic.
I have a solution as per your question. This is very simple and easy solution. I used your code for solve your problem.
$domains = array(0=>"11856,12235", 1=>"11843");
foreach($domains as $row){
$domain = explode(',',$row);
foreach($domain as $d){
$a[] = $d;
}
}
echo '<pre>';
print_r($a);
Result:
Array
(
[0] => 11856
[1] => 12235
[2] => 11843
)

PHP foreach change array value [duplicate]

This question already has answers here:
How to modify an array's values by a foreach loop?
(2 answers)
Closed 4 months ago.
When you have a foreach loop like below, I know that you can change the current element of the array through $array[$key], but is there also a way to just change it through $value?
foreach($array as $key => $value){
}
It's probably really simple, but I'm quite new to PHP so please don't be annoyed by my question :)
To be able to directly assign values to $value, you want to reference $value by preceding it with & like this:
foreach($array as $key => &$value){
$value = 12321; //the same as $array[$key] = 12321;
}
unset($value);
After the foreach loop, you should do unset($value) because you're still able to access it after the loop.
Note: You can only pass $value by reference when the array is a variable. The following example won't work:
foreach(array(1, 2, 3) as $key => &$value){
$value = 12321; //the same as $array[$key] = 12321
}
unset($value);
The php manual on foreach loops
there's a function for that, and is builtin since early version of PHP, is called array_map

unsetting an array within an array

In the screenshot, you can see i have an array of arrays. I need to find an array that contains say, 'Russia', and unset it completely. That is, for Russia, remove the element [303].
I've played around with array search but I'm sure theres a funkier way of doing this.
Chris.
$array = your array;
foreach ($array as $key => $value) {
if ($value['countryName'] == 'Russia') {
unset($array[$key]);
}
}
and if you want reorder the key , you could use:
$new_array = array_values($array);

Categories