<?php
$codes = 'test1,test1,test3';
$names = '1226261693assistenza-pc-1-1.jpeg,1226261693cobinhood.png,1226261693a.png';
$array = array_combine ( explode(',',$codes), explode(',', $names) );
foreach($array as $k=>$v)
{
echo $k."<br>";
echo $v."<br>";
}
?>
How to get duplicate value in array combine function.
I would appreciate any help and tips.
Related
I have already try like this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values(array_column($arrayName,'5'));
print_r($arr);
?>
I just want to counting of repeated value
This script counts the amount of each value.
<?php
echo '<pre>';
$arrayName = [1,2,3,4,5,6,2,3,1];
$arr = [];
foreach ($arrayName as $item) {
if (empty($arr[$item]))
$arr[$item] = 0;
$arr[$item] += 1;
}
print_r($arr);
if you want count the values
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr =array_count_values($arrayName);
print_r($arr);
Why are you using array_column if you only want to count repeated values
you can do this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values($arrayName);
print_r($arr);
?>
How can I combine the following arrays? For example the first $match with the first $register and after that, to echo the array
$matches = array();
$registration = array();
preg_match_all('#(69\d{8}|21\d{8}|22\d{8}|23\d{8})#', $str2, $matches);
preg_match_all('!<td class="registration">(.*?)</td>!is', $str2, $registration);
foreach ($matches[1] as $match) {
echo $match.'<br>';
}
foreach ($registration[1] as $register) {
echo $register.'<br>';
}
Try with this example :
foreach (array_combine($matches[1], $registrations[1]) as $matche => $registration) {
echo $matche." - ".$registration;
}
and an other post like as your : Two arrays in foreach loop
You can loop through one and get the same key from the other array.
foreach ($matches[1] as $key=>$match) {
$register = $register[1][$key];
echo $match.' '.$register.'<br>';
}
may be this will help you out
$array = array();
foreach ($matches[1] as $key => $match) {
$array[] = array($match, $register[1][$i]);
}
var_dump($array);
you can use array_merge() function .
$combinedArray = array_merge($matches, $registration);
foreach ($combinedArray as $row) {
}
https://codeupweb.wordpress.com/2017/07/14/merging-and-sorting-arrays-in-php/
I have two array output (using preg_match_all), for example: $name[1] and $family[1].
i need to put these arrays together, and i use foreach as this:
foreach( $name[1] as $name) {
foreach( $family[1] as $family) {
echo $name.$family.'<br />';
}
}
but it don't work.
(each foreach loop works separately)
Assuming they have matching keys for the loop:
foreach( $name as $key => $value) {
echo $value[$key] . $family[$key] . '<br />';
}
This will go through each match for $name and print it out, and then print out the corresponding $family with it. I don't think you want to hardcode [1].
If you do, I'm a little confused and would like to see a var_dump of both $name and $family for clarification.
$together= array();
foreach( $name as $key => $n) {
$tuple= array('name'=>$name[$key],'family'=>$family[$key]);
$together[$key]= $tuple;
}
foreach( $together as $key => $tuple) {
echo "{$tuple['name']} {$tuple['family']}<br />";
}
Use array_combine():
Creates an array by using the values from the keys array as keys and
the values from the values array as the corresponding values.
PHP CODE:
$nameFamilly=array_combine($name[1] , $family[1]);
foreach( $nameFamilly as $name=>$familly) {
echo $name.$family.'<br />';
}
I have the following code of which I want to echo array elements separated by commas. The code outputs the disered list, but without commas. What am I missing?
<?php
$array = get_field('casts');
$elements = $array;
foreach($array as $key => $value) {
echo implode(', ', $value)};
?>
EDIT 1: where $elements are nested arrays.
EDIT 2: Working snippet:
<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
Why are you assigning $elements = $array; and then never using $elements?
Also you don't need to loop (foreach) to implode an array.
Try this:
<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
// this array_push() function adds $value to the end of $new_array.
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
Here is the documentation on implode()
You can play around and test the above code here.
Also next time, add the tag php, otherwise our codes won't get color syntax.
I have the following code:
foreach ($row as $item) {
foreach($item as $key) {
echo "<pre>";
print_r($key);
echo "</pre>";
}
}
I am trying to copy the keys ($key) into another array for further processing. How can i do this?
define some variable as array $array = array(); and just push the keys in with array_push($array, $key);
$array = array();
foreach ($row as $item) {
foreach($item as $key) {
array_push($array, $key);
}
}
$aNew = array();
foreach($row as $item) {
foreach($item as $key) {
$aNew[] = $key;
}
}
But; why would you do this? You can also just perform your commands / processing inside the second foreach().
If you want to get all keys of an array you may use
array_keys()
instead. Also, if each of your rows has the same keys in your second foreach loop, you may break both of the loops after getting all the keys from the first row.
Just use array_keys()
$a = array();
$array_of_keys = array_keys( $a );